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

RuntimeError: "There is no current event loop" when running streamlit app with stpyvista #17

Closed
mhamidasn opened this issue Jan 26, 2024 · 4 comments

Comments

@mhamidasn
Copy link

I am encountering a RuntimeError when attempting to run a Streamlit app that utilizes stpyvista for visualization. I run the example stpyvista code, which is this one:

import streamlit as st
import pyvista as pv
from stpyvista import stpyvista

## Initialize a plotter object
plotter = pv.Plotter(window_size=[400,400])

## Create a mesh with a cube 
mesh = pv.Cube(center=(0,0,0))

## Add some scalar field associated to the mesh
mesh['myscalar'] = mesh.points[:, 2]*mesh.points[:, 0]

## Add mesh to the plotter
plotter.add_mesh(mesh, scalars='myscalar', cmap='bwr', line_width=1)

## Final touches
plotter.view_isometric()
plotter.add_scalar_bar()
plotter.background_color = 'white'

## Pass a key to avoid re-rendering at each time something changes in the page
stpyvista(plotter, key="pv_cube")

However, when I did run streamlit app.py, I encounter the following error:

RuntimeError: There is no current event loop in thread 'ScriptRunner.scriptThread'.
Traceback:
File "D:\Python\Python39\lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 535, in _run_script
    exec(code, module.__dict__)
File "C:\Users\hamid\Downloads\app.py", line 3, in <module>
    from stpyvista import stpyvista
File "D:\Python\Python39\lib\site-packages\stpyvista\__init__.py", line 8, in <module>
    import panel as pn
File "D:\Python\Python39\lib\site-packages\panel\__init__.py", line 50, in <module>
    from . import chat  # noqa
File "D:\Python\Python39\lib\site-packages\panel\chat\__init__.py", line 33, in <module>
    from .feed import ChatFeed  # noqa
File "D:\Python\Python39\lib\site-packages\panel\chat\feed.py", line 25, in <module>
    from ..io.resources import CDN_DIST
File "D:\Python\Python39\lib\site-packages\panel\io\__init__.py", line 9, in <module>
    from .document import (  # noqa
File "D:\Python\Python39\lib\site-packages\panel\io\document.py", line 47, in <module>
    WRITE_LOCK = asyncio.Lock()
File "D:\Python\Python39\lib\asyncio\locks.py", line 81, in __init__
    self._loop = events.get_event_loop()
File "D:\Python\Python39\lib\asyncio\events.py", line 642, in get_event_loop
    raise RuntimeError('There is no current event loop in thread %r.'

Any guidance on resolving this issue would be greatly appreciated :)

Additional Information:

  • python version: 3.9.13
  • pyvista version: 0.42.0
  • Streamlit version: 1.30.0
  • stpyvista version: 0.0.14
@edsaac
Copy link
Owner

edsaac commented Jan 26, 2024

What version of panel is installed in your environment?

@mhamidasn
Copy link
Author

mhamidasn commented Jan 26, 2024

``The version of panel installed in my environment is 1.3.8.

However, after several attempts, I decided to downgrade the stpyvista version to see if it resolves the issue. After several downgrades, I found that the version that does not return any error is stpyvista version 0.0.11.

(+) after i add this to my code:

import asyncio

try:
    loop = asyncio.get_event_loop()
except RuntimeError as ex:
    if "There is no current event loop in thread" in str(ex):
        loop = asyncio.new_event_loop()
        asyncio.set_event_loop(loop)
    else:
        raise

@edsaac
Copy link
Owner

edsaac commented Jan 26, 2024

👍 I was able to replicate your issue only if running python 3.9. No exception is raised if running python 3.10 or 3.11. I'll check what exactly is going wrong...

@edsaac
Copy link
Owner

edsaac commented Mar 29, 2024

It seems this issue has been solved as the Runtime Error is not raised anymore with the latest version of panel.

Tested with:

  • Python 3.9.18
  • panel 1.4.0
  • pyvista 0.43.4
  • stpyvista 0.0.16

I'm closing this issue but feel free to reopen if I missed something.

@edsaac edsaac closed this as completed Mar 29, 2024
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

2 participants