timmy / Streamlit 嵌入 HTML
0 polubień
0 forków
2 plików
Ostatnio aktywne 1 year ago
此範例展示如何在 Streamlit 應用中嵌入 HTML、CSS 和 JavaScript,允許自訂 UI 元素、樣式和互動功能,使 Web 應用更具靈活性和可視化效果。
| 1 | import streamlit as st |
| 2 | |
| 3 | st.title("嵌入 HTML 的範例") |
| 4 | |
| 5 | html_code = """ |
| 6 | <div style="border: 2px solid #4CAF50; padding: 10px; border-radius: 5px;"> |
| 7 | <h2 style="color: #4CAF50;">這是一個 HTML 區塊</h2> |
| 8 | <p>你可以在這裡嵌入 HTML, CSS, 甚至 JavaScript。</p> |
| 9 | <button onclick="alert('Hello, Streamlit!')">點擊我</button> |
| 10 | </div> |
timmy / 使用 Python 生成隨機密碼
0 polubień
0 forków
1 plików
Ostatnio aktywne 1 year ago
這段程式碼定義了一個 PasswordGenerator 類別,依據指定的類型("easy" 或 "hard")和密碼長度,隨機產生密碼。其中 "easy" 模式生成的密碼僅包含去除易混淆字元的大小寫英文字母和數字,而 "hard" 模式則包含了所有大小寫字母及標點符號。程式最後示範如何使用該生成器產生 10 個隨機密碼。
| 1 | import random |
| 2 | import string |
| 3 | |
| 4 | |
| 5 | class PasswordGenerator: |
| 6 | def __init__(self, type, length=12): |
| 7 | |
| 8 | self.length = length |
| 9 | self.type = type |
timmy / Python 類別與屬性訪問器
0 polubień
0 forków
1 plików
Ostatnio aktywne 1 year ago
| 1 | class Person(object): |
| 2 | |
| 3 | def __init__(self, name, age): |
| 4 | self._name = name |
| 5 | self._age = age |
| 6 | |
| 7 | # 訪問器 - getter方法 |
| 8 | @property |
| 9 | def name(self): |
| 10 | return self._name |
timmy / Python 單元測試與函式驗證
0 polubień
0 forków
1 plików
Ostatnio aktywne 1 year ago
這段程式碼使用 Python 的 unittest 模組進行單元測試,測試 add(x, y) 函式是否正確運算兩個數字的加總。
| 1 | import unittest |
| 2 | |
| 3 | |
| 4 | def add(x, y): |
| 5 | return x + y |
| 6 | |
| 7 | |
| 8 | class TestAdd(unittest.TestCase): |
| 9 | |
| 10 | def test_add_two_numbers(self): |
timmy / 使用 Streamlit 與 Pydantic 建立使用者資訊展示
0 polubień
0 forków
1 plików
Ostatnio aktywne 1 year ago
這段程式碼使用 pydantic 來定義使用者模型,並且透過 Streamlit 在網頁上顯示使用者資訊,同時 pretty_errors 提供美觀的錯誤訊息格式。
| 1 | from datetime import date |
| 2 | |
| 3 | import pretty_errors |
| 4 | import streamlit as st |
| 5 | from pydantic import BaseModel, Field |
| 6 | |
| 7 | |
| 8 | class User(BaseModel): |
| 9 | id: int |
| 10 | name: str |
timmy / Python 類別與繼承示範
0 polubień
0 forków
1 plików
Ostatnio aktywne 1 year ago
這段程式碼示範了 Python 的類別繼承機制,透過 Person(人)和 Employee(員工)類別,展現子類別如何繼承父類別的屬性與方法,並添加自己的功能。
| 1 | # 定義 Person 類別 |
| 2 | class Person: |
| 3 | # 定義建構子 |
| 4 | def __init__(self, name): |
| 5 | # 設定人的名字 |
| 6 | self.name = name |
| 7 | |
| 8 | # 定義取得人的名字的方法 |
| 9 | def get_name(self): |
| 10 | """取得人的名字""" |
timmy / 檢查 Python 套件是否已安裝
0 polubień
0 forków
1 plików
Ostatnio aktywne 1 year ago
這段程式碼用於檢查 Python 環境中是否安裝了 numpy 模組。 如果 numpy 已安裝,則輸出 "numpy is installed",否則輸出 "numpy is not installed"。
| 1 | from importlib.util import find_spec |
| 2 | |
| 3 | if find_spec("numpy") is not None: |
| 4 | print("numpy is installed") |
| 5 | else: |
| 6 | print("numpy is not installed") |
timmy / 模擬硬幣擲投並計算機率
0 polubień
0 forków
1 plików
Ostatnio aktywne 1 year ago
這段程式碼用於模擬擲硬幣 n 次,並統計正面與反面的機率。程式會計算並顯示正面與反面的出現次數與機率,以驗證隨機性。
| 1 | import random |
| 2 | |
| 3 | |
| 4 | def toss_coin(): |
| 5 | # 0代表正面,1代表反面 |
| 6 | return "正面" if random.randint(0, 1) == 0 else "反面" |
| 7 | |
| 8 | |
| 9 | def simulate_tosses(n): |
| 10 | # 初始化計數器 |
Ostatnio aktywne 1 year ago
這段程式碼模擬擲硬幣(Toss a Coin)的行為,每次執行都會隨機回傳「正面」或「反面」。
| 1 | import random |
| 2 | |
| 3 | |
| 4 | def toss_coin(): |
| 5 | # 0代表正面,1代表反面 |
| 6 | return "正面" if random.randint(0, 1) == 0 else "反面" |
| 7 | |
| 8 | |
| 9 | # 擲硬幣 |
| 10 | result = toss_coin() |
timmy / 使用 icecream 進行偵錯並透過上下文管理控制輸出
0 polubień
0 forków
1 plików
Ostatnio aktywne 1 year ago
這段 Python 程式碼 使用 contextlib.contextmanager 來建立一個 自訂的上下文管理器 (ic_disabled()),用於 暫時停用 icecream (ic) 的輸出,並在 離開上下文後復原原始狀態。
| 1 | # 引用python內建模組contextlib中的contextmanager |
| 2 | from contextlib import contextmanager |
| 3 | |
| 4 | # 將icecream模組簡稱為ic |
| 5 | from icecream import ic # 引用icecream模組 |
| 6 | |
| 7 | # 設定icecream的輸出選項,包含上下文 |
| 8 | ic.configureOutput(includeContext=True) |
| 9 | |
| 10 | # 定義ic_disabled函式為一個上下文管理器 |