streamlit初步尝试

 参考:

https://share.streamlit.io/daniellewisdl/streamlit-cheat-sheet/app.py

https://docs.streamlit.io/en/stable/api.html?highlight=FORM#streamlit.form

https://docs.streamlit.io/en/stable/session_state_api.html#use-callbacks-to-update-session-state

https://docs.streamlit.io/en/stable/session_state_api.html#session-state-and-widget-state-association 

import streamlit as st

a = st.sidebar.radio('R:',[1,2])

st.button('Hit me')
st.checkbox('Check me out')
st.radio('Radio', [1,2,3])
st.selectbox('Select', [1,2,3])
st.multiselect('Multiselect', [1,2,3])
st.slider('Slide me', min_value=0, max_value=10)
st.select_slider('Slide to select', options=[1,'2'])
st.text_input('Enter some text')
st.number_input('Enter a number')
st.text_area('Area for textual entry')
st.date_input('Date input')
st.time_input('Time entry')
st.file_uploader('File uploader')
st.color_picker('Pick a color')



st.text('Fixed width text')
st.markdown('_Markdown_') # see *
st.latex(r''' e^{i\pi} + 1 = 0 ''')
st.write('Most objects') # df, err, func, keras!
st.write(['st', 'is <', 3]) # see *
st.title('My title')
st.header('My header')
st.subheader('My sub')
st.code('for i in range(8): foo()')

x = st.slider('Select a value')
st.write(x, 'squared is', x * x)



import time
st.progress(1)
st.progress(100)
st.spinner()
with st.spinner(text='In progress'):
    time.sleep(5)
    st.success('Done')
st.balloons()
st.error('Error message')
st.warning('Warning message')
st.info('Info message')
st.success('Success message')
#st.exception("error")



for i in range(int(st.number_input('Num:'))): 
    st.text('Fixed width text')
if st.sidebar.selectbox('I:',['f']) == 'f':
    st.text('Fixed width text')
my_slider_val = st.slider('Quinn Mallory', 1, 88)
st.write(my_slider_val)


col1, col2 = st.beta_columns(2)
col1.subheader('Columnisation')
st.beta_expander('Expander')

with st.beta_expander('Expand'):
    st.write('Juicy deets')

with st.echo():
    st.write('Code will be executed and printed')

#st.image('./img_00001.png')
#st.audio("https://www.youtube.com/watch?v=kgJMaP4msTs")
#st.video("https://www.youtube.com/watch?v=kgJMaP4msTs")

并列和python通信

import streamlit as st

#https://docs.streamlit.io/en/stable/session_state_api.html#session-state-and-widget-state-association
#https://docs.streamlit.io/en/stable/session_state_api.html#use-callbacks-to-update-session-state
#https://docs.streamlit.io/en/stable/api.html?highlight=FORM#streamlit.form

def form_callback():
    st.write(st.session_state.my_slider)
    st.write(st.session_state.my_checkbox)

with st.form(key='my_form'):
    slider_input = st.slider('My slider', 0, 10, 5, key='my_slider')
    checkbox_input = st.checkbox('Yes or No', key='my_checkbox')
    submit_button = st.form_submit_button(label='Submit', on_click=form_callback)



# python更新前端的值
cccc = 1.0
def form_callback2():
    st.session_state.thresh = cccc

with st.form(key='my_form2'):
    height_loosen_thresh = st.sidebar.number_input(label='height loosen thresh', #value=3.2,
                                                    min_value=1.0, max_value=30.0,
                                                    format='%f', key='thresh')
    submit_button = st.form_submit_button(label='Submit2', on_click=form_callback2)





col1, col2, col3 = st.beta_columns(3)
with col1:
    st.header("A cat")
    st.image("https://static.streamlit.io/examples/cat.jpg")
with col2:
    st.header("A dog")
    st.image("https://static.streamlit.io/examples/dog.jpg")
with col3:
    st.header("An owl")
    st.image("https://static.streamlit.io/examples/owl.jpg")



acol1, acol2, acol3 = st.sidebar.beta_columns(3)
with acol1:
    height_loosen_thresh1 = st.number_input(label='height', #value=3.2,
                                                    min_value=1.0, max_value=30.0,
                                                    format='%f', key='thresh1')
