File picker or folder selector for local running app #283
Replies: 16 comments 29 replies
-
For example, I would like users click a button which pop up a dialog to select a file or a folder. Then, the file-path or folder-path is returned and I can do something else with it (i.e. loading an image and do some image filtering...) |
Beta Was this translation helpful? Give feedback.
-
The file picker dialog uses the internals of the browser. For security reasons it does not provide the full path of the selected files. See #269 for a similar discussion. But you could build your own file browser which acts on the filesystem of the local running app. |
Beta Was this translation helpful? Give feedback.
-
Since we can't use Quasar's file picker for getting file paths, we will probably need to implement some kind of file picker ourselves. A first prove of concept is probably only a few lines of code. Does anyone from the community like to try it? |
Beta Was this translation helpful? Give feedback.
-
Thanks very much. Please do. I keen to try it. |
Beta Was this translation helpful? Give feedback.
-
Streamlit users would like to have the same thing: streamlit/streamlit#904 (ticket from 2020) but still waiting. It would be great if nicegui can do that first. |
Beta Was this translation helpful? Give feedback.
-
@nghia-vo Absolutely! 😎
There are two noteworthy workarounds:
Another weird and probably unstable approach is to combine streamlit with tkinter or wx: |
Beta Was this translation helpful? Give feedback.
-
I've just tried the tkinter solution. It didn't work with nicegui (I created a button to call the file_picker function, but nothing returns when clicked). For streamlit, it returns an error related to thread usage. |
Beta Was this translation helpful? Give feedback.
-
I just created an example with a local file picker built in NiceGUI. It uses an AG Grid to show the content of the current directory and allows for up/down navigation and multi-selection. An There might be some edge cases and security risks I haven't thought of (e.g. what about links?), but that's why I uploaded it as an example and not an official NiceGUI element (yet). Please let me know what you think! 🙂 https://github.com/zauberzeug/nicegui/tree/main/examples/local_file_picker |
Beta Was this translation helpful? Give feedback.
-
I've just tested that, but the web browser keeps loading... |
Beta Was this translation helpful? Give feedback.
-
Related discussion to request about see files in other drivers: |
Beta Was this translation helpful? Give feedback.
-
@nghia-vo said:
Do you mean different "disks" in Windows? Eg. |
Beta Was this translation helpful? Give feedback.
-
On Unix you can use an >>> Path('/some/path').parent.parent
PosixPath('/') So the condition On the other hand, the origin of a Windows path seems to be ".": >>> Path('C:/some/path').parent.parent.parent
PosixPath('.') Therefore I guess you can try |
Beta Was this translation helpful? Give feedback.
-
I just pushed 6aa4956 to allow |
Beta Was this translation helpful? Give feedback.
-
I've got a tkinter approach, from nicegui import ui
import tkinter as tk
from tkinter import filedialog
root = tk.Tk()
root.withdraw()
def run_tk_filedialog(e):
root.wm_attributes("-topmost", 1) # to make sure the filedialog pops up at the top
file_path = filedialog.askopenfilename()
root.wm_attributes("-topmost", 0)
mylabel.text = str(file_path)
mylabel = ui.label('File path: ')
my_btn = ui.button("pick", on_click=run_tk_filedialog)
ui.run() it seems ok while running in native mode. But if I deploy the app on one machine and visit the app through webbrowser by another PC, clicking the button will only show a file dialog on the server machine, the visiting PC get nothing. Hope any one can solve this! |
Beta Was this translation helpful? Give feedback.
-
I think this is finally solved. For web apps there is no way around an implementation similar to our local file picker example. And thanks to @polaar, we also now have the ability to open a regular file picker in native mode: from nicegui import app, ui
async def choose_file():
files = await app.native.main_window.create_file_dialog(allow_multiple=True)
for file in files:
ui.notify(file)
ui.button('choose file', on_click=choose_file)
ui.run(native=True) |
Beta Was this translation helpful? Give feedback.
-
Hi, I've added a drive selector for Windows (see #1028) based on @vickorian's comment. |
Beta Was this translation helpful? Give feedback.
-
For local running app, it's critical to be able to select local files (i.e open file picker dialog). In my applications, file-size can be > 20GB. Is it possible to modify upload module for this purpose?
Beta Was this translation helpful? Give feedback.
All reactions