0
votes
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)

Here is the error:

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.'
This looks more like threading problem than streamlitPrakash Dahal
I have run your code in pycharm and it runs fine wihout any problem.Prakash Dahal
Did you run on terminal/prompt/shell: streamlit run main.py ? Where 'main.py' is you source code filename? Your code is totally correct!Humberto Moura
Looks like this was an Issue with an older version of streamlit: github.com/streamlit/streamlit/issues/744YScharf