google / mesop

Rapidly build AI apps in Python
https://google.github.io/mesop/
Apache License 2.0
5.41k stars 260 forks source link

Provide a simple way to return files in Mesop app #472

Closed wwwillchen closed 1 month ago

wwwillchen commented 4 months ago

Discussed in https://github.com/google/mesop/discussions/453

Originally posted by **herb** June 17, 2024 I want to setup a website that takes a URL and returns a PDF based on that URL. Back in the day that means an API endpoint that takes a Json body with the URL and returns a response with content type `application/pdf` and the contents of the PDF in the body. And then some trivial html form that has a field for the URL. How do I accomplish this with mesop? I suspect some of the hangup is the separating line between client and server when writing mesop code. But also how to accomplish the 'download file ' piece is unclear to me.
richard-to commented 2 months ago

For this, I'm wondering if we can optionally allow the user to register a download route and let the user implement some function to handle downloads.

For the simple case, we could wrap the Flask send_from_directory for Mesop usage. The issue here is that this function isn't great since it uses the local file system, so not really scalable, but could work basic uses cases. Another potential issue is people accessing other people's data. We could mitigate this a bit if we generated session IDs for users using the builtin Flask session object.

@app.route('/uploads/<path:filename>', methods=['GET', 'POST'])
def download(filename):
    return send_from_directory(directory=app.config['UPLOAD_FOLDER'], filename=filename)

This also goes back to the question of whether want to expose more of the Flask internals or not. Though I lean toward not exposing more internals since I do think we'll likely want to move away from Flask at some point.

Interface maybe something like:

me.register_downloads_route(route_function)

I guess I'm not sure if the Mesop state would be possible to make state accessible from the route function since there could be cases where we just want to serialize data from state for download. Also it seems maybe a bit too specific to register a route specifically for downloads.

kevinknights29 commented 2 months ago

Hi All,

I'm interested in this functionality as well.

Looking forward to more comments on the topic!

wwwillchen commented 1 month ago

@kevinknights29 just posted a response to: https://github.com/google/mesop/discussions/453#discussioncomment-10492369 - please take a look and let us know if you have any questions or comments.