import jsonimport urllib.requestimport urllib.parsedef get_history(prompt_id=None, max_items=None, offset=None): """Get execution history. Args: prompt_id: Optional specific prompt ID to retrieve max_items: Maximum number of items to return (all history only) offset: Offset for pagination (all history only) """ if prompt_id: url = f"http://127.0.0.1:8188/history/{prompt_id}" else: url = "http://127.0.0.1:8188/history" params = {} if max_items is not None: params['max_items'] = max_items if offset is not None: params['offset'] = offset if params: url += '?' + urllib.parse.urlencode(params) with urllib.request.urlopen(url) as response: return json.loads(response.read())# Get specific prompt historyprompt_id = "a1b2c3d4-e5f6-7890-abcd-ef1234567890"history = get_history(prompt_id)if prompt_id in history: prompt_history = history[prompt_id] print(f"Status: {prompt_history['status']['status_str']}") # Extract image outputs for node_id, node_output in prompt_history['outputs'].items(): if 'images' in node_output: for image in node_output['images']: print(f"Image: {image['filename']}") print(f" Subfolder: {image['subfolder']}") print(f" Type: {image['type']}")# Get paginated historyrecent_history = get_history(max_items=20, offset=0)print(f"Retrieved {len(recent_history)} history items")
Once you have the history, use the /view endpoint to download images:
import urllib.requestimport urllib.parsedef get_image(filename, subfolder, folder_type): """Download an image from the history.""" data = { "filename": filename, "subfolder": subfolder, "type": folder_type } url_values = urllib.parse.urlencode(data) url = f"http://127.0.0.1:8188/view?{url_values}" with urllib.request.urlopen(url) as response: return response.read()# Get history and download imageshistory = get_history(prompt_id)for node_id in history[prompt_id]['outputs']: node_output = history[prompt_id]['outputs'][node_id] if 'images' in node_output: for image in node_output['images']: image_data = get_image( image['filename'], image['subfolder'], image['type'] ) # Save or process image_data with open(image['filename'], 'wb') as f: f.write(image_data) print(f"Downloaded: {image['filename']}")
History is stored in memory and will be cleared when the server restarts. For persistent storage, implement your own database layer.
Use the WebSocket API to receive real-time notifications when execution completes, then immediately fetch the history to get results.
The prompt array in the response includes sensitive data in position 5 (index 5), which is removed from queue responses but present in history. Handle this data carefully.