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

Startup fails when using if __name__ == "__main__": #252

Closed
Quadrum1 opened this issue Jan 17, 2023 · 3 comments
Closed

Startup fails when using if __name__ == "__main__": #252

Quadrum1 opened this issue Jan 17, 2023 · 3 comments

Comments

@Quadrum1
Copy link

The following code runs fine:

from nicegui import ui
ui.label('some label')
ui.run()

However, the two following code-snippets do not run:

from nicegui import ui
if __name__ == "__main__":
	ui.label('some label')
	ui.run()
if __name__ == "__main__":
	from nicegui import ui
	ui.label('some label')
	ui.run()

In both cases, the raised error looks like the following.

←[31mERROR←[0m:    Traceback (most recent call last):
  File "F:\Python\lib\site-packages\starlette\routing.py", line 671, in lifespan

    async with self.lifespan_context(app):
  File "F:\Python\lib\site-packages\starlette\routing.py", line 566, in __aenter__
    await self._router.startup()
  File "F:\Python\lib\site-packages\starlette\routing.py", line 650, in startup
    handler()
  File "F:\Python\lib\site-packages\nicegui\nicegui.py", line 59, in handle_startup
    print(f'NiceGUI ready to go on http://{globals.host}:{globals.port}')
AttributeError: module 'nicegui.globals' has no attribute 'host'

←[31mERROR←[0m:    Application startup failed. Exiting.
Task raised an exception
Traceback (most recent call last):
  File "F:\Python\lib\site-packages\nicegui\background_tasks.py", line 57, in _handle_task_result
    task.result()
  File "F:\Python\lib\site-packages\nicegui\binding.py", line 27, in loop
    await asyncio.sleep(globals.binding_refresh_interval)
AttributeError: module 'nicegui.globals' has no attribute 'binding_refresh_interval'
@solzard
Copy link

solzard commented Jan 17, 2023

Related: #181

# workaround
from nicegui import ui

if __name__ in {"__main__", "__mp_main__"}:
    ui.label("some label")
    ui.run()

@Quadrum1
Copy link
Author

Didn't spot that one, thanks for mentioning it!

@rodja
Copy link
Member

rodja commented Jan 18, 2023

I'll close this issue. If someone has an idea on how to improve the error message a pull request would be welcome.

@rodja rodja closed this as completed Jan 18, 2023
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