Son aktivite 10 months ago

這段程式碼使用 streamlit_authenticator 建立一個簡單的登入系統,支持使用者驗證(帳號、密碼)和登出功能,並在側邊欄提供登出按鈕。密碼經過雜湊處理以增加安全性。

Revizyon 672c646e19e064233c82ea406133a01087849d99

streamlit_authentication_example.py Ham
1import streamlit as st # 匯入 streamlit 模組
2import streamlit_authenticator as stauth # 匯入 streamlit_authenticator 模組
3
4names = ["John", "Mary"] # 定義名字清單
5usernames = ["john", "mary"] # 定義使用者名清單
6passwords = ["1234", "5678"] # 定義密碼清單
7
8hashed_passwords = stauth.Hasher(passwords).generate() # 使用 Hasher 類別對密碼進行雜湊處理
9
10authenticator = stauth.Authenticate( # 建立一個 stauth.Authenticate 的實例 authenticator
11 names, # 使用 names 清單
12 usernames, # 使用 usernames 清單
13 hashed_passwords, # 使用雜湊後的密碼清單
14 'some_cookie_name', # 設定 cookie 名稱為 'some_cookie_name'
15 'some_signature_key', # 設定簽名金鑰為 'some_signature_key'
16 cookie_expiry_days=30 # 設定 cookie 有效天數為 30 天
17)
18
19name, authentication_status = authenticator.login('Login', 'main') # 進行登入驗證,並顯示 "Login" 按鈕,連接到 "main"
20
21if authentication_status == False: # 如果驗證狀態為假
22 st.error('Username/password is incorrect') # 顯示錯誤訊息,提示使用者帳號或密碼不正確
23elif authentication_status == None: # 如果驗證狀態為空
24 st.warning('Please enter your username and password') # 顯示警告訊息,提示使用者輸入帳號和密碼
25else:
26 st.write('Welcome *%s*' % (name)) # 顯示歡迎訊息,使用 name 變數的值
27
28 with st.sidebar: # 在側邊欄中
29 authenticator.logout('Logout', 'main') # 顯示 "Logout" 按鈕,連接到 "main"