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

Fails running with Visual Studio Code Debugger on Windows #2

Closed
DeviceNut opened this issue Jul 15, 2021 · 10 comments
Closed

Fails running with Visual Studio Code Debugger on Windows #2

DeviceNut opened this issue Jul 15, 2021 · 10 comments

Comments

@DeviceNut
Copy link

Gives exception on import:

"Could not find ui.run() command"

But runs ok from command line.

This is on a Windows 10 Pro system.

@rodja
Copy link
Member

rodja commented Jul 15, 2021

Maybe you've set another python version as default interpreter within vscode? Could you try running the following script

from nicegui import ui
import nicegui
import sys

print(sys.version)
print(nicegui.__file__)

ui.run()

and provide to output when running from command line vs vscode?

@falkoschindler
Copy link
Contributor

Please note, that from version 0.4.0 NiceGUI requires a ui.run() command in the main script. It should be located at "top level" and not nested in a class, function, condition or loop (thus not indented). So if VSCode runs with a different Python installation with a newer NiceGUI version, it would explain the different behavior.

@DeviceNut
Copy link
Author

DeviceNut commented Jul 15, 2021

It fails on the import statement itself (either from nicegui import ui, or just import nicegui), only when running with the debugger. Running without the debugger, or from the command line, works.

Here's the output when running to the above snippet:

3.9.6 (tags/v3.9.6:db3ff76, Jun 28 2021, 15:26:21) [MSC v.1929 64 bit (AMD64)]
C:\Python\lib\site-packages\nicegui\__init__.py

So it's something to do with VSCode's debugger, but I've never seen this happen with anything else.

@falkoschindler
Copy link
Contributor

Although it might be possible that the VSCode debugger conflicts with NiceGUI's internal reload detection mechanism, I'm struggling to reproduce the problem. When starting something like

from nicegui import ui

def click():
    print('click', flush=True)

ui.label('Hello world!')
ui.button('Click me', on_click=click)

ui.run()

with "Run">"Start Debugging">"Python File: Debug the currently active Python file", the Uvicorn server starts nicely, the browser opens a new tab on localhost:80 and I can set breakpoints (e.g. in the event handler of a simple button).

Maybe VSCode runs the debugger differently on Windows than on Mac?

@falkoschindler
Copy link
Contributor

It fails on the import statement itself

An error in the import statement might have to do with the ui.run() command: NiceGUI needs to decide whether to immediately start a Uvicorn server with auto-reload activated or to continue evaluating the main script and to start the server later during ui.run(). It does so by inspecting the main script if ui.run() will be called with reload=True. If this approach causes issues in certain development environments, we're eager to resolve the issue.

@DeviceNut
Copy link
Author

Sounds like that is causing the issue on my setup. Curious it works fine on yours. Perhaps it's a side effect of some extensions I've got loaded (a bunch of stuff for python/javascript/npm/react/etc.)?

And like I said, it works fine without "debugging". I'm not a VSCode guru so I don't know what else to do here.

You might want to see if anyone else can repro this, or it's just me.

@DeviceNut
Copy link
Author

Right, so it's your code at config.py:41 that is causing this. Why it's not finding the ui.run() call in the main source I don't know.

Also, it's opening 0.0.0.0 not localhost, which doesn't work. If I redirect to localhost it connects.

@falkoschindler
Copy link
Contributor

I suspect that the debugger calls your main script with some wrapper and NiceGUI is looking for ui.run() in the wrong file. There are two things that would be of great help:

  1. You could add a print(filepath, flush=True) before config.py:41 to check which file is analyzed.
  2. If it's the wrong one, you could add print(f.filename) in config.py:19 to show all the files in the call stack.

Maybe we need to add a new exception in endings... or find a better way to search for the main file.


And thanks for the hint about 0.0.0.0 vs. localhost. Although the server is starting with IP 0.0.0.0, the browser should open localhost.

@DeviceNut
Copy link
Author

Here's the problem: Windows uses forward slashes, so your list of filenames to exclude needs to include names using them as well. Made that change and it works.

@falkoschindler
Copy link
Contributor

Awesome! We totally forgot to use platform-independent file paths. I fixed it in Release 0.4.7. It also addresses the issue with Host 0.0.0.0.

Thanks @DeviceNut for your contribution!

@falkoschindler falkoschindler changed the title Fails running within Visual Code Fails running with Visual Studio Code Debugger on Windows Jul 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants