How do you open a "Choose File" prompt from Julia?

I use file.choose() in R to open a “Choose File” window prompt to print a file path for files that I want to reference in my scripts.

Is there a Julia equivalent for this?

One option is https://juliagraphics.github.io/Gtk.jl/latest/manual/filedialogs/

2 Likes

Gtk.jl is the easiest option. If you like tinkering you can roll your own browser with CImGui.jl, by using Julia’s readdir and walkdir and populating CImGui windows with their contents (it will be snappier on Windows).
Here’s an example of listing the contents of a dir and printing the currently selected item in a window (the code might seem long, but the magic is done inside the bold widget comments):

using CImGui
using CImGui.CSyntax
using CImGui.CSyntax.CStatic
using CImGui.GLFWBackend
using CImGui.OpenGLBackend
using CImGui.GLFWBackend.GLFW
using CImGui.OpenGLBackend.ModernGL
using Printf

@static if Sys.isapple()
    # OpenGL 3.2 + GLSL 150
    const glsl_version = 150
    GLFW.WindowHint(GLFW.CONTEXT_VERSION_MAJOR, 3)
    GLFW.WindowHint(GLFW.CONTEXT_VERSION_MINOR, 2)
    GLFW.WindowHint(GLFW.OPENGL_PROFILE, GLFW.OPENGL_CORE_PROFILE) # 3.2+ only
    GLFW.WindowHint(GLFW.OPENGL_FORWARD_COMPAT, GL_TRUE) # required on Mac
else
    # OpenGL 3.0 + GLSL 130
    const glsl_version = 130
    GLFW.WindowHint(GLFW.CONTEXT_VERSION_MAJOR, 3)
    GLFW.WindowHint(GLFW.CONTEXT_VERSION_MINOR, 0)
    # GLFW.WindowHint(GLFW.OPENGL_PROFILE, GLFW.OPENGL_CORE_PROFILE) # 3.2+ only
    # GLFW.WindowHint(GLFW.OPENGL_FORWARD_COMPAT, GL_TRUE) # 3.0+ only
end

# setup GLFW error callback
error_callback(err::GLFW.GLFWError) = @error "GLFW ERROR: code $(err.code) msg: $(err.description)"
GLFW.SetErrorCallback(error_callback)

# create window
window = GLFW.CreateWindow(800, 600, "Dir contents")
@assert window != C_NULL
GLFW.MakeContextCurrent(window)
GLFW.SwapInterval(1)  # enable vsync

# setup Dear ImGui context
ctx = CImGui.CreateContext()
CImGui.StyleColorsClassic()
fonts_dir = joinpath(@__DIR__, "..", "fonts")
fonts = CImGui.GetIO().Fonts
CImGui.AddFontFromFileTTF(fonts, joinpath(fonts_dir, "Roboto-Medium.ttf"), 16)

# setup Platform/Renderer bindings
ImGui_ImplGlfw_InitForOpenGL(window, true)
ImGui_ImplOpenGL3_Init(glsl_version)

try
    # DEFINE YOUR WIDGETS
    default_path = "C:\\Julia-1.4.0"
    show_list = false
    list=Cstring.(pointer.(readdir(default_path)))
    clear_color = Cfloat[0.45, 0.55, 0.60, 1.00]
    while !GLFW.WindowShouldClose(window)
        GLFW.PollEvents()
        # start the Dear ImGui frame
        ImGui_ImplOpenGL3_NewFrame()
        ImGui_ImplGlfw_NewFrame()
        CImGui.NewFrame()
        # we use a Begin/End pair to created a named window.
        @cstatic begin
            CImGui.Begin("List dir contents")
            if CImGui.MenuItem("Open")
                show_list = true
            end
            CImGui.End()
        end
        # show files list
        if show_list
            @c CImGui.Begin("Dir contents", &show_list)
            @cstatic listbox_item_current=Cint(0) begin
            CImGui.Button("Close Me") && (show_list = false;)
            # list box
            @c CImGui.ListBox("listbox\n(single select)", &listbox_item_current, list, length(list), 5)
            crt_pick = unsafe_string(list[listbox_item_current+1])
            CImGui.Text("You selected $crt_pick")
            CImGui.End()
        end
    end
    # END WIDGETS

    # rendering
    CImGui.Render()
    GLFW.MakeContextCurrent(window)
    display_w, display_h = GLFW.GetFramebufferSize(window)
    glViewport(0, 0, display_w, display_h)
    glClearColor(clear_color...)
    glClear(GL_COLOR_BUFFER_BIT)
    ImGui_ImplOpenGL3_RenderDrawData(CImGui.GetDrawData())

    GLFW.MakeContextCurrent(window)
    GLFW.SwapBuffers(window)
    sleep(0.03) #can this be used as update rate?
end
catch e
    @error "Error in renderloop!" exception=e
    Base.show_backtrace(stderr, catch_backtrace())
finally
    ImGui_ImplOpenGL3_Shutdown()
    ImGui_ImplGlfw_Shutdown()
    CImGui.DestroyContext(ctx)
    GLFW.DestroyWindow(window)
end

On Windows 10

You’re trying to pass in the value of a variable called title, which evidently you never set. Try this: open_dialog("My first file dialog").

3 Likes

Thank you.