File Download element #543
Replies: 3 comments 2 replies
-
If you have just static files you can look at https://nicegui.io/reference#add_static_files_ for a straight forward example. To generate content on the fly you can use FastAPI's ability for arbitrary responses: https://nicegui.io/reference#api_responses. |
Beta Was this translation helpful? Give feedback.
-
Absolutely, you can link a file download to a standard button by utilizing FastAPI techniques. While this method may not fully embody the self-containment of the NiceGUI elements. from nicegui import app, ui
from fastapi.responses import FileResponse
# Creating a file for testing
filepath = "test.txt"
with open(filepath, "w", encoding="utf-8") as f:
f.write("Sample text")
@app.get("/download")
def download_file():
return FileResponse(path=filepath, media_type='application/octet-stream')
ui.button('Download file', on_click=lambda: ui.open('/download'))
ui.run() The same feature can be implemented via add_static_files. It's just not clear how to set the media type to prevent the browser from displaying the file in a new tab if it can. So, the example below doesn't really allow you to download a file in txt format. Although for other non-displayable formats, the download of the file is started. from nicegui import app, ui
filepath = "test.txt"
app.add_static_files('/download', '.')
ui.button('Download file', on_click=lambda: ui.open(f'/download/{filepath}'))
ui.run() |
Beta Was this translation helpful? Give feedback.
-
Since NiceGUI 1.2.7 there is a |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hey there,
I must say, this framework is fantastic! Highly customizable and comes with a plethora of intricate elements ready to use.
However, I'm struggling to figure out how to download files using a simple and standard method. Is this element missing, or am I overlooking something?
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions