timmy / 使用 argparse 解析命令列參數
0 likes
0 forks
1 files
Last active 10 months ago
這段程式碼使用 Python 的 argparse 模組來解析命令列參數。它支援必填的位置參數(輸入檔案路徑)、選填的輸出檔案路徑(-o 或 --output),以及一個開關參數(-v 或 --verbose)來啟用詳細模式。這適用於 CLI 工具,使得用戶可以透過命令列提供不同的參數來控制程式行為。
| 1 | import argparse |
| 2 | |
| 3 | # 建立參數解析器物件 |
| 4 | parser = argparse.ArgumentParser(description='這是一個範例程式,使用 argparse 模組解析命令列參數') |
| 5 | |
| 6 | # 添加位置參數 |
| 7 | parser.add_argument('input_file', help='輸入檔案的路徑') |
| 8 | |
| 9 | # 添加選擇性參數 |
| 10 | parser.add_argument('-o', '--output', help='輸出檔案的路徑') |
timmy / Streamlit 整合 Lit Web Component
0 likes
0 forks
1 files
Last active 10 months ago
此範例展示如何在 Streamlit 應用中嵌入 Lit Web Component,而不依賴 CDN。使用 Web Components 技術,可封裝自訂 UI 元件,並透過 JavaScript 互動,適用於建立模組化的前端元件,提升 Web 應用的可維護性與重用性。
| 1 | import streamlit as st |
| 2 | import streamlit.components.v1 as components |
| 3 | |
| 4 | st.title("使用 Lit Web Component (無 CDN)") |
| 5 | |
| 6 | lit_component = """ |
| 7 | <script type="module"> |
| 8 | class MyLitComponent extends HTMLElement { |
| 9 | constructor() { |
| 10 | super(); |
timmy / Streamlit 聊天應用示範
0 likes
0 forks
1 files
Last active 10 months ago
| 1 | import streamlit as st |
| 2 | import random |
| 3 | import time |
| 4 | |
| 5 | st.write("Streamlit loves LLMs! 🤖 [Build your own chat app](https://docs.streamlit.io/develop/tutorials/llms/build-conversational-apps) in minutes, then make it powerful by adding images, dataframes, or even input widgets to the chat.") |
| 6 | |
| 7 | st.caption("Note that this demo app isn't actually connected to any LLMs. Those are expensive ;)") |
| 8 | |
| 9 | # Initialize chat history |
| 10 | if "messages" not in st.session_state: |
timmy / Streamlit 大規模資料表格與互動編輯
0 likes
0 forks
1 files
Last active 10 months ago
此範例展示如何使用 Streamlit 處理大量資料,並提供可視化表格 (dataframe) 和可編輯表格 (data_editor) 來動態調整資料。支援圖片預覽、進度條顯示及類別選擇,適用於資料分析與管理應用。
| 1 | import streamlit as st |
| 2 | import pandas as pd |
| 3 | import numpy as np |
| 4 | |
| 5 | st.write("Got lots of data? Great! Streamlit can show [dataframes](https://docs.streamlit.io/develop/api-reference/data) with hundred thousands of rows, images, sparklines – and even supports editing! ✍️") |
| 6 | |
| 7 | num_rows = st.slider("Number of rows", 1, 10000, 500) |
| 8 | np.random.seed(42) |
| 9 | data = [] |
| 10 | for i in range(num_rows): |
timmy / Streamlit 資料視覺化與互動選擇
0 likes
0 forks
1 files
Last active 10 months ago
此範例展示如何使用 Streamlit 建立互動式數據視覺化,包含多重選擇 (multiselect) 和切換開關 (toggle) 來調整數據顯示,並透過 line_chart 繪製折線圖,讓使用者輕鬆分析數據趨勢。
| 1 | import streamlit as st |
| 2 | import pandas as pd |
| 3 | import numpy as np |
| 4 | |
| 5 | st.write("Streamlit supports a wide range of data visualizations, including [Plotly, Altair, and Bokeh charts](https://docs.streamlit.io/develop/api-reference/charts). 📊 And with over 20 input widgets, you can easily make your data interactive!") |
| 6 | |
| 7 | all_users = ["Alice", "Bob", "Charly"] |
| 8 | with st.container(border=True): |
| 9 | users = st.multiselect("Users", all_users, default=all_users) |
| 10 | rolling_average = st.toggle("Rolling average") |
timmy / Streamlit 顯示自訂物件的 HTML 表示
0 likes
0 forks
1 files
Last active 10 months ago
此範例展示如何在 Streamlit 應用中,透過 st.write 和 unsafe_allow_html=True 來顯示自訂物件的 HTML 格式輸出,使物件能夠以自訂的 HTML 樣式呈現。
| 1 | import streamlit as st |
| 2 | |
| 3 | class MyObject: |
| 4 | def _repr_html_(self): |
| 5 | return "<h1 style='color:blue;'>這是一個自訂物件的 HTML 表示</h1>" |
| 6 | |
| 7 | obj = MyObject() |
| 8 | st.write(obj._repr_html_(), unsafe_allow_html=True) |
timmy / Streamlit 嵌入 HTML
0 likes
0 forks
2 files
Last active 10 months 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 likes
0 forks
1 files
Last active 10 months 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 likes
0 forks
1 files
Last active 10 months 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 |