user_information_display.py
· 1.1 KiB · Python
Originalformat
from datetime import date
import pretty_errors
import streamlit as st
from pydantic import BaseModel, Field
class User(BaseModel):
id: int
name: str
dob: date = Field(title="Date of Birth")
def __str__(self):
return f"User(id={self.id}, name={self.name}, dob={self.dob})"
# 設定 pretty_errors 的顯示配置
pretty_errors.configure(
line_number_first=True, # 顯示行號在前面
lines_before=5, # 顯示錯誤行之前的行數
lines_after=2, # 顯示錯誤行之後的行數
line_color=pretty_errors.RED
+ "> "
+ pretty_errors.default_config.line_color, # 自訂錯誤行的顏色
display_locals=True, # 顯示局部變數
)
# 定義一些使用者
users = [
User(id=1, name="John", dob=date(1990, 1, 1)),
User(id=2, name="Jack", dob=date(1991, 1, 1)),
User(id=3, name="Jill", dob=date(1992, 1, 1)),
User(id=4, name="Jane", dob=date(1993, 1, 1)),
]
# 在 Streamlit 應用程式中展示使用者資訊
st.title("User Information")
for user in users:
st.write(f"ID: {user.id}, Name: {user.name}, Date of Birth: {user.dob}")
| 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 |
| 11 | dob: date = Field(title="Date of Birth") |
| 12 | |
| 13 | def __str__(self): |
| 14 | return f"User(id={self.id}, name={self.name}, dob={self.dob})" |
| 15 | |
| 16 | |
| 17 | # 設定 pretty_errors 的顯示配置 |
| 18 | pretty_errors.configure( |
| 19 | line_number_first=True, # 顯示行號在前面 |
| 20 | lines_before=5, # 顯示錯誤行之前的行數 |
| 21 | lines_after=2, # 顯示錯誤行之後的行數 |
| 22 | line_color=pretty_errors.RED |
| 23 | + "> " |
| 24 | + pretty_errors.default_config.line_color, # 自訂錯誤行的顏色 |
| 25 | display_locals=True, # 顯示局部變數 |
| 26 | ) |
| 27 | |
| 28 | |
| 29 | # 定義一些使用者 |
| 30 | users = [ |
| 31 | User(id=1, name="John", dob=date(1990, 1, 1)), |
| 32 | User(id=2, name="Jack", dob=date(1991, 1, 1)), |
| 33 | User(id=3, name="Jill", dob=date(1992, 1, 1)), |
| 34 | User(id=4, name="Jane", dob=date(1993, 1, 1)), |
| 35 | ] |
| 36 | |
| 37 | # 在 Streamlit 應用程式中展示使用者資訊 |
| 38 | st.title("User Information") |
| 39 | for user in users: |
| 40 | st.write(f"ID: {user.id}, Name: {user.name}, Date of Birth: {user.dob}") |
| 41 |