Could I use function like QFileDialog.getExistingDirectory in napari through magicgui?

I want to make custom saving function in napari.
So I want to use function like QFileDialog.getExistingDirectory to get the path for save images.

    view = napari.view_image(images)
    @magicgui(call_button="select output dir")
    def show_dialog_out(out_dir: str = ''):
        default_path = os.path.expanduser('~')
        out_dir = QFileDialog.getExistingDirectory('Select Directory', default_path)
    gui = show_dialog_out.Gui()
    view1.window.add_dock_widget(gui, area='bottom')
    out_dir = gui.out_dir

error is

TypeError: getExistingDirectory(parent: QWidget = None, caption: str = '', directory: str = '', options: Union[QFileDialog.Options, QFileDialog.Option] = QFileDialog.ShowDirsOnly): argument 1 has unexpected type 'str'

How could I use any kinds of way to use dialog to get the path ?

1 Like

The master version of magicgui allows you to create a native file dialog automatically by using the pathlib.Path type annotation. See this example for more details:

You can use mode='w' for saving.

Perhaps @talley can cut a new release soon so that this is available on PyPI? :smiley:

Thanks @jni !
I installed latest master branch and try the example.
This is what I need.
Additionally, would you tell me how to get the filename, which filepicker returns? and also, how could I select the directory?

1 Like

how could I select the directory?

To select an existing directory, use mode='d'.

would you tell me how to get the filename, which filepicker returns?

If you click through to the example script, you can see that you can connect to the filename_changed event. In other words, when the user selects a file, you can use a function that takes in the selected file and “connect it” to the event. This means that when the user selects a file, that function will run with the filename as input.

@talley is the event bound by the input parameter name or the returned parameter name? It’s ambiguous in the script!

1 Like

just released magicgui v0.1.6 on PyPI.

Additionally, would you tell me how to get the filename, which filepicker returns

in “standard” magicgui usage you “get”/use the value for the parameter in the body of the function being decorated. See the print statement in the example above where the filename value is actually consumed. If you want to have that function called whenever the filepath (or any other widget in the GUI) changes, just add “auto_call = True” to your decorator:

def my_func(filepath: Path):
    # do something with chosen filepath

lastly, as @jni mentioned, you can also hook up a callback to listen to just the filepath parameter change:

widget = my_func.Gui()
1 Like

Thank you so much!!! @talley @jni
I was able to implement all the function I want!