Zuletzt aktiv 10 months ago

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

timmy hat die Gist bearbeitet 10 months ago. Zu Änderung gehen

Keine Änderungen

timmy hat die Gist bearbeitet 11 months ago. Zu Änderung gehen

Keine Änderungen

timmy hat die Gist bearbeitet 2 years ago. Zu Änderung gehen

1 file changed, 29 insertions

streamlit_authentication_example.py(Datei erstellt)

@@ -0,0 +1,29 @@
1 + import streamlit as st # 匯入 streamlit 模組
2 + import streamlit_authenticator as stauth # 匯入 streamlit_authenticator 模組
3 +
4 + names = ["John", "Mary"] # 定義名字清單
5 + usernames = ["john", "mary"] # 定義使用者名清單
6 + passwords = ["1234", "5678"] # 定義密碼清單
7 +
8 + hashed_passwords = stauth.Hasher(passwords).generate() # 使用 Hasher 類別對密碼進行雜湊處理
9 +
10 + authenticator = 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 +
19 + name, authentication_status = authenticator.login('Login', 'main') # 進行登入驗證,並顯示 "Login" 按鈕,連接到 "main"
20 +
21 + if authentication_status == False: # 如果驗證狀態為假
22 + st.error('Username/password is incorrect') # 顯示錯誤訊息,提示使用者帳號或密碼不正確
23 + elif authentication_status == None: # 如果驗證狀態為空
24 + st.warning('Please enter your username and password') # 顯示警告訊息,提示使用者輸入帳號和密碼
25 + else:
26 + st.write('Welcome *%s*' % (name)) # 顯示歡迎訊息,使用 name 變數的值
27 +
28 + with st.sidebar: # 在側邊欄中
29 + authenticator.logout('Logout', 'main') # 顯示 "Logout" 按鈕,連接到 "main"
Neuer Älter