赞
踩
安装:pip install streamlit
查看是否安装成功:streamlit hello
如果安装报错:AttributeError: module ‘google.protobuf.descriptor‘ has no attribute ‘_internal_create_key‘ 则升级protobuf即可。 pip install --upgrade protobuf
step1:streamlit run ****.py
step2:浏览器中访问本地URL:localhost:8501
- import streamlit as st
- url = st.text_input('Enter URL')
- st.write('The Entered URL is', url)
- import streamlit as st
- x = st.slider('x')
- st.write(x, 'squared is', x * x)
隐藏或显示/隐藏程序中的特定区域,或者设置函数的布尔参数值。st.checkbox() 需要一个参数,即插件标签。在该应用程序中,复选框会用来切换条件语句。
- import streamlit as st
- import pandas as pd
- import numpy as np
-
- df = pd.read_csv("sample_submission.csv")
-
- if st.checkbox('Show dataframe'):
-
- st.write(df)
通过st.selectbox可以在一系列选项或列表中进行选择。常见的用法是将其作为下拉项然后从名单中挑选值。
- import streamlit as st
- import pandas as pd
- import numpy as np
-
- df = pd.read_csv("sample_submission.csv")
-
- option = st.selectbox('Which Club do you like best?',df['ID'].unique())
-
- 'You selected: ', option
也可以用下拉框内的多个值。这里讲的是使用 st.multiselect在变量选选中获取多个值作为列表。
- import streamlit as st
- import pandas as pd
- import numpy as np
-
-
- df = pd.read_csv("sample_submission.csv")
-
- options = st.multiselect('What are your favorite clubs?', df['ID'].unique())
-
- st.write('You selected:', options)
在Streamlit中通过st.cache装饰器函数实现缓存功能。用Streamlit的缓存装饰器标记函数时,无论这个函数是否执行,都会检查输入的参数值(由该函数处理的)。如果Streamlit之前没有处理过这些数据,它会调用函数并将运算结果存到本地缓存中。下次再调用函数时,倘若还是这些参数,Streamlit就会完全跳过这一块的函数执行,直接用缓存器里的结果数据。
- import streamlit as st
- import pandas as pd
- import numpy as np
- import plotly_express as px
- df = st.cache(pd.read_csv)("sample_submission.csv")
-
-
- @st.cache
- def complex_func(a,b):
- complex_func(a,b)
把插件移动到侧边栏内,比如像Rshiny仪表盘,只需在插件代码中添加 st.sidebar即可。
- import streamlit as st
- import pandas as pd
- import numpy as np
- import plotly_express as px
-
-
- @st.cache
- def complex_func(a,b):
- complex_func(a,b)
-
- df = st.cache(pd.read_csv)("sample_submission .csv")
-
- clubs = st.sidebar.multiselect('Show Player for clubs?', df['ID'].unique())
-
- nationalities = st.sidebar.multiselect('Show Player from Nationalities?', df['CLASS'].unique())
-
- new_df = df[(df['ID'].isin(clubs)) & (df['CLASS'].isin(nationalities))]
-
- st.write(new_df)
-
- fig = px.scatter(new_df, x ='ID',y='CLASS',color='ID')
-
- st.plotly_chart(fig)
在Streamlit程序中应用Markdown,最合适的就是调用Magic指令。通过该指令,用户做标记语言就会像写评论一样简单。用户也可以使用指令st.markdown。
- import streamlit as st
- import pandas as pd
- import numpy as np
- import plotly_express as px
-
- df = st.cache(pd.read_csv)("sample_submission.csv")
- clubs = st.sidebar.multiselect('Show Player for clubs?', df['ID'].unique())
-
- nationalities = st.sidebar.multiselect('Show Player from Nationalities?', df['CLASS'].unique())
-
- new_df = df[(df['ID'].isin(clubs)) & (df['CLASS'].isin(nationalities))]
-
- st.write(new_df)
-
- fig = px.scatter(new_df, x ='ID',y='CLASS',color='ID')
-
- st.plotly_chart(fig)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。