2025年5月10日 星期六

Python 學習筆記 : Streamlit 升版至 v1.45.0

我在 2023 年開始學習 Streamlit (那時還是 v1.17 版), 但後來因為較常使用 Gradio 因而學習停頓下來, 最近在 LangChain 的書上發現蠻多 LLM 使用 Streamlit 來開發 AI 應用, 因此決定先來把 Streamlit 認真地測試完, 所謂工欲善其事必先利其器也.

先來提升版本, 檢視 LG 筆電目前的 Streamlit 版本 : 

>>> import streamlit as st    
>>> st.__version__   
'1.27.0'

事實上兩年間它已演進到最新的 v1.45.0 了, 而且上週安裝 LangChain 時它強制將一些與 Streamlit 共用的相依套件升版, 這可能造成舊版的 Streamlit 執行時出現錯誤, 這可以透過將 Streamlit 升至最新版解決, 指令如下 : 

pip install streamlit -U

D:\python\test>pip install streamlit -U   
Requirement already satisfied: streamlit in c:\users\tony1\appdata\roaming\python\python310\site-packages (1.27.0)
Collecting streamlit
  Downloading streamlit-1.45.0-py3-none-any.whl.metadata (8.9 kB)
Requirement already satisfied: altair<6,>=4.0 in c:\users\tony1\appdata\roaming\python\python310\site-packages (from streamlit) (5.1.1)
Requirement already satisfied: blinker<2,>=1.5.0 in c:\users\tony1\appdata\roaming\python\python310\site-packages (from streamlit) (1.6.2)
Requirement already satisfied: cachetools<6,>=4.0 in c:\users\tony1\appdata\roaming\python\python310\site-packages (from streamlit) (5.3.1)
Requirement already satisfied: click<9,>=7.0 in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (from streamlit) (8.1.8)
Requirement already satisfied: numpy<3,>=1.23 in c:\users\tony1\appdata\roaming\python\python310\site-packages (from streamlit) (1.24.3)
Requirement already satisfied: packaging<25,>=20 in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (from streamlit) (24.2)
Requirement already satisfied: pandas<3,>=1.4.0 in c:\users\tony1\appdata\roaming\python\python310\site-packages (from streamlit) (2.0.3)
Requirement already satisfied: pillow<12,>=7.1.0 in c:\users\tony1\appdata\roaming\python\python310\site-packages (from streamlit) (9.5.0)
Requirement already satisfied: protobuf<7,>=3.20 in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (from streamlit) (5.28.1)
Requirement already satisfied: pyarrow>=7.0 in c:\users\tony1\appdata\roaming\python\python310\site-packages (from streamlit) (13.0.0)
Requirement already satisfied: requests<3,>=2.27 in c:\users\tony1\appdata\roaming\python\python310\site-packages (from streamlit) (2.31.0)
Requirement already satisfied: tenacity<10,>=8.1.0 in c:\users\tony1\appdata\roaming\python\python310\site-packages (from streamlit) (8.2.3)
Requirement already satisfied: toml<2,>=0.10.1 in c:\users\tony1\appdata\roaming\python\python310\site-packages (from streamlit) (0.10.2)
Requirement already satisfied: typing-extensions<5,>=4.4.0 in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (from streamlit) (4.12.2)
Requirement already satisfied: watchdog<7,>=2.1.5 in c:\users\tony1\appdata\roaming\python\python310\site-packages (from streamlit) (2.3.1)
Requirement already satisfied: gitpython!=3.1.19,<4,>=3.0.7 in c:\users\tony1\appdata\roaming\python\python310\site-packages (from streamlit) (3.1.37)
Requirement already satisfied: pydeck<1,>=0.8.0b4 in c:\users\tony1\appdata\roaming\python\python310\site-packages (from streamlit) (0.8.1b0)
Requirement already satisfied: tornado<7,>=6.0.3 in c:\users\tony1\appdata\roaming\python\python310\site-packages (from streamlit) (6.3.3)
Requirement already satisfied: jinja2 in c:\users\tony1\appdata\roaming\python\python310\site-packages (from altair<6,>=4.0->streamlit) (3.1.2)
Requirement already satisfied: jsonschema>=3.0 in c:\users\tony1\appdata\roaming\python\python310\site-packages (from altair<6,>=4.0->streamlit) (4.19.0)
Requirement already satisfied: toolz in c:\users\tony1\appdata\roaming\python\python310\site-packages (from altair<6,>=4.0->streamlit) (0.12.0)
Requirement already satisfied: colorama in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (from click<9,>=7.0->streamlit) (0.4.6)
Requirement already satisfied: gitdb<5,>=4.0.1 in c:\users\tony1\appdata\roaming\python\python310\site-packages (from gitpython!=3.1.19,<4,>=3.0.7->streamlit) (4.0.10)
Requirement already satisfied: smmap<6,>=3.0.1 in c:\users\tony1\appdata\roaming\python\python310\site-packages (from gitdb<5,>=4.0.1->gitpython!=3.1.19,<4,>=3.0.7->streamlit) (5.0.1)
Requirement already satisfied: python-dateutil>=2.8.2 in c:\users\tony1\appdata\roaming\python\python310\site-packages (from pandas<3,>=1.4.0->streamlit) (2.8.2)
Requirement already satisfied: pytz>=2020.1 in c:\users\tony1\appdata\roaming\python\python310\site-packages (from pandas<3,>=1.4.0->streamlit) (2023.3)
Requirement already satisfied: tzdata>=2022.1 in c:\users\tony1\appdata\roaming\python\python310\site-packages (from pandas<3,>=1.4.0->streamlit) (2023.3)
Requirement already satisfied: charset-normalizer<4,>=2 in c:\users\tony1\appdata\roaming\python\python310\site-packages (from requests<3,>=2.27->streamlit) (3.2.0)
Requirement already satisfied: idna<4,>=2.5 in c:\users\tony1\appdata\roaming\python\python310\site-packages (from requests<3,>=2.27->streamlit) (3.4)
Requirement already satisfied: urllib3<3,>=1.21.1 in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (from requests<3,>=2.27->streamlit) (1.26.19)
Requirement already satisfied: certifi>=2017.4.17 in c:\users\tony1\appdata\roaming\python\python310\site-packages (from requests<3,>=2.27->streamlit) (2023.7.22)
Requirement already satisfied: MarkupSafe>=2.0 in c:\users\tony1\appdata\roaming\python\python310\site-packages (from jinja2->altair<6,>=4.0->streamlit) (2.1.3)
Requirement already satisfied: attrs>=22.2.0 in c:\users\tony1\appdata\roaming\python\python310\site-packages (from jsonschema>=3.0->altair<6,>=4.0->streamlit) (23.1.0)
Requirement already satisfied: jsonschema-specifications>=2023.03.6 in c:\users\tony1\appdata\roaming\python\python310\site-packages (from jsonschema>=3.0->altair<6,>=4.0->streamlit) (2023.7.1)
Requirement already satisfied: referencing>=0.28.4 in c:\users\tony1\appdata\roaming\python\python310\site-packages (from jsonschema>=3.0->altair<6,>=4.0->streamlit) (0.30.2)
Requirement already satisfied: rpds-py>=0.7.1 in c:\users\tony1\appdata\roaming\python\python310\site-packages (from jsonschema>=3.0->altair<6,>=4.0->streamlit) (0.10.2)
Requirement already satisfied: six>=1.5 in c:\users\tony1\appdata\local\programs\thonny\lib\site-packages (from python-dateutil>=2.8.2->pandas<3,>=1.4.0->streamlit) (1.16.0)
Downloading streamlit-1.45.0-py3-none-any.whl (9.9 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 9.9/9.9 MB 8.2 MB/s eta 0:00:00
Installing collected packages: streamlit
  Attempting uninstall: streamlit
    Found existing installation: streamlit 1.27.0
    Uninstalling streamlit-1.27.0:
      Successfully uninstalled streamlit-1.27.0
Successfully installed streamlit-1.45.0

在命令列可用 streamlit version 指令檢視目前版本 : 

D:\python\test>streamlit version  
Streamlit, version 1.45.0  

也可以進入 Python 執行環境檢查 : 

>>> import streamlit as st  
>>> st.__version__   
'1.45.0'

新版主要的功能與架構異動摘要如下 : 
  1. 新的分頁架構 :
    新版 API 引入 st.navigation() 和 st.Page() 函式讓開發者能以程式化方式定義應用的頁面與導覽功能, 使頁面可以依據使用者的操作而動態變化, 並支援在主程式中設定共用的初始化邏輯, 樣式與權限檢查. 新的側邊欄導覽也支援群組標題, Material Icons 及應用程式 Logo 等功能. 
  2. 使用者資訊存取物件 st.user : 
    st.user 是一個類似字典的物件, 可讓開發者存取目前使用者的相關資訊, 例如 IP, URL, 以及是否為嵌入式等, 這對於需要進行使用者識別或權限控管的應用特別有用.
  3. 增強元件的互動性 :
    例如 st.multiselect 和 st.selectbox 引入了可讓使用者新增自定義選項的參數; st.text_input 和 st.number_input 現在可以在輸入框左側加入圖示以提升使用者介面的可讀性與美觀性; st.components.v1.html 和 st.components.v1.iframe 新增設定 tab index 的功能, 可改善鍵盤導覽體驗. 
  4. 自訂主題與外觀 : 
    新版引入了進階的主題設定, 讓開發者無需撰寫 CSS 就可以透過設定檔自訂應用程式的字體, 顏色與圓角等外觀. 此外 st.badge 元件允許在應用中插入彩色徽章, 並可在 Markdown 中使用新的指令來加入徽章.
  5. DataFrame 資料框互動性提升 :
    使用者可以拖曳資料框的欄位以重新排序; 支援釘選功能並在滑鼠懸停時高亮顯示列; 當資料框中的欄位值與其設定的類型不一致時 Streamlit 會顯示工具提示以說明錯誤.
  6. 其他重大改變 :
    • 音訊輸入元件改為 st.audio_input, 實驗版的 st.experimental_audio_input 元件已被移除.
    • 支援 Python 3.13 並停止支援 Python 3.8.
    • st.exception 元件已可包含連結以方便開發者直接在 Google 或 ChatGPT 中查詢錯誤內容. 
新版在多頁應用架構, 使用者資訊存取, 與元件互動性與主題自訂等方面的變更可說了增進了開發者的方便與大幅提升了使用者體驗. 

沒有留言 :