Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting a "connection lost" error when trying to use the file picker in native mode #1430

Open
varchasgopalaswamy opened this issue Aug 16, 2023 · 11 comments
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@varchasgopalaswamy
Copy link

Description

I'm trying to use the example case provided in #283

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)

but when I try to run this, I get a message "Connection lost", and the file browser doesn't show up.

@rodja
Copy link
Member

rodja commented Aug 16, 2023

The posted code is working for me. Maybe its an operating system issue? I'm on Mac OS with Chrome.

@varchasgopalaswamy
Copy link
Author

Interesting. I'm on RHEL 9 and using GTK for Webview. I found QT did not work (I was getting a white blank screen). I am able to launch the file browser using Webview, e.g.

import webview


def open_file_dialog(window):
    file_types = ('Image Files (*.bmp;*.jpg;*.gif)', 'All files (*.*)')

    result = window.create_file_dialog(webview.OPEN_DIALOG, allow_multiple=True, file_types=file_types)
    print(result)


if __name__ == '__main__':
    window = webview.create_window('Open file dialog example', 'https://pywebview.flowrl.com/hello')
    webview.start(open_file_dialog, window)

so I guess it's not an issue with Webview?

@varchasgopalaswamy
Copy link
Author

I turned on uvicorn_debug and got

DEBUG: < TEXT '42["event",{"id":12,"listener_id":"a96b632f-5e0...3cfa3ff0eb","args":[]}]' [84 bytes]

when I clicked on the "open file" dialog. I guess it's not able to get a connection to something since there's no reply?

@rodja
Copy link
Member

rodja commented Aug 17, 2023

It may be some code is blocking the main thread. Can anyone on Linux with GTK confirm this issue?

@rodja rodja added the question Further information is requested label Aug 17, 2023
@falkoschindler falkoschindler added the help wanted Extra attention is needed label Aug 24, 2023
@falkoschindler falkoschindler removed the question Further information is requested label Oct 5, 2023
@falkoschindler
Copy link
Contributor

@NiklasNeugebauer, @JensOgorek, I already tried to reproduce this issue with @denniswittich, but we couldn't get GTK running. Could one of you, please, try to run the code from the original post and see if the file dialog works? Thanks!

@JensOgorek
Copy link
Contributor

JensOgorek commented Oct 19, 2023

I can confirm the error. Tried the code above, clicked on "Choose File" and connection is lost.

@falkoschindler falkoschindler added the bug Something isn't working label Oct 19, 2023
@Liquidmasl
Copy link

to add some info, for me it works fine if I am in debug mode (pytorch) but i get the same issue when i am in run mode.

@falkoschindler
Copy link
Contributor

I just reproduced the issue on an Ubuntu machine, but I have no clue why the dialog doesn't open.

I thought it might have to do with the button click, but this minimal pywebview example works perfectly fine:

import webview

class Api:
    def open_file_dialog(self):
        print(window.create_file_dialog(webview.OPEN_DIALOG, allow_multiple=True, file_types=()))

api = Api()
window = webview.create_window('Test', html='<button onclick="pywebview.api.open_file_dialog()">open dialog</button>', js_api=api)
webview.start()

Are we doing something wrong when sending the request to the other process?

async def _request(self, *args: Any, **kwargs: Any) -> Any:
def wrapper(*args: Any, **kwargs: Any) -> Any:
try:
method_queue.put((name, args, kwargs))
return response_queue.get() # wait for the method to be called and writing its result to the queue
except Exception:
log.exception(f'error in {name}')
return None
name = inspect.currentframe().f_back.f_code.co_name # type: ignore
return await run.io_bound(wrapper, *args, **kwargs)

Should we set block or timeout differently when calling get()? I don't have my dev environment on this Linux machine, so it's a bit cumbersome to play around with the source code. Can anyone step in?

@JensOgorek
Copy link
Contributor

@falkoschindler I changed the code of native.py, but it seems it already get stuck at method_queue.put((name, args, kwargs)) for me.

@gsal
Copy link

gsal commented May 23, 2024

I am trying nicegui and native=True, but things are not working for me.

If I try the code at the top from the OP:

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)

The final error message is as follows:

File "/home/gsal/.conda/envs/py311nice/lib/python3.11/multiprocessing/synchronize.py", line 107, in __getstate__
    raise RuntimeError('A SemLock created in a fork context is being '
RuntimeError: A SemLock created in a fork context is being shared with a process in a spawn context. This is not supported. Please use the same context to create multiprocessing objects and Process.

I am in Rocky9 and using a conda environment that I created (in the last 48 hours) with the latest nicegui

@falkoschindler
Copy link
Contributor

pywebview 5.3 just got released. Can anyone check if the problem persists?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

6 participants