timmy / 使用 Streamlit 建立 YAML 設定檔編輯器
0 likes
0 forks
2 files
Last active 10 months ago
此程式使用 Streamlit 讀取、編輯並存儲 YAML 設定檔,透過 pandas 轉換為表格格式,提供直覺化 UI 介面,適用於設定管理與資料修改。
| 1 | age: 30 |
| 2 | is_active: true |
| 3 | name: John Doe |
timmy / 使用 Streamlit 建立線上訂餐系統
0 likes
0 forks
1 files
Last active 10 months ago
這是一個使用 Streamlit 構建的訂餐系統,包含訂購人選擇、餐廳菜單顯示、主餐及加點品項的選擇與數量輸入,並計算和顯示訂單詳情與總價。
| 1 | from datetime import date |
| 2 | |
| 3 | import streamlit as st |
| 4 | |
| 5 | # 定義 Customer 類別 |
| 6 | class Customer: |
| 7 | def __init__(self, name, email, preferences): |
| 8 | self.name = name |
| 9 | self.email = email |
| 10 | self.preferences = preferences |
timmy / 使用 Streamlit 建立文件內容查看器
0 likes
0 forks
1 files
Last active 10 months ago
此程式使用 Streamlit 讓使用者上傳文字或 Markdown 檔案,顯示其內容,並透過暫存檔處理,可應用於文件檢視與內容處理工具。
| 1 | import streamlit as st |
| 2 | import os |
| 3 | |
| 4 | # 標題 |
| 5 | st.title("文件內容查看器") |
| 6 | |
| 7 | # 文件上傳 |
| 8 | uploaded_file = st.file_uploader("請上傳文字或 Markdown 文件", type=["txt", "md"]) |
| 9 | |
| 10 | # 如果有文件被上傳 |
timmy / 使用 Streamlit 建立使用者身份驗證系統
0 likes
0 forks
2 files
Last active 10 months ago
這段程式碼使用 Streamlit 和 streamlit-authenticator 來 管理使用者登入,透過 YAML 設定檔 儲存使用者憑證與 Cookie 配置,並提供 登入、登出 和 使用者驗證 功能。
| 1 | cookie: |
| 2 | expiry_days: 30 |
| 3 | key: some_signature_key |
| 4 | name: my_auth_cookie |
| 5 | credentials: |
| 6 | usernames: |
| 7 | jsmith: |
| 8 | email: jsmith@gmail.com |
| 9 | failed_login_attempts: 0 # Will be managed automatically |
| 10 | logged_in: False # Will be managed automatically |
timmy / 使用 Streamlit 建立簡單使用者驗證系統
0 likes
0 forks
1 files
Last active 10 months ago
這段程式碼使用 streamlit_authenticator 建立一個簡單的登入系統,支持使用者驗證(帳號、密碼)和登出功能,並在側邊欄提供登出按鈕。密碼經過雜湊處理以增加安全性。
| 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 |
timmy / MySQL 設定 root 使用者權限與密碼
0 likes
0 forks
1 files
Last active 10 months ago
這段 MySQL 指令 用於 建立 root 帳戶並賦予全域權限,適用於 開放遠端連線的 MySQL 伺服器。
| 1 | CREATE USER 'root'@'%' IDENTIFIED BY 'my_root_pwd'; |
| 2 | GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'; |
| 3 | FLUSH PRIVILEGES; -- 重新整理權限表 |
| 4 | |
| 5 | ALTER USER 'root'@'%' IDENTIFIED BY 'my_root_pwd'; |
| 6 | FLUSH PRIVILEGES; -- 重新整理權限表 |
timmy / 使用 funcy 裝飾器檢查作業系統類型
0 likes
0 forks
1 files
Last active 10 months ago
這段 Python 程式碼利用 funcy 模組的裝飾器來 檢查目前作業系統,從而限制某些函式僅在特定平台上執行。具體來說,need_mac、need_linux、need_windows 與 need_unix 分別用來檢查是否在 macOS、Linux、Windows 或 Unix(macOS 或 Linux)系統上執行;函式 foo() 被 @need_unix 裝飾,僅允許在 Unix 系統上執行,而函式 bar() 則僅允許在 Windows 系統上執行。主程式透過 try-except 捕捉例外,若目前作業系統不符合要求,則輸出相應錯誤訊息。這樣的設計有助於確保平台專屬功能在正確的環境中執行,避免跨平台錯誤。
| 1 | import platform |
| 2 | from funcy import decorator |
| 3 | |
| 4 | @decorator |
| 5 | def need_mac(call): |
| 6 | """檢查系統是否為 macOS,若不是則拋出異常。""" |
| 7 | if platform.system() != "Darwin": |
| 8 | raise Exception("This functionality is only supported in macOS") |
| 9 | return call() |
timmy / 使用 funcy.count 進行無限計數迴圈
0 likes
0 forks
1 files
Last active 10 months ago
| 1 | from funcy import count # 匯入 funcy 模組中的 count 函式 |
| 2 | |
| 3 | # 使用 count 函式生成無限迴圈,從 0 開始計數 |
| 4 | for i in count(): |
| 5 | print(i, end="\r") # 將目前的計數 i 印出,並利用Enter符 \r 讓每次印出的數字覆蓋前一個數字的位置 |
| 6 | if i >= 100_000_000: # 當計數超過或等於 100,000,000 時 |
| 7 | break # 跳出迴圈 |
timmy / 使用 funcy.flatten 展平巢狀列表
0 likes
0 forks
1 files
Last active 10 months ago
此程式使用 funcy.flatten 函式,將多層巢狀列表展平成單一列表,適用於數據處理與資料結構轉換,提高數據操作的便利性。
| 1 | from funcy import flatten # 匯入 funcy 模組中的 flatten 函式 |
| 2 | |
| 3 | mylist = [[1, 2, 3], [2, 3, 4], [3, 4, 5]] # 定義一個包含多個子列表的列表 |
| 4 | |
| 5 | print(list(flatten(mylist))) # 將 mylist 中的多個子列表展平後轉換成列表並印出 |
timmy / 使用 funcy 裝飾器測量函式執行時間
0 likes
0 forks
1 files
Last active 10 months ago
這段程式碼使用 funcy 模組的 print_durations 裝飾器,測量並印出 func 函式的執行時間,範例中透過簡單的迴圈模擬運算過程。
| 1 | from funcy import print_durations # 匯入 funcy 模組中的 print_durations 裝飾器 |
| 2 | |
| 3 | @print_durations # 裝飾器:用於計算函式執行時間並印出 |
| 4 | def func(): |
| 5 | """一個範例函式,使用範例裝飾器 print_durations 來印出其執行時間。""" |
| 6 | for i in range(10000): # 進行一個範例的迴圈計算 |
| 7 | pass |
| 8 | |
| 9 | func() # 呼叫函式 |
Newer
Older