简体   繁体   中英

Running a justpy web app on Jupyter returns RuntimeError

I am trying to run justpy web apps such as this one down here on Jupyter:

import justpy as jp

def hello_world():
    wp = jp.WebPage()
    d = jp.Div(text='Hello world!')
    wp.add(d)
    return wp

jp.justpy(hello_world)

but that always generates an error no matter if I run it on Jupyter Notebook or Jupyter Lab:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-2-a5b426517caf> in <module>
      7     return wp
      8 
----> 9 jp.justpy(hello_world)

/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/justpy/justpy.py in justpy(func, start_server, websockets, host, port, startup, **kwargs)
    381                         ssl_keyfile=SSL_KEYFILE, ssl_certfile=SSL_CERTFILE, ssl_version=SSL_VERSION)
    382         else:
--> 383             uvicorn.run(app, host=host, port=port, log_level=UVICORN_LOGGING_LEVEL)
    384 
    385     return func_to_run

/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/uvicorn/main.py in run(app, **kwargs)
    389         supervisor.run()
    390     else:
--> 391         server.run()
    392 
    393 

/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/uvicorn/main.py in run(self, sockets)
    417         self.config.setup_event_loop()
    418         loop = asyncio.get_event_loop()
--> 419         loop.run_until_complete(self.serve(sockets=sockets))
    420 
    421     async def serve(self, sockets=None):

/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/asyncio/base_events.py in run_until_complete(self, future)
    590         """
    591         self._check_closed()
--> 592         self._check_running()
    593 
    594         new_task = not futures.isfuture(future)

/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/asyncio/base_events.py in _check_running(self)
    550     def _check_running(self):
    551         if self.is_running():
--> 552             raise RuntimeError('This event loop is already running')
    553         if events._get_running_loop() is not None:
    554             raise RuntimeError(

RuntimeError: This event loop is already running

Does anyone have a solution to run justpy apps on Jupyter?

Add this at the beginning of your notebook

import nest_asyncio
nest_asyncio.apply()

It's because the ipython kernel itself runs on an event loop,
and to quote from nest_asyncio documentation:

By design asyncio does not allow its event loop to be nested. This presents a practical problem: When in an environment where the event loop is already running it's impossible to run tasks and wait for the result. Trying to do so will give the error "RuntimeError: This event loop is already running".

install nest-asyncio

pip install nest-asyncio

after that adding below lines

import nest_asyncio
nest_asyncio.apply()
__import__('IPython').embed()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM