Description
Version
1.49.1
Steps to reproduce
Write a Simple FastAPI App:
Create a FastAPI application that allows you to take screenshots of any web page
Use Synchronous or Asynchronous Methods:
You can use either synchronous or asynchronous approaches for your application
Run the Application with reload=True:
Start the FastAPI app with reload=True enabled, like this:
uvicorn.run("playwriteTest:app", host="0.0.0.0", port=8000, reload=True)
You will start encountring the error
Im attaching my code
from fastapi import FastAPI, HTTPException
from playwright.async_api import async_playwright
import uuid
import os
import uvicorn
app = FastAPI()
async def takeScreenshot(url, screenshot_path):
async with async_playwright() as playwright:
browser = await playwright.chromium.launch(headless=True)
context = await browser.new_context(ignore_https_errors=True)
page = await context.new_page()
# Go to the target URL
await page.goto(url, timeout=60000) # 60 seconds timeout
# Wait for the page to load fully
await page.wait_for_load_state("networkidle")
# Capture and save the screenshot
await page.screenshot(path=screenshot_path, full_page=True)
# Close the browser
await browser.close()
# Define a directory for storing screenshots
SCREENSHOT_DIR = "screenshots"
os.makedirs(SCREENSHOT_DIR, exist_ok=True)
@app.post("/screenshot/")
async def take_screenshot():
screenshot_path = os.path.join(SCREENSHOT_DIR, f"{uuid.uuid4()}.png")
url = 'https://pypi.org/project/playwright/'
try:
await takeScreenshot(url, screenshot_path)
except Exception as e:
raise HTTPException(
status_code=500, detail=f"Error capturing screenshot: {str(e)}"
)
return {"screenshot_path": screenshot_path, "message": "Screenshot taken successfully!"}
if __name__ == "__main__":
# if you use this you will encounter the error
uvicorn.run("playwriteTest:app", host="0.0.0.0", port=8000, reload=True)
# uncomment this it will start work
#uvicorn.run("playwriteTest:app", host="0.0.0.0", port=8000, reload=False)
Expected behavior
When running with reload=True, you may encounter the error
raise NotImplementedError
Fix the Issue by Setting reload=False:
To resolve this error, update the reload parameter to False.
Actual behavior
Run the Application with reload=True:
Start the FastAPI app with reload=True enabled, like this:
uvicorn.run("playwriteTest:app", host="0.0.0.0", port=8000, reload=True)
When running with reload=True, you may encounter the error
raise NotImplementedError
Additional context
Dear Team,
Many of us are encountering this issue, and we would greatly appreciate it if you could look into it. This is a fantastic package that we truly enjoy using, and we hope the issue can be addressed promptly. I've attached my code for your reference.
We noticed that the issue becomes apparent when running Uvicorn with the reload parameter set to True. If we set the reload parameter to False, the error does not occur. We hope this issue will be resolved in the next release.
Thank you for your attention to this matter!
Environment
currently im using windows
- my system configuration
Host Name: AC-LAP-081
OS Name: Microsoft Windows 10 Pro
OS Version: 10.0.19045 N/A Build 19045
OS Manufacturer: Microsoft Corporation
OS Configuration: Standalone Workstation
OS Build Type: Multiprocessor Free
Registered Owner: User
Registered Organization:
Product ID: 00330-54179-04263-AAOEM
Original Install Date: 29-12-2022, 01:33:44
System Boot Time: 27-12-2024, 04:25:30
System Manufacturer: LENOVO
System Model: 20VD
System Type: x64-based PC
Processor(s): 1 Processor(s) Installed.
[01]: Intel64 Family 6 Model 140 Stepping 1 GenuineIntel ~2803 Mhz
BIOS Version: LENOVO F8CN46WW(V2.09), 24-12-2021