运行时错误:线程“ScriptRunner.scriptThread”中没有当前事件循环

问题描述 投票:0回答:3
import streamlit as st
import pandas as pd
from sklearn import datasets
from sklearn.ensemble import RandomForestClassifier

st.write("""
# Simple Iris Flower Prediction App
This app predicts the **Iris flower** type!
""")

st.sidebar.header('User Input Parameters')

def user_input_features():
    sepal_length = st.sidebar.slider('Sepal length', 4.3, 7.9, 5.4)
    sepal_width = st.sidebar.slider('Sepal width', 2.0, 4.4, 3.4)
    petal_length = st.sidebar.slider('Petal length', 1.0, 6.9, 1.3)
    petal_width = st.sidebar.slider('Petal width', 0.1, 2.5, 0.2)
    data = {'sepal_length': sepal_length,
            'sepal_width': sepal_width,
            'petal_length': petal_length,
            'petal_width': petal_width}
    features = pd.DataFrame(data, index=[0])
    return features

df = user_input_features()

st.subheader('User Input parameters')
st.write(df)

iris = datasets.load_iris()
X = iris.data
Y = iris.target

clf = RandomForestClassifier()
clf.fit(X, Y)

prediction = clf.predict(df)
prediction_proba = clf.predict_proba(df)

st.subheader('Class labels and their corresponding index number')
st.write(iris.target_names)

st.subheader('Prediction')
st.write(iris.target_names[prediction])
#st.write(prediction)

st.subheader('Prediction Probability')
st.write(prediction_proba)

这是错误:

RuntimeError: There is no current event loop in thread 'ScriptRunner.scriptThread'.
Traceback:
File "c:\users\hp\anaconda3\anaconda\lib\site-packages\streamlit\script_runner.py", line 333, in _run_script
    exec(code, module.__dict__)
File "C:\Users\HP\anaconda3\Anaconda\lib\site-packages\ipykernel_launcher.py", line 16, in <module>
    app.launch_new_instance()
File "C:\Users\HP\anaconda3\Anaconda\lib\site-packages\traitlets\config\application.py", line 844, in launch_instance
    app.initialize(argv)
File "C:\Users\HP\anaconda3\Anaconda\lib\site-packages\traitlets\config\application.py", line 87, in inner
    return method(app, *args, **kwargs)
File "C:\Users\HP\anaconda3\Anaconda\lib\site-packages\ipykernel\kernelapp.py", line 582, in initialize
    self.init_kernel()
File "C:\Users\HP\anaconda3\Anaconda\lib\site-packages\ipykernel\kernelapp.py", line 439, in init_kernel
    shell_stream = ZMQStream(self.shell_socket)
File "C:\Users\HP\anaconda3\Anaconda\lib\site-packages\zmq\eventloop\zmqstream.py", line 115, in __init__
    self.io_loop = io_loop or IOLoop.current()
File "C:\Users\HP\anaconda3\Anaconda\lib\site-packages\zmq\eventloop\ioloop.py", line 116, in current
    loop = ioloop.IOLoop.current(*args, **kwargs)
File "c:\users\hp\anaconda3\anaconda\lib\site-packages\tornado\ioloop.py", line 265, in current
    loop = asyncio.get_event_loop()
File "c:\users\hp\anaconda3\anaconda\lib\asyncio\events.py", line 639, in get_event_loop
    raise RuntimeError('There is no current event loop in thread %r.'
python streamlit
3个回答
1
投票

尝试:

import asyncio

def get_or_create_eventloop():
    try:
        return 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)
            return asyncio.get_event_loop()

loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)

1
投票

假设您将脚本编写为 Jupyter 笔记本,并在 Jupyter 环境中执行它。结果是一个包含以下内容的警告框:

Warning: to view this Streamlit app on a browser, run it with the following command:

streamlit run /Users/TG/opt/anaconda3/lib/python3.8/site-packages/ipykernel_launcher.py [ARGUMENTS]

复制此命令并将其粘贴到命令行非常诱人。

但问题是这个命令不起作用。相反,从命令行执行它会引发您遇到的错误。

补救方法是这样的:

  • 将 Jupyter 笔记本导出为可执行脚本(使用
    .py
    作为文件扩展名),例如名字是
    myscript.py
  • 使用
    streamlit run myscript.py
    从命令行运行此脚本(切换到脚本所在的目录后)。

0
投票

使用python版本3.10和最新版本的streamlit

© www.soinside.com 2019 - 2024. All rights reserved.