Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 16714

Best practices to run Playwright asyncio.run(main()) inside FastAPI function

$
0
0

I'm building an API with FastAPI and Playwright and I'm wondering how I should I write my code.

This is Playwright Asyncio:

import asynciofrom playwright.async_api import async_playwrightasync def main():    async with async_playwright() as p:        browser = await p.chromium.launch()        page = await browser.new_page()        await page.goto("http://playwright.dev")        print(await page.title())        await browser.close()asyncio.run(main())

and this is FastAPI:

from fastapi import FastAPIapp = FastAPI()@app.get("/")def read_root():    return {"Hello": "World"}@app.get("/playwright")def run_playwright():    return "Results from running Playwright"

How can I run Playright asyncio inside a FastAPI function?

Documentation:

FastAPI: https://fastapi.tiangolo.com/

Playwright: https://playwright.dev/python/docs/library#usage


Viewing all articles
Browse latest Browse all 16714

Trending Articles