from datetime import datetime
import matplotlib.pyplot as plt
script_dir = os.path.dirname(os.path.abspath(__file__))
images_dir = os.path.join(script_dir, "comfyui_history_images")
os.makedirs(images_dir, exist_ok=True)
def get_comfyui_history(server_url="http://127.0.0.1:7860", max_entries=None):
server_url: ComfyUI服务器地址 (默认: http://127.0.0.1:7860)
max_entries: 最多返回的记录数 (默认: 返回全部)
history_url = f"{server_url}/history"
response = requests.get(history_url, headers=headers)
print(f"----Response Headers----: {response.headers}")
response.raise_for_status()
history_data = response.json()
if max_entries and max_entries > 0:
items = list(history_data.items())
sorted_history = dict(items[:max_entries])
sorted_history = history_data
except requests.exceptions.RequestException as e:
except json.JSONDecodeError:
def download_image(image_url, save_path=None):
response = requests.get(image_url, headers=headers)
response.raise_for_status()
print(f"----Response Headers----: {response.headers}")
img = Image.open(io.BytesIO(response.content))
print(f"图像已保存到: {save_path}")
plt.figure(figsize=(8, 8))
print(f"下载/显示图像失败: {e}")
def print_history_summary(history, server_url="http://127.0.0.1:8188", show_images=True):
server_url: ComfyUI服务器地址
print(f"发现 {len(history)} 条历史记录")
for i, (prompt_id, task) in enumerate(history.items()):
status = "已完成" if task['status']['completed'] else "未完成"
for node_id, node_output in task['outputs'].items():
if 'images' in node_output:
for image in node_output['images']:
image_url = f"{server_url}/view?filename={image['filename']}&type=output"
image_urls.append(image_url)
print(f"├─ Prompt ID: {prompt_id}")
print(f"├─ 状态: {status}")
print(f"├─ 生成图像: {len(image_urls)} 张")
if show_images and image_urls:
image_url = image_urls[0]
save_path = os.path.join(images_dir, f"{prompt_id}.png")
if download_image(image_url, save_path):
print(f"├─ 显示完成: {image_url}")
print(f"├─ 其他图像URL ({len(image_urls)-1}张):")
for j, url in enumerate(image_urls[1:3]):
print(f"│ ├─ 图像 {j+2}: {url}")
print(f"│ └─ ...还有 {len(image_urls)-3} 张图片")
for j, url in enumerate(image_urls[:3]):
print(f"│ ├─ 图像 {j+1}: {url}")
print(f"│ └─ ...还有 {len(image_urls)-3} 张图片")
if __name__ == "__main__":
server_address = "http://sdxxxxxxxxxxpjg.apigateway-cn-beijing.volceapi.com"
history = get_comfyui_history(server_url=server_address, max_entries=1)
print_history_summary(history, server_url=server_address, show_images=True)
comfyui_history = os.path.join(script_dir, "comfyui_history.json")
with open(comfyui_history, 'w') as f:
json.dump(history, f, indent=2)
print("\n完整历史记录已保存到 comfyui_history.json")