From 06ada16b162ebd9c21068a501a435f2e1dfc3bbc Mon Sep 17 00:00:00 2001 From: XCQ <3433394217@qq.com> Date: Sat, 21 Mar 2026 13:55:50 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6=E8=87=B3?= =?UTF-8?q?=E3=80=8C/=E3=80=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 01-Streamlit-基本语法.py | 11 +++++++++++ 02-streamlit-表格组件.py | 7 +++++++ 03-streamlit-输入组件.py | 6 ++++++ 04-streamlit-聊天输入.py | 8 ++++++++ 05-streamlit-会话状态.py | 22 ++++++++++++++++++++++ 5 files changed, 54 insertions(+) create mode 100644 01-Streamlit-基本语法.py create mode 100644 02-streamlit-表格组件.py create mode 100644 03-streamlit-输入组件.py create mode 100644 04-streamlit-聊天输入.py create mode 100644 05-streamlit-会话状态.py diff --git a/01-Streamlit-基本语法.py b/01-Streamlit-基本语法.py new file mode 100644 index 0000000..d2f7d15 --- /dev/null +++ b/01-Streamlit-基本语法.py @@ -0,0 +1,11 @@ +import streamlit as st + +st.title("Streamlit 实例") + +st.write("这是一个简单的 Streamlit 应用程序。") + +st.markdown("# 这是一个一级标题") +st.markdown("## 这是一个二级标题") +st.markdown("### 这是一个三级标题") +st.markdown("#### 这是一个四级标题") +st.markdown("这是正文") \ No newline at end of file diff --git a/02-streamlit-表格组件.py b/02-streamlit-表格组件.py new file mode 100644 index 0000000..ea993d2 --- /dev/null +++ b/02-streamlit-表格组件.py @@ -0,0 +1,7 @@ +import streamlit as st + +user_data=[{"编号":1,"姓名":"小王","年龄":18,"手机号":123456789}, + {"编号":2,"姓名":"小李","年龄":18,"手机号":123456789}, + {"编号":3,"姓名":"小张","年龄":18,"手机号":123456789}] + +st.table(user_data) \ No newline at end of file diff --git a/03-streamlit-输入组件.py b/03-streamlit-输入组件.py new file mode 100644 index 0000000..a9a562a --- /dev/null +++ b/03-streamlit-输入组件.py @@ -0,0 +1,6 @@ +import streamlit as st + + +st.text_input(label="姓名:") +st.text_input(label="年龄:") +st.text_input(label="密码:",type="password") diff --git a/04-streamlit-聊天输入.py b/04-streamlit-聊天输入.py new file mode 100644 index 0000000..9474036 --- /dev/null +++ b/04-streamlit-聊天输入.py @@ -0,0 +1,8 @@ +import streamlit as st +from streamlit import user + +user_input=st.chat_input() + +if user_input: + with st.chat_message("user"): + st.write(user_input) \ No newline at end of file diff --git a/05-streamlit-会话状态.py b/05-streamlit-会话状态.py new file mode 100644 index 0000000..e984e33 --- /dev/null +++ b/05-streamlit-会话状态.py @@ -0,0 +1,22 @@ +import streamlit as st + +# 会话管理状态:在程序执行过程中,无论代码重新运行多少次,会话状态里面的变量都会保存 +st.title("黑马智聊机器人") + +# st.session_state 对象,本质就是json对象字典, +if "messages" not in st.session_state: + st.session_state["messages"] = [{"role": "assistant", "content": "你好,我是黑马智聊机器人,有什么可以帮助你的么?"}] + +for message in st.session_state.messages: + with st.chat_message(message["role"]): + st.write(message["content"]) + +# 聊天输入 +print("用户输入前打印————————") +user_input = st.chat_input("请输入你的问题:") +print(f"用户输入后打印————————{user_input}") + +if user_input: + st.session_state.messages.append({"role": "user", "content": user_input}) + with st.chat_message("user"): + st.write(user_input) \ No newline at end of file