Spaces:
Running
Running
Delete capture_screenshots.py
Browse files- capture_screenshots.py +0 -266
capture_screenshots.py
DELETED
|
@@ -1,266 +0,0 @@
|
|
| 1 |
-
"""
|
| 2 |
-
Screenshot capture tool for ChatUI Helper documentation
|
| 3 |
-
Uses Playwright to automate browser interactions and capture screenshots
|
| 4 |
-
"""
|
| 5 |
-
|
| 6 |
-
import asyncio
|
| 7 |
-
import os
|
| 8 |
-
from pathlib import Path
|
| 9 |
-
from playwright.async_api import async_playwright
|
| 10 |
-
import logging
|
| 11 |
-
from datetime import datetime
|
| 12 |
-
|
| 13 |
-
# Configure logging
|
| 14 |
-
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(message)s')
|
| 15 |
-
logger = logging.getLogger(__name__)
|
| 16 |
-
|
| 17 |
-
# Screenshot configurations for each documentation step
|
| 18 |
-
SCREENSHOT_CONFIGS = [
|
| 19 |
-
{
|
| 20 |
-
"name": "main_interface",
|
| 21 |
-
"url": "http://localhost:7860",
|
| 22 |
-
"description": "Main interface with all tabs",
|
| 23 |
-
"actions": [],
|
| 24 |
-
"wait_time": 2000,
|
| 25 |
-
"selector": None, # Full page
|
| 26 |
-
"output": "img/main_interface.png"
|
| 27 |
-
},
|
| 28 |
-
{
|
| 29 |
-
"name": "config_tab",
|
| 30 |
-
"url": "http://localhost:7860",
|
| 31 |
-
"description": "Configuration tab",
|
| 32 |
-
"actions": [
|
| 33 |
-
{"type": "click", "selector": "button:has-text('Configure Space')"}
|
| 34 |
-
],
|
| 35 |
-
"wait_time": 1000,
|
| 36 |
-
"selector": None,
|
| 37 |
-
"output": "img/config_tab.png"
|
| 38 |
-
},
|
| 39 |
-
{
|
| 40 |
-
"name": "template_dropdown",
|
| 41 |
-
"url": "http://localhost:7860",
|
| 42 |
-
"description": "Template selection dropdown",
|
| 43 |
-
"actions": [
|
| 44 |
-
{"type": "click", "selector": "button:has-text('Configure Space')"},
|
| 45 |
-
{"type": "click", "selector": "div[data-testid='dropdown']", "nth": 0}
|
| 46 |
-
],
|
| 47 |
-
"wait_time": 1000,
|
| 48 |
-
"selector": "div.dropdown-menu",
|
| 49 |
-
"output": "img/template_dropdown.png"
|
| 50 |
-
},
|
| 51 |
-
{
|
| 52 |
-
"name": "preview_tab",
|
| 53 |
-
"url": "http://localhost:7860",
|
| 54 |
-
"description": "Preview tab with chat interface",
|
| 55 |
-
"actions": [
|
| 56 |
-
{"type": "click", "selector": "button:has-text('Preview')"}
|
| 57 |
-
],
|
| 58 |
-
"wait_time": 2000,
|
| 59 |
-
"selector": None,
|
| 60 |
-
"output": "img/preview_tab.png"
|
| 61 |
-
},
|
| 62 |
-
{
|
| 63 |
-
"name": "preview_chat_example",
|
| 64 |
-
"url": "http://localhost:7860",
|
| 65 |
-
"description": "Preview tab with example conversation",
|
| 66 |
-
"actions": [
|
| 67 |
-
{"type": "click", "selector": "button:has-text('Preview')"},
|
| 68 |
-
{"type": "wait", "time": 2000},
|
| 69 |
-
{"type": "type", "selector": "textarea[placeholder*='Type a message']", "text": "Hello! Can you help me understand quantum mechanics?"},
|
| 70 |
-
{"type": "click", "selector": "button:has-text('Send')"},
|
| 71 |
-
{"type": "wait", "time": 3000}
|
| 72 |
-
],
|
| 73 |
-
"wait_time": 1000,
|
| 74 |
-
"selector": None,
|
| 75 |
-
"output": "img/preview_chat_example.png"
|
| 76 |
-
},
|
| 77 |
-
{
|
| 78 |
-
"name": "docs_tab",
|
| 79 |
-
"url": "http://localhost:7860",
|
| 80 |
-
"description": "Documentation tab",
|
| 81 |
-
"actions": [
|
| 82 |
-
{"type": "click", "selector": "button:has-text('Docs')"}
|
| 83 |
-
],
|
| 84 |
-
"wait_time": 1000,
|
| 85 |
-
"selector": None,
|
| 86 |
-
"output": "img/docs_tab.png"
|
| 87 |
-
},
|
| 88 |
-
{
|
| 89 |
-
"name": "docs_quickstart",
|
| 90 |
-
"url": "http://localhost:7860",
|
| 91 |
-
"description": "Documentation quick start guide",
|
| 92 |
-
"actions": [
|
| 93 |
-
{"type": "click", "selector": "button:has-text('Docs')"},
|
| 94 |
-
{"type": "wait", "time": 1000},
|
| 95 |
-
{"type": "click", "selector": "button:has-text('📖 Quick Start Guide')"}
|
| 96 |
-
],
|
| 97 |
-
"wait_time": 1000,
|
| 98 |
-
"selector": None,
|
| 99 |
-
"output": "img/docs_quickstart.png"
|
| 100 |
-
},
|
| 101 |
-
{
|
| 102 |
-
"name": "config_system_prompt",
|
| 103 |
-
"url": "http://localhost:7860",
|
| 104 |
-
"description": "System configuration section",
|
| 105 |
-
"actions": [
|
| 106 |
-
{"type": "click", "selector": "button:has-text('Configure Space')"},
|
| 107 |
-
{"type": "wait", "time": 1000},
|
| 108 |
-
{"type": "scroll", "y": 500}
|
| 109 |
-
],
|
| 110 |
-
"wait_time": 1000,
|
| 111 |
-
"selector": None,
|
| 112 |
-
"output": "img/config_system_prompt.png"
|
| 113 |
-
},
|
| 114 |
-
{
|
| 115 |
-
"name": "config_api_section",
|
| 116 |
-
"url": "http://localhost:7860",
|
| 117 |
-
"description": "API configuration section",
|
| 118 |
-
"actions": [
|
| 119 |
-
{"type": "click", "selector": "button:has-text('Configure Space')"},
|
| 120 |
-
{"type": "wait", "time": 1000},
|
| 121 |
-
{"type": "scroll", "y": 1200}
|
| 122 |
-
],
|
| 123 |
-
"wait_time": 1000,
|
| 124 |
-
"selector": None,
|
| 125 |
-
"output": "img/config_api_section.png"
|
| 126 |
-
},
|
| 127 |
-
{
|
| 128 |
-
"name": "generate_button",
|
| 129 |
-
"url": "http://localhost:7860",
|
| 130 |
-
"description": "Generate deployment package button",
|
| 131 |
-
"actions": [
|
| 132 |
-
{"type": "click", "selector": "button:has-text('Configure Space')"},
|
| 133 |
-
{"type": "wait", "time": 1000},
|
| 134 |
-
{"type": "scroll", "y": 2000}
|
| 135 |
-
],
|
| 136 |
-
"wait_time": 1000,
|
| 137 |
-
"selector": "button:has-text('🗳️ Generate Deployment Package')",
|
| 138 |
-
"output": "img/generate_button.png"
|
| 139 |
-
}
|
| 140 |
-
]
|
| 141 |
-
|
| 142 |
-
async def capture_screenshot(page, config):
|
| 143 |
-
"""Capture a single screenshot based on configuration"""
|
| 144 |
-
logger.info(f"Capturing screenshot: {config['name']} - {config['description']}")
|
| 145 |
-
|
| 146 |
-
try:
|
| 147 |
-
# Navigate to URL
|
| 148 |
-
await page.goto(config['url'])
|
| 149 |
-
|
| 150 |
-
# Execute actions
|
| 151 |
-
for action in config.get('actions', []):
|
| 152 |
-
if action['type'] == 'click':
|
| 153 |
-
selector = action['selector']
|
| 154 |
-
nth = action.get('nth', 0)
|
| 155 |
-
if nth > 0:
|
| 156 |
-
await page.locator(selector).nth(nth).click()
|
| 157 |
-
else:
|
| 158 |
-
await page.click(selector)
|
| 159 |
-
await page.wait_for_timeout(500)
|
| 160 |
-
|
| 161 |
-
elif action['type'] == 'type':
|
| 162 |
-
await page.fill(action['selector'], action['text'])
|
| 163 |
-
|
| 164 |
-
elif action['type'] == 'wait':
|
| 165 |
-
await page.wait_for_timeout(action['time'])
|
| 166 |
-
|
| 167 |
-
elif action['type'] == 'scroll':
|
| 168 |
-
await page.evaluate(f"window.scrollBy(0, {action['y']})")
|
| 169 |
-
await page.wait_for_timeout(500)
|
| 170 |
-
|
| 171 |
-
# Wait for any animations to complete
|
| 172 |
-
await page.wait_for_timeout(config.get('wait_time', 1000))
|
| 173 |
-
|
| 174 |
-
# Take screenshot
|
| 175 |
-
output_path = Path(config['output'])
|
| 176 |
-
output_path.parent.mkdir(parents=True, exist_ok=True)
|
| 177 |
-
|
| 178 |
-
if config.get('selector'):
|
| 179 |
-
# Screenshot of specific element
|
| 180 |
-
element = page.locator(config['selector'])
|
| 181 |
-
await element.screenshot(path=str(output_path))
|
| 182 |
-
else:
|
| 183 |
-
# Full page screenshot
|
| 184 |
-
await page.screenshot(path=str(output_path), full_page=False)
|
| 185 |
-
|
| 186 |
-
logger.info(f"✅ Saved screenshot to {output_path}")
|
| 187 |
-
return True
|
| 188 |
-
|
| 189 |
-
except Exception as e:
|
| 190 |
-
logger.error(f"❌ Failed to capture {config['name']}: {str(e)}")
|
| 191 |
-
return False
|
| 192 |
-
|
| 193 |
-
async def capture_all_screenshots():
|
| 194 |
-
"""Capture all configured screenshots"""
|
| 195 |
-
logger.info("Starting screenshot capture process...")
|
| 196 |
-
|
| 197 |
-
# Ensure the app is running
|
| 198 |
-
logger.info("Make sure the Gradio app is running on http://localhost:7860")
|
| 199 |
-
|
| 200 |
-
async with async_playwright() as p:
|
| 201 |
-
# Launch browser
|
| 202 |
-
browser = await p.chromium.launch(headless=False) # Set to True for headless mode
|
| 203 |
-
context = await browser.new_context(
|
| 204 |
-
viewport={'width': 1280, 'height': 800},
|
| 205 |
-
device_scale_factor=2 # Higher quality screenshots
|
| 206 |
-
)
|
| 207 |
-
page = await context.new_page()
|
| 208 |
-
|
| 209 |
-
# Capture each screenshot
|
| 210 |
-
success_count = 0
|
| 211 |
-
for config in SCREENSHOT_CONFIGS:
|
| 212 |
-
if await capture_screenshot(page, config):
|
| 213 |
-
success_count += 1
|
| 214 |
-
await page.wait_for_timeout(1000) # Brief pause between screenshots
|
| 215 |
-
|
| 216 |
-
# Clean up
|
| 217 |
-
await browser.close()
|
| 218 |
-
|
| 219 |
-
logger.info(f"\nCapture complete! {success_count}/{len(SCREENSHOT_CONFIGS)} screenshots captured successfully.")
|
| 220 |
-
|
| 221 |
-
# Create a summary report
|
| 222 |
-
report_path = Path("img/screenshot_report.txt")
|
| 223 |
-
with open(report_path, 'w') as f:
|
| 224 |
-
f.write(f"Screenshot Capture Report\n")
|
| 225 |
-
f.write(f"Generated: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n")
|
| 226 |
-
f.write(f"Total Screenshots: {len(SCREENSHOT_CONFIGS)}\n")
|
| 227 |
-
f.write(f"Successful: {success_count}\n\n")
|
| 228 |
-
|
| 229 |
-
f.write("Screenshots Generated:\n")
|
| 230 |
-
for config in SCREENSHOT_CONFIGS:
|
| 231 |
-
f.write(f"- {config['output']}: {config['description']}\n")
|
| 232 |
-
|
| 233 |
-
logger.info(f"Report saved to {report_path}")
|
| 234 |
-
|
| 235 |
-
async def capture_single_screenshot(name):
|
| 236 |
-
"""Capture a single screenshot by name"""
|
| 237 |
-
config = next((c for c in SCREENSHOT_CONFIGS if c['name'] == name), None)
|
| 238 |
-
if not config:
|
| 239 |
-
logger.error(f"No configuration found for screenshot: {name}")
|
| 240 |
-
return
|
| 241 |
-
|
| 242 |
-
async with async_playwright() as p:
|
| 243 |
-
browser = await p.chromium.launch(headless=False)
|
| 244 |
-
context = await browser.new_context(
|
| 245 |
-
viewport={'width': 1280, 'height': 800},
|
| 246 |
-
device_scale_factor=2
|
| 247 |
-
)
|
| 248 |
-
page = await context.new_page()
|
| 249 |
-
|
| 250 |
-
await capture_screenshot(page, config)
|
| 251 |
-
|
| 252 |
-
await browser.close()
|
| 253 |
-
|
| 254 |
-
def main():
|
| 255 |
-
"""Main entry point"""
|
| 256 |
-
import sys
|
| 257 |
-
|
| 258 |
-
if len(sys.argv) > 1:
|
| 259 |
-
# Capture specific screenshot
|
| 260 |
-
asyncio.run(capture_single_screenshot(sys.argv[1]))
|
| 261 |
-
else:
|
| 262 |
-
# Capture all screenshots
|
| 263 |
-
asyncio.run(capture_all_screenshots())
|
| 264 |
-
|
| 265 |
-
if __name__ == "__main__":
|
| 266 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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