Select a folder with bokeh

I’m looking for a way to select a folder with bokeh in python. I know I can select a file by importing Fileinput, is there a similar way for directory?

No it’s not possible with Bokeh, and I am not sure it is even possible in principle, since browsers will not return local filesystem path information due to security concerns (which is why, for example, the FileInput can only return the file name and nothing more about the full path)

I have stumbled over a similar problem, I really wanted to have the FULL filepath, which is impossible just like @Bryan said.

However, if you are using the bokeh server AND the server is running on your local machine you could use a Desktop-based file selector, instead, that supports selecting a folder.
E.g., clicking a button makes the bokeh-server open a tkinter directoryselect-window and you can then use the path of the selected directory in whatever way.

I used something like this:

Tkinter askdirectory
from tkinter import Tk
from tkinter.filedialog import askdirectory

from import curdoc
from bokeh.layouts import column
from bokeh.models import Button, PreText

selected_path_tf = PreText()

def select_file():
    root = Tk()
    root.attributes('-topmost', True)
    dirname = askdirectory()  # blocking
    if dirname:
        selected_path_tf.text = dirname

dir_input_btn = Button(label="Select Directory")
dir_input_btn.on_click(lambda x: select_file())

curdoc().add_root(column(dir_input_btn, selected_path_tf))

Alternatively you can collect all directories of your current file system and use a modified Table to display them for the user to choose one. That’s how most people do it (google drive for example).


This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.