with acol2:
    height_loosen_thresh2 = st.number_input(label='height', #value=3.2,
                                                    min_value=1.0, max_value=30.0,
                                                    format='%f', key='thresh2')
with acol3:
    height_loosen_thresh3 = st.number_input(label='height', #value=3.2,
                                                    min_value=1.0, max_value=30.0,
                                                    format='%f', key='thresh3')


bcol1, bcol2, bcol3 = st.beta_columns(3)
with bcol1:
    height_loosen_thresh4 = st.number_input(label='height', #value=3.2,
                                                    min_value=1.0, max_value=30.0,
                                                    format='%f', key='thresh4')
with bcol2:
    height_loosen_thresh5 = st.number_input(label='height', #value=3.2,
                                                    min_value=1.0, max_value=30.0,
                                                    format='%f', key='thresh5')
with bcol3:
    height_loosen_thresh6 = st.number_input(label='height', #value=3.2,
                                                    min_value=1.0, max_value=30.0,
                                                    format='%f', key='thresh6')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Streamlit是一个用于构建数据科学和机器学习应用程序的Python库。它提供了一个简单易用的界面,可以快速创建交互式的Web应用程序。然而,Streamlit本身并不提供直接的登录界面功能,但你可以使用其他库或框架来实现登录功能,并将其与Streamlit集成在一起。 一种常见的做法是使用Flask作为后端框架来处理用户认证和登录逻辑。你可以使用Flask的登录扩展,如Flask-Login或Flask-User,来管理用户的登录状态和权限控制。这些扩展提供了一些方便的功能,如用户注册、登录、注销等。 在Streamlit应用程序中,你可以使用Flask的路由功能来定义不同的页面和视图函数。例如,你可以创建一个登录页面的路由,并在该路由上渲染一个HTML模板,包含用户名和密码输入框以及登录按钮。当用户点击登录按钮时,你可以通过Flask的认证机制验证用户输入的用户名和密码是否正确,并根据验证结果进行相应的处理。 以下是一个简单的示例代码,演示了如何在Streamlit应用程序中实现一个基本的登录界面: ```python import streamlit as st from flask import Flask, render_template, request from flask_login import LoginManager, UserMixin, login_user, logout_user, login_required # 创建Flask应用程序 app = Flask(__name__) app.secret_key = 'your_secret_key' # 创建LoginManager对象 login_manager = LoginManager() login_manager.init_app(app) # 创建一个简单的用户类 class User(UserMixin): def __init__(self, username): self.username = username # 用户认证回调函数 @login_manager.user_loader def load_user(user_id): # 这里可以根据user_id从数据库或其他存储中获取用户信息 return User(user_id) # 登录页面路由 @app.route('/login', methods=['GET', 'POST']) def login(): if request.method == 'POST': username = request.form['username'] password = request.form['password'] # 这里可以根据用户名和密码进行验证 if username == 'admin' and password == 'password': user = User(username) login_user(user) return '登录成功!' else: return '用户名或密码错误!' return render_template('login.html') # 保护页面的路由 @app.route('/protected') @login_required def protected(): return '这是一个受保护的页面,只有登录用户才能访问!' # 注销路由 @app.route('/logout') @login_required def logout(): logout_user() return '注销成功!' # Streamlit应用程序 def main(): st.title('登录示例') st.write('请访问以下链接进行登录:') st.write('[登录](http://localhost:5000/login)') st.write('[受保护页面](http://localhost:5000/protected)') st.write('[注销](http://localhost:5000/logout)') if __name__ == '__main__': main() ``` 在上述示例中,我们使用Flask-Login来处理用户认证和登录逻辑。在登录页面的路由中,我们通过`request.form`获取用户输入的用户名和密码,并进行验证。如果验证通过,我们创建一个`User`对象,并使用`login_user`函数将用户登录状态保存在会话中。在受保护页面的路由中,我们使用`@login_required`装饰器来限制只有登录用户才能访问该页面。在注销路由中,我们使用`logout_user`函数将用户的登录状态从会话中移除。 请注意,上述示例仅提供了一个基本的登录界面实现思路,并没有涉及到真实的数据库或密码加密等安全性考虑。在实际应用中,你需要根据具体需求和安全性要求进行适当的改进和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值