config.yaml
· 455 B · YAML
Raw
credentials:
usernames:
user1:
email: user1@example.com
name: User One
password: $2b$12$RVn2UdS.KJiM24xT3Vb9fOIEv4S3hvP6Pjw2xzaP5d/fDq1U2pX2e
user2:
email: user2@example.com
name: User Two
password: $2b$12$C4E7p9kXQKoM3W8Jp5s1zOtk0D2q7T2zJtOjVg22uW/1sLp7B4sGW
cookie:
expiry_days: 30
key: some_signature_key
name: my_auth_cookie
preauthorized:
emails:
- admin@example.com
- manager@example.com
| 1 | credentials: |
| 2 | usernames: |
| 3 | user1: |
| 4 | email: user1@example.com |
| 5 | name: User One |
| 6 | password: $2b$12$RVn2UdS.KJiM24xT3Vb9fOIEv4S3hvP6Pjw2xzaP5d/fDq1U2pX2e |
| 7 | user2: |
| 8 | email: user2@example.com |
| 9 | name: User Two |
| 10 | password: $2b$12$C4E7p9kXQKoM3W8Jp5s1zOtk0D2q7T2zJtOjVg22uW/1sLp7B4sGW |
| 11 | cookie: |
| 12 | expiry_days: 30 |
| 13 | key: some_signature_key |
| 14 | name: my_auth_cookie |
| 15 | preauthorized: |
| 16 | emails: |
| 17 | - admin@example.com |
| 18 | - manager@example.com |
streamlit_authentication_with_yaml.py
· 2.4 KiB · Python
Raw
import streamlit as st # 匯入 streamlit 模組
import streamlit_authenticator as stauth # 匯入 streamlit_authenticator 模組
import yaml # 匯入 yaml 模組
from yaml.loader import SafeLoader # 從 yaml.loader 匯入 SafeLoader
with open("./config.yaml") as file: # 開啟名為 config.yaml 的檔案
config = yaml.load(file, Loader=SafeLoader) # 使用 SafeLoader 從檔案讀取配置資訊並存入 config 變數中
authenticator = stauth.Authenticate( # 建立一個 stauth.Authenticate 的實例 authenticator
config["credentials"], # 使用配置中的 credentials 屬性
config["cookie"]["name"], # 使用配置中的 cookie 的 name 屬性
config["cookie"]["key"], # 使用配置中的 cookie 的 key 屬性
config["cookie"]["expiry_days"], # 使用配置中的 cookie 的 expiry_days 屬性
config["preauthorized"], # 使用配置中的 preauthorized 屬性
)
authenticator.login("Login", "main") # 進行登入驗證,並顯示 "Login" 按鈕,連接到 "main"
if st.session_state["authentication_status"]: # 如果驗證狀態為真
authenticator.logout("Logout", "main", key="unique_key") # 顯示 "Logout" 按鈕,連接到 "main",並使用特定的 key
st.write(f'Welcome *{st.session_state["name"]}*') # 顯示歡迎訊息,使用 st.session_state 中的 name 屬性
st.title("Some content") # 顯示標題為 "Some content"
elif st.session_state["authentication_status"] is False: # 如果驗證狀態為假
st.error("Username/password is incorrect") # 顯示錯誤訊息,提示使用者帳號或密碼不正確
elif st.session_state["authentication_status"] is None: # 如果驗證狀態為空
st.warning("Please enter your username and password") # 顯示警告訊息,提示使用者輸入帳號和密碼
if st.session_state["authentication_status"]: # 如果驗證狀態為真
try:
if authenticator.reset_password(st.session_state["username"], "Reset password"): # 嘗試重設密碼,並顯示 "Reset password" 按鈕
st.success("Password modified successfully") # 顯示成功訊息,提示密碼修改成功
except Exception as e: # 處理可能的例外狀況
st.error(e) # 顯示錯誤訊息,提示發生了例外狀況
with open("./config.yaml", "w") as file: # 開啟名為 config.yaml 的檔案,以寫入模式
yaml.dump(config, file, default_flow_style=False) # 將配置資訊寫入檔案,不使用預設流風格
| 1 | import streamlit as st # 匯入 streamlit 模組 |
| 2 | import streamlit_authenticator as stauth # 匯入 streamlit_authenticator 模組 |
| 3 | import yaml # 匯入 yaml 模組 |
| 4 | from yaml.loader import SafeLoader # 從 yaml.loader 匯入 SafeLoader |
| 5 | |
| 6 | with open("./config.yaml") as file: # 開啟名為 config.yaml 的檔案 |
| 7 | config = yaml.load(file, Loader=SafeLoader) # 使用 SafeLoader 從檔案讀取配置資訊並存入 config 變數中 |
| 8 | |
| 9 | authenticator = stauth.Authenticate( # 建立一個 stauth.Authenticate 的實例 authenticator |
| 10 | config["credentials"], # 使用配置中的 credentials 屬性 |
| 11 | config["cookie"]["name"], # 使用配置中的 cookie 的 name 屬性 |
| 12 | config["cookie"]["key"], # 使用配置中的 cookie 的 key 屬性 |
| 13 | config["cookie"]["expiry_days"], # 使用配置中的 cookie 的 expiry_days 屬性 |
| 14 | config["preauthorized"], # 使用配置中的 preauthorized 屬性 |
| 15 | ) |
| 16 | |
| 17 | authenticator.login("Login", "main") # 進行登入驗證,並顯示 "Login" 按鈕,連接到 "main" |
| 18 | |
| 19 | if st.session_state["authentication_status"]: # 如果驗證狀態為真 |
| 20 | authenticator.logout("Logout", "main", key="unique_key") # 顯示 "Logout" 按鈕,連接到 "main",並使用特定的 key |
| 21 | st.write(f'Welcome *{st.session_state["name"]}*') # 顯示歡迎訊息,使用 st.session_state 中的 name 屬性 |
| 22 | st.title("Some content") # 顯示標題為 "Some content" |
| 23 | elif st.session_state["authentication_status"] is False: # 如果驗證狀態為假 |
| 24 | st.error("Username/password is incorrect") # 顯示錯誤訊息,提示使用者帳號或密碼不正確 |
| 25 | elif st.session_state["authentication_status"] is None: # 如果驗證狀態為空 |
| 26 | st.warning("Please enter your username and password") # 顯示警告訊息,提示使用者輸入帳號和密碼 |
| 27 | |
| 28 | if st.session_state["authentication_status"]: # 如果驗證狀態為真 |
| 29 | try: |
| 30 | if authenticator.reset_password(st.session_state["username"], "Reset password"): # 嘗試重設密碼,並顯示 "Reset password" 按鈕 |
| 31 | st.success("Password modified successfully") # 顯示成功訊息,提示密碼修改成功 |
| 32 | except Exception as e: # 處理可能的例外狀況 |
| 33 | st.error(e) # 顯示錯誤訊息,提示發生了例外狀況 |
| 34 | |
| 35 | with open("./config.yaml", "w") as file: # 開啟名為 config.yaml 的檔案,以寫入模式 |
| 36 | yaml.dump(config, file, default_flow_style=False) # 將配置資訊寫入檔案,不使用預設流風格 |
| 37 |