Giter Club home page Giter Club logo

Comments (8)

rodja avatar rodja commented on May 13, 2024 1

NiceGUI is using uvicorn as a server which has still problems with graceful shutdowns (see encode/uvicorn#1103 for example). We need to think about this a bit. My not so graceful suggestion would be to start NiceGUI without reload=True, launch a new process on exit and just live with exceptions printed on the console:

from nicegui import ui
import sys

def apply():
    print('Starting main system in seperate process')
    # subprocess.Popen(['python3', 'my_main_system.py'])
    sys.exit(0)


ui.label('Configuration')
ui.label('... your config UI here ...')
ui.button('Apply', on_click=apply)

ui.run(reload=False)

from nicegui.

canDry avatar canDry commented on May 13, 2024 1

ui.shutdown

Correction: app.shutdown

from nicegui.

falkoschindler avatar falkoschindler commented on May 13, 2024

Yes, it would be great to have something like

from nicegui import ui

ui.label('Your configurtion UI...')

def quit():
    ... # How?

ui.button('Quit', on_click=quit)

ui.run(reload=False)

print('Do something else...')

It's a pity that uvicorn.run() starts a server that's not exposed to the outside, but would have a server.shutdown() method.

Monkey-patching uvicorn.run() (or simply copying its content over into our run.py) would be an option. But due to the many configuration options thats more than 120 lines of code and doesn't feel quite right.

from nicegui.

rodja avatar rodja commented on May 13, 2024

The solution multiprocessing.Process-Solution described in https://stackoverflow.com/a/70980373/364388 looked promising. But the whole app needs to be pickled... which does not work in NiceGUI:

AttributeError: Can't pickle local object 'CustomView.use.<locals>.<lambda>'

I've pushed the code to branch #83-Process.

from nicegui.

rodja avatar rodja commented on May 13, 2024

I've got a first idea working on #83-ui.shutdown.
The main idea stems from @falkoschindler #83 (comment). We could simply create the uvicorn server object inside nicegui instead of relying on the uvicorn.run helper. The API may look as following:

ui.button('Quit', on_click=ui.shutdown)

But there is still work to be done. For example auto-reloading is still broken.

from nicegui.

falkoschindler avatar falkoschindler commented on May 13, 2024

I tried to add more code from uvicorn.run: 501323d

Now the app starts with reload=False and reload=True. But with auto-reload the "Quit" button does not work, because the spawned process has no nicegui.globals.server set. That's only the case in the parent process. But maybe we don't need reloading anyway. We could simply raise an exception that ui.shutdown does not work with reload=True.

Another issue: When the socket connection is lost due to the server being shut down, the JustPy frontend asks for confirmation to reload the page. This makes no sense and should be avoided.

from nicegui.

falkoschindler avatar falkoschindler commented on May 13, 2024

Nice! After moving ui.shutdown into the lifecycle module the reload-confirmation dialog is gone.

from nicegui.

rodja avatar rodja commented on May 13, 2024

The ui.shutdown() method is now available in v0.8.16

from nicegui.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.