All gists

此範例展示如何使用 PyWebIO 及 JavaScript 顯示即時更新的時間。透過 put_html() 方法嵌入 HTML 和 JavaScript,並使用 setInterval() 讓時間每秒更新一次,使 PyWebIO 應用能夠提供更動態的用戶體驗。

0 0 0 1
1 from pywebio import start_server
2 from pywebio.output import put_html
3 import datetime
4
5 def use_scope_case3():
6 put_html('''
7 <div id="time"></div>
8 <script>
9 function updateTime() {
10 document.getElementById("time").innerText = new Date().toLocaleString();
Last active 9 hours ago bmi fitness health python pywebio webapp

此應用程式使用 PyWebIO 提供 Web 介面,讓使用者輸入身高和體重,並計算 BMI 指數。結果會顯示對應的 BMI 分類,例如正常、過重或肥胖等級。適用於健康管理與自我評估。

0 0 0 1
1 from pywebio import start_server
2 from pywebio.input import FLOAT, input # 引入 pywebio 模組中的 FLOAT 資料型態和 input 函數
3 from pywebio.output import put_text # 引入 pywebio 模組中的 put_text 函數
4
5 def bmi():
6 # 輸入身高和體重
7 height = input("您的身高(cm):", type=FLOAT)
8 weight = input("您的體重(kg):", type=FLOAT)
9
10 # 計算 BMI

此程式使用 Streamlit 讀取、編輯並儲存員工資料至 YAML 檔案,提供直覺化 UI 介面,並支援欄位過濾與年齡篩選,適用於企業資料管理。

0 0 0 2
1 employees:
2 - Age: 34
3 City: New York
4 Name: Alice
5 Occupation: Engineer
6 Salary: 70000
7 - Age: 27
8 City: Los Angeles
9 Name: Bob
10 Occupation: Doctor

此程式使用 tempfile.NamedTemporaryFile 建立暫存檔案,寫入內容並獲取檔案路徑,適用於臨時資料儲存、測試與文件處理應用。

1 0 0 1
1 import tempfile
2
3 # 使用 NamedTemporaryFile 建立暫存檔案
4 with tempfile.NamedTemporaryFile(delete=False) as temp_file:
5 # 在暫存檔案中寫入一些內容
6 temp_file.write(b'Hello, this is a temporary file example.')
7
8 # 獲取暫存檔案的路徑
9 temp_file_path = temp_file.name

這是一個使用 Streamlit 構建的訂餐系統,包含訂購人選擇、餐廳菜單顯示、主餐及加點品項的選擇與數量輸入,並計算和顯示訂單詳情與總價。

0 0 0 1
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

此程式使用 Streamlit 讓使用者上傳文字或 Markdown 檔案,顯示其內容,並透過暫存檔處理,可應用於文件檢視與內容處理工具。

0 0 0 1
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 # 如果有文件被上傳

這段程式碼使用 Streamlit 和 streamlit-authenticator 來 管理使用者登入,透過 YAML 設定檔 儲存使用者憑證與 Cookie 配置,並提供 登入、登出 和 使用者驗證 功能。

0 0 0 2
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

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

0 0 0 1
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