[Python] Redirect to another webpage after background task is complete

Discussão em 'Python' iniciado por Stack, Dezembro 7, 2024.

  1. Stack

    Stack Membro Participativo

    I have an endpoint which triggers a process in FastAPI. The process takes several minutes and produces a CSV file that can later be downloaded by the users.

    How can I return a message saying that the process is running and redirecting the users to the "Download" page after the process has finished.

    So far, I am returning an HTML response pointing to the "Download" page, but if the user clicks on it before the process has finished, they may not get the correct results:

    @router.post("/run")
    async def run(background_tasks: BackgroundTasks) -> HTMLResponse:
    """
    1. Run model.
    2. Retrieve output file.
    """

    background_tasks.add_task(optimization_process)

    content = """
    <label for="file">Generating results:</label><br>
    <progress id="file" max="100" value="70"> 70% </progress><br>
    <body> Visit <a href="./get-results">result page</a> to get the results </body>
    """
    return HTMLResponse(content=content)


    I am looking to improve this setup by informing the users that the process has started and redirecting them to the next screen after the successful run of optimization_process.

    Continue reading...

Compartilhe esta Página