当前位置:   article > 正文

【小笔记】streamlit使用笔记_在phcht中运行 streamlit 应用程序 - 知乎

在phcht中运行 streamlit 应用程序 - 知乎

【小笔记】streamlit使用笔记

在这里插入图片描述

1.streamlit是什么,为什么要用它?

一句话,这个东西是一个python的可视化库,当你想要给你的程序添加个web界面,而又不会或不想用前端技术时,你就可以考虑用它。
类似的可视化库还有下面这几个,对比如下:
在这里插入图片描述
参考:
Python 可视化 web 神器:streamlit、Gradio、dash、nicegui;低代码 Python Web 框架:PyWebIO

2.安装及版本要求:

安装:
pip install streamlit

以下辅助库也可以安装:
pip install pyecharts
pip install streamlit_echarts

特别注意:streamlit最新版要求python >= 3.8

3.入门:一个问答demo

教程参考:小白也能轻松搞定:用几行代码实现强大的问答机器人!

创建:
demo.py


import streamlit as st


def display_existing_messages():
    if "messages" not in st.session_state:
        st.session_state["messages"] = []
    for message in st.session_state["messages"]:
        with st.chat_message(message["role"]):
            st.markdown(message["content"])


def add_user_message_to_session(prompt):
    if prompt:
        st.session_state["messages"].append({"role": "user", "content": prompt})
        with st.chat_message("user"):
            st.markdown(prompt)

#
def generate_assistant_response(query):
    # add_user_message_to_session 显示消息的时候做了处理,所以这里不需要再次添加最新提问
    print('history-->')
    history = st.session_state["messages"]
    print(history)
    with st.chat_message("assistant"):
        message_placeholder = st.empty()
        full_response = ""
        for response in client.chat.completions.create(
                model=model,
                temperature=0,
                messages=history,
                stream=True,
        ):
            try:
                full_response += response.choices[0].delta.content
            except Exception as e:
                print("")
            message_placeholder.markdown(full_response + "▌")
        message_placeholder.markdown(full_response)

        st.session_state["messages"].append(
            {"role": "assistant", "content": full_response}
        )
    return full_response


def hide_streamlit_header_footer():
    hide_st_style = """
            <style>
            #MainMenu {visibility: hidden;}
            footer {visibility: hidden;}
            header {visibility: hidden;}
            #root > div:nth-child(1) > div > div > div > div > section > div {padding-top: 0rem;}
            </style>
            """
    st.markdown(hide_st_style, unsafe_allow_html=True)


def main():
    st.title("问答机器人")
    st.write(
        "我的第一个专属机器人,它可以回答你的问题,也可以和你聊天。"
    )

    hide_streamlit_header_footer()
    display_existing_messages()

    query = st.chat_input("你可以问我任何你想问的问题")

    if query:
        print(query)
        add_user_message_to_session(query)
        # response = generate_assistant_response(query)
        # print(response)


if __name__ == "__main__":
    main()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79

启动:
streamlit run demo.py

在这里插入图片描述

其它问答系统界面参考示例:
1.【Langchain+Streamlit】打造一个旅游问答AI
在这里插入图片描述

2.当科技遇上神奇:用Streamlit打造定制AI问答界面
在这里插入图片描述

官方的chatGLM3-6B问答界面也是基于streamlit创建的。说明streamlit还是很好用的。
在这里插入图片描述

4.streamlit基础组件及教程

1.基础组件使用教程
1.实用篇 | 一文快速构建人工智能前端展示streamlit应用
2.python库streamlit学习笔记
3.streamlit中文开发手册(详细版)
4.Streamlit开发手册

2.布局教程
1.streamlit框架,各种图表绘制,布局以及生产综合案例剖析
2.Streamlit 布局篇(容器布局)

6.streamlit结合echarts
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Guff_9hys/article/detail/1004344

推荐阅读
相关标签