-
QuestionI am running the program on windows 11 PC with WSL2 Ubuntu 22.04.3 LTS and am running the flet_fastapi app using uvicorn. In the guide I see the following. / (GET) - Flet app static files with SPA catch-all handler. https://flet.dev/docs/guides/python/deploying-web-app/running-flet-with-fastapi I have my assets and upload_dir path defined. I can upload files to the using upload_async but if I try to download a file in the assets folder from the server I see the following log. INFO: 127.0.0.1:39730 - "GET /assets/uploads/batErrors2023-10-13-12-32-7.csv HTTP/1.1" 304 Not Modified When the app first launches it opens a web page: http://127.0.0.1:8000/ When I press a Filled button I have setup to run launch_url_async a new tab opens with http://127.0.0.1:8000/assets/uploads/batErrors2023-10-13-12-32-7.csv but the file does not download. I would like to be able to download static files from the flet_fastapi app if possible. From what I had found I should be able to with this setup but it seems like any URL I go to just loads the flet interface. Code sampleasync def download_results_clicked(self, e):
await self.page.launch_url_async('http://127.0.0.1:8000/assets/uploads/batErrors2023-10-13-12-32-7.csv')
async def main(page: Page):
"""
Sets up a web page and adds an instance of the BulkCommandApp class to the page.
Args:
page (Page): The web page object on which the application will be displayed.
Returns:
None
"""
# Set up web page
page.title = "Bulk Command Tool"
page.horizontal_alignment = "center"
page.scroll = ScrollMode.AUTO
await page.update_async()
# Add BulkCommandApp instance to the page
app = BulkCommandApp()
await page.add_async(app)
app = flet_fastapi.app(main, assets_dir="/home/nkane/Bulk-Command-Tool/bulk_command_tool/assets/", upload_dir="/home/nkane/Bulk-Command-Tool/bulk_command_tool/assets/uploads/", secret_key=secret_key) Error messageDEBUG:flet_fastapi:_on_message: {"action":"pageEventFromWeb","payload":{"eventTarget":"_7","eventName":"click","eventData":""}}
DEBUG:flet_core:page.on_event_async: _7 click
DEBUG:flet_core:_process_command: invokeMethod ['9116a62236cb4e1595f8f4d592771687', 'launchUrl', ''] {'url': 'http://127.0.0.1:8000/assets/uploads/batErrors2023-10-13-12-32-7.csv', 'web_popup_window': 'False'}
DEBUG:flet_fastapi:__send: {"action":"invokeMethod","payload":{"methodId":"9116a62236cb4e1595f8f4d592771687","methodName":"launchUrl","controlId":"","arguments":{"url":"http://127.0.0.1:8000/assets/uploads/batErrors2023-10-13-12-32-7.csv","web_popup_window":"False"}}}
DEBUG:flet_fastapi:StaticFiles.lookup_path: assets/uploads/batErrors2023-10-13-12-32-7.csv
INFO: 127.0.0.1:50604 - "GET /assets/uploads/batErrors2023-10-13-12-32-7.csv HTTP/1.1" 304 Not Modified
INFO:flet_fastapi:New FletConnection
INFO: ('127.0.0.1', 50618) - "WebSocket /ws" [accepted]
INFO: connection open
DEBUG:flet_fastapi:_on_message: {"action":"registerWebClient","payload":{"pageName":"assets/uploads","pageRoute":"/assets/uploads/batErrors2023-10-13-12-32-7.csv","pageWidth":"0","pageHeight":"0","windowWidth":"","windowHeight":"","windowTop":"","windowLeft":"","isPWA":"false","isWeb":"true","isDebug":"false","platform":"windows","platformBrightness":"light","sessionId":""}}
INFO:flet_fastapi:New session created (3 total): oSwvyxzBfQTHDoqC
DEBUG:flet_fastapi:__send: {"action":"registerWebClient","payload":{"session":{"id":"oSwvyxzBfQTHDoqC","controls":{"page":{"i":"page","t":"page","p":"","c":[],"clientip":"127.0.0.1","clientuseragent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/119.0","debug":"false","height":"0","platform":"windows","platformbrightness":"light","pwa":"false","route":"/assets/uploads/batErrors2023-10-13-12-32-7.csv","web":"true","width":"0","windowheight":"","windowleft":"","windowtop":"","windowwidth":""}}},"error":"","appInactive":false}}
INFO:flet_fastapi:Start session: oSwvyxzBfQTHDoqC
DEBUG:flet_core:_process_command: set ['page'] {'title': 'Bulk Command Tool'}
DEBUG:flet_core:_process_command: add [] {'to': 'page', 'at': '0'}
DEBUG:flet_core:_process_command: add [] {'to': 'page', 'at': '1'}
DEBUG:flet_fastapi:__send: {"action":"pageControlsBatch","payload":[{"action":"updateControlProps","payload":{"props":[{"i":"page","title":"Bulk Command Tool"}]}},{"action":"addPageControls","payload":{"controls":[{"t":"view","i":"_1","p":"page","c":[],"at":"0","horizontalalignment":"center","scroll":"auto"}],"trimIDs":[]}},{"action":"addPageControls","payload":{"controls":[{"t":"offstage","i":"_2","p":"page","c":["_3"],"at":"1"},{"t":"clipboard","i":"_3","p":"_2","c":[]}],"trimIDs":[]}}]}
DEBUG:flet_core:_process_command: add [] {'to': '_1', 'at': '0'}
DEBUG:flet_fastapi:__send: {"action":"pageControlsBatch","payload":[{"action":"addPageControls","payload":{"controls":[{"t":"stack","i":"_4","p":"_1","c":["_5"],"at":"0"},{"t":"column","i":"_5","p":"_4","c":["_6","_8","_13","_17","_19","_21","_24","_26"],"width":"800"},{"t":"row","i":"_6","p":"_5","c":["_7"]},{"t":"elevatedbutton","i":"_7","p":"_6","c":[],"style":"{\"color\":\"onPrimary\",\"bgcolor\":\"primary\",\"elevation\":0}","text":"Download Results"},{"t":"row","i":"_8","p":"_5","c":["_9","_10","_11","_12"]},{"t":"textfield","i":"_9","p":"_8","c":[],"expand":"1","label":"Host"},{"t":"floatingactionbutton","i":"_10","p":"_8","c":[],"icon":"add"},{"t":"textfield","i":"_11","p":"_8","c":[],"expand":"1","label":"Subnet"},{"t":"floatingactionbutton","i":"_12","p":"_8","c":[],"icon":"add"},{"t":"row","i":"_13","p":"_5","c":["_14"]},{"t":"row","i":"_14","p":"_13","c":["_15","_16"]},{"t":"elevatedbutton","i":"_15","p":"_14","c":[],"icon":"upload_file","style":"{\"color\":\"onPrimary\",\"bgcolor\":\"primary\",\"elevation\":0}","text":"Select CSV"},{"t":"text","i":"_16","p":"_14","c":[]},{"t":"row","i":"_17","p":"_5","c":["_18"]},{"t":"listview","i":"_18","p":"_17","c":[],"autoscroll":"true","height":"200","padding":"20","spacing":"10"},{"t":"row","i":"_19","p":"_5","c":["_20"]},{"t":"elevatedbutton","i":"_20","p":"_19","c":[],"style":"{\"color\":\"onPrimary\",\"bgcolor\":\"primary\",\"elevation\":0}","text":"Run Bulk Commands"},{"t":"row","i":"_21","p":"_5","c":["_22","_23"]},{"t":"textfield","i":"_22","p":"_21","c":[],"expand":"1","label":"Username"},{"t":"textfield","i":"_23","p":"_21","c":[],"canrevealpassword":"true","expand":"1","label":"Password","password":"true"},{"t":"row","i":"_24","p":"_5","c":["_25"]},{"t":"textfield","i":"_25","p":"_24","c":[],"expand":"1","label":"Command"},{"t":"row","i":"_26","p":"_5","c":["_27"]},{"t":"textfield","i":"_27","p":"_26","c":[],"expand":"1","label":"TTP Template","maxlines":"100","minlines":"10","multiline":"true"}],"trimIDs":[]}}]}
DEBUG:flet_core:_process_command: add [] {'to': '_2', 'at': '0'}
DEBUG:flet_fastapi:__send: {"action":"pageControlsBatch","payload":[{"action":"addPageControls","payload":{"controls":[{"t":"filepicker","i":"_28","p":"_2","c":[],"at":"0","upload":"[]"}],"trimIDs":[]}}]}
DEBUG:flet_fastapi:_on_message: {"action":"updateControlProps","payload":{"props":[{"i":"page","width":"1920"},{"i":"page","height":"947"}]}}
DEBUG:flet_core:page.on_event_async: page change [{"i":"page","width":"1920"},{"i":"page","height":"947"}]
DEBUG:flet_fastapi:_on_message: {"action":"pageEventFromWeb","payload":{"eventTarget":"page","eventName":"resize","eventData":"1920,947"}}
DEBUG:flet_core:page.on_event_async: page resize 1920,947
DEBUG:flet_fastapi:StaticFiles.lookup_path: flutter_service_worker.js
INFO: 127.0.0.1:50604 - "GET /flutter_service_worker.js?v=3684986922 HTTP/1.1" 200 OK ------------------------------------------------------
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
This might be useful: https://flet.dev/docs/guides/python/deploying-web-app/running-flet-with-fastapi/#static-files |
Beta Was this translation helpful? Give feedback.
-
I got this working. Including /assets/ in the URL was the issue. The following code works as expected. async def download_results_clicked(self, e): |
Beta Was this translation helpful? Give feedback.
I got this working. Including /assets/ in the URL was the issue. The following code works as expected.
async def download_results_clicked(self, e):
await self.page.launch_url_async('http://127.0.0.1:8000/uploads/batErrors2023-10-13-12-32-7.csv')