{"id":205168,"date":"2025-05-29T09:37:03","date_gmt":"2025-05-29T01:37:03","guid":{"rendered":"https:\/\/server.hk\/cnblog\/205168\/"},"modified":"2025-05-29T09:37:03","modified_gmt":"2025-05-29T01:37:03","slug":"streamlit-%e9%83%a8%e5%88%86%e5%86%99%e5%85%a5%e5%92%8c%e6%96%87%e6%9c%ac%e5%85%83%e7%b4%a0","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/205168\/","title":{"rendered":"Streamlit \u90e8\u5206\u5199\u5165\u548c\u6587\u672c\u5143\u7d20"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Streamlit \u90e8\u5206\u5199\u5165\u548c\u6587\u672c\u5143\u7d20<\/h1>\n<p>\u5c0f\u4f19\u4f34\u4eec\u6709\u6ca1\u6709\u89c9\u5f97\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u5f88\u6709\u610f\u601d\uff1f\u6709\u610f\u601d\u5c31\u5bf9\u4e86\uff01\u4eca\u5929\u5c31\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aStreamlit \u90e8\u5206\u5199\u5165\u548c\u6587\u672c\u5143\u7d20\u300b<\/span>\uff0c\u4ee5\u4e0b\u5185\u5bb9\u5c06\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u82e5\u662f\u5728\u5b66\u4e60\u4e2d\u5bf9\u5176\u4e2d\u90e8\u5206\u77e5\u8bc6\u70b9\u6709\u7591\u95ee\uff0c\u6216\u8bb8\u770b\u4e86\u672c\u6587\u5c31\u80fd\u5e2e\u5230\u4f60\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241125\/17324987666743d54ebc6d5.jpg\" class=\"aligncenter\"><\/p>\n<p>\u4ee3\u7801\u53ef\u4ee5\u5728\u8fd9\u91cc\u627e\u5230\uff1agithub &#8211; jamesbmour\/blog_tutorials\uff1a<\/p>\n<p>\u535a\u5ba2\u7684\u89c6\u9891\u7248\u672c\u53ef\u4ee5\u5728\u8fd9\u91cc\u627e\u5230\uff1ahttps:\/\/youtu.be\/eqcqnw7nw7m<\/p>\n<p>streamlit \u662f\u4e00\u4e2a\u5f00\u6e90\u5e94\u7528\u7a0b\u5e8f\u6846\u67b6\uff0c\u53ef\u8ba9\u60a8\u4ee5\u6700\u5c11\u7684\u52aa\u529b\u521b\u5efa\u7f8e\u89c2\u7684\u4ea4\u4e92\u5f0f web \u5e94\u7528\u7a0b\u5e8f\u3002\u5982\u679c\u60a8\u662f\u6570\u636e\u79d1\u5b66\u5bb6\u3001\u673a\u5668\u5b66\u4e60\u5de5\u7a0b\u5e08\u6216\u4efb\u4f55\u5904\u7406\u6570\u636e\u7684\u4eba\uff0cstreamlit \u662f\u5c06\u60a8\u7684 python \u811a\u672c\u5feb\u901f\u8f6c\u53d8\u4e3a\u4ea4\u4e92\u5f0f\u5e94\u7528\u7a0b\u5e8f\u7684\u5b8c\u7f8e\u5de5\u5177\u3002\u5728\u672c\u6559\u7a0b\u4e2d\uff0c\u6211\u4eec\u5c06\u901a\u8fc7\u63a2\u7d22 streamlit \u7684\u4e00\u4e9b\u5f3a\u5927\u529f\u80fd\uff08\u4f8b\u5982 st.write()\u3001\u9b54\u672f\u547d\u4ee4\u548c\u6587\u672c\u5143\u7d20\uff09\u6765\u6df1\u5165\u4e86\u89e3 streamlit \u7684\u57fa\u7840\u77e5\u8bc6\u3002<\/p>\n<p>\u8ba9\u6211\u4eec\u5f00\u59cb\u6784\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u5e94\u7528\u7a0b\u5e8f\u6765\u6f14\u793a\u8fd9\u4e9b\u529f\u80fd\uff01<\/p>\n<p>\u5728\u6211\u4eec\u5f00\u59cb\u7f16\u5199\u4ee3\u7801\u4e4b\u524d\uff0c\u8bf7\u786e\u4fdd\u60a8\u5df2\u7ecf\u5b89\u88c5\u4e86 streamlit\u3002\u5982\u679c\u60a8\u5c1a\u672a\u5b89\u88c5\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<pre>pip install streamlit\n<\/pre>\n<p>\u73b0\u5728\uff0c\u8ba9\u6211\u4eec\u5f00\u59cb\u7f16\u5199\u6211\u4eec\u7684\u7b2c\u4e00\u4e2a streamlit \u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<p>streamlit \u4f7f\u5411\u60a8\u7684\u5e94\u7528\u6dfb\u52a0\u6807\u9898\u548c\u6807\u9898\u53d8\u5f97\u5f02\u5e38\u7b80\u5355\u3002 st.title() \u51fd\u6570\u5141\u8bb8\u60a8\u5728\u5e94\u7528\u7a0b\u5e8f\u9876\u90e8\u663e\u793a\u4e00\u4e2a\u5927\u6807\u9898\uff0c\u4f5c\u4e3a\u4e3b\u6807\u9898\u3002<\/p>\n<pre>import streamlit as st\n\nst.title(\"introduction to streamlit: part 1\")\n\n<\/pre>\n<p>\u8fd9\u5c06\u5728\u60a8\u7684\u5e94\u7528\u9876\u90e8\u663e\u793a\u4e00\u4e2a\u5927\u7684\u3001\u7c97\u4f53\u7684\u6807\u9898\u3002<\/p>\n<p>st.write() \u51fd\u6570\u662f streamlit \u4e2d\u6700\u901a\u7528\u7684\u51fd\u6570\u4e4b\u4e00\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u663e\u793a\u51e0\u4e4e\u4efb\u4f55\u5185\u5bb9\uff0c\u5305\u62ec\u6587\u672c\u3001\u6570\u636e\u6846\u3001\u56fe\u8868\u7b49\uff0c\u6240\u6709\u8fd9\u4e9b\u90fd\u53ea\u9700\u4e00\u884c\u4ee3\u7801\u3002<\/p>\n<p>\u8ba9\u6211\u4eec\u9996\u5148\u4f7f\u7528 st.write() \u663e\u793a\u4e00\u4e2a\u7b80\u5355\u7684 dataframe\u3002<\/p>\n<pre>import pandas as pd\n\ndf = pd.dataframe({\n    \"column 1\": [1, 2, 3, 4],\n    \"column 2\": [10, 20, 30, 40]\n})\n\nst.write(\"dataframe using st.write() function\")\nst.write(df)\n\n<\/pre>\n<p>\u6b64\u4ee3\u7801\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u4e24\u5217\u7684 dataframe \u5e76\u5c06\u5176\u76f4\u63a5\u663e\u793a\u5728\u60a8\u7684\u5e94\u7528\u4e2d\u3002 st.write() \u7684\u7f8e\u5999\u4e4b\u5904\u5728\u4e8e\u5b83\u4f1a\u81ea\u52a8\u5c06 dataframe \u683c\u5f0f\u5316\u4e3a\u4e00\u4e2a\u6574\u6d01\u7684\u8868\u683c\uff0c\u5982\u679c\u9700\u8981\u7684\u8bdd\u8fd8\u53ef\u4ee5\u6dfb\u52a0\u6eda\u52a8\u6761\u3002<\/p>\n<p>st.write() \u7684\u53e6\u4e00\u4e2a\u5f88\u9177\u7684\u529f\u80fd\u662f\u5b83\u80fd\u591f\u6e32\u67d3 markdown \u6587\u672c\u3002\u8fd9\u4f7f\u60a8\u53ef\u4ee5\u8f7b\u677e\u6dfb\u52a0\u683c\u5f0f\u5316\u6587\u672c\uff0c\u4f8b\u5982\u6807\u9898\u3001\u526f\u6807\u9898\u548c\u6bb5\u843d\u3002<\/p>\n<pre>markdown_txt = (\n    \"### this is a markdown header\\\\n\"\n    \"#### this is a markdown subheader\\\\n\"\n    \"this is a markdown paragraph.\\\\n\"\n)\nst.write(markdown_txt)\n\n<\/pre>\n<p>\u53ea\u9700\u51e0\u884c\u4ee3\u7801\uff0c\u60a8\u5c31\u53ef\u4ee5\u5411\u60a8\u7684\u5e94\u7528\u6dfb\u52a0\u5bcc\u6587\u672c\u3002<\/p>\n<p>streamlit \u8fd8\u5141\u8bb8\u60a8\u4f7f\u7528 st.write_stream() \u51fd\u6570\u5c06\u6570\u636e\u5b9e\u65f6\u6d41\u5f0f\u4f20\u8f93\u5230\u60a8\u7684\u5e94\u7528\u7a0b\u5e8f\u3002\u8fd9\u5bf9\u4e8e\u663e\u793a\u968f\u65f6\u95f4\u66f4\u65b0\u7684\u6570\u636e\u7279\u522b\u6709\u7528\uff0c\u4f8b\u5982\u4f20\u611f\u5668\u8bfb\u6570\u6216\u5b9e\u65f6\u5206\u6790\u3002<\/p>\n<pre>import time\n\nst.write(\"## streaming data using st.write_stream() function\")\nstream_btn = st.button(\"click button to stream data\")\n\ntext = \"\"\"\n# stream a generator, iterable, or stream-like sequence to the app.\n\"\"\"\n\ndef stream_data(txt=\"hello, world!\"):\n    for word in txt.split(\" \"):\n        yield word + \" \"\n        time.sleep(0.01)\n\nif stream_btn:\n    st.write_stream(stream_data(text))\n\n<\/pre>\n<p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u5355\u51fb\u6309\u94ae\u65f6\uff0c\u5e94\u7528\u7a0b\u5e8f\u5c06\u5f00\u59cb\u4ece text \u5b57\u7b26\u4e32\u9010\u5b57\u6d41\u5f0f\u4f20\u8f93\u6570\u636e\uff0c\u6a21\u62df\u5b9e\u65f6\u6570\u636e\u66f4\u65b0\u3002<\/p>\n<p>\u9664\u4e86\u6570\u636e\u6d41\u4e4b\u5916\uff0cstreamlit \u8fd8\u63d0\u4f9b\u4e86\u591a\u4e2a\u6587\u672c\u5143\u7d20\u6765\u589e\u5f3a\u5e94\u7528\u7a0b\u5e8f\u7684\u5448\u73b0\u6548\u679c\u3002<\/p>\n<p>\u60a8\u53ef\u4ee5\u4f7f\u7528 st.header() \u548c st.subheader() \u8f7b\u677e\u6dfb\u52a0\u6807\u9898\u548c\u5b50\u6807\u9898\uff1a<\/p>\n<pre>st.header(\"this is a header\")\nst.subheader(\"this is a subheader\")\n\n<\/pre>\n<p>\u8fd9\u4e9b\u529f\u80fd\u6709\u52a9\u4e8e\u6784\u5efa\u60a8\u7684\u5185\u5bb9\uff0c\u4f7f\u60a8\u7684\u5e94\u7528\u66f4\u6709\u6761\u7406\u3001\u66f4\u5177\u89c6\u89c9\u5438\u5f15\u529b\u3002<\/p>\n<p>\u6807\u9898\u5bf9\u4e8e\u6dfb\u52a0\u5c0f\u6ce8\u91ca\u6216\u89e3\u91ca\u5f88\u6709\u7528\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528 st.caption() \u6dfb\u52a0\u5b83\u4eec\uff1a<\/p>\n<pre>st.caption(\"this is a caption\")\n\n<\/pre>\n<p>\u5982\u679c\u4f60\u60f3\u5728\u5e94\u7528\u7a0b\u5e8f\u4e2d\u663e\u793a\u4ee3\u7801\u7247\u6bb5\uff0c\u53ef\u4ee5\u4f7f\u7528 st.code():<\/p>\n<pre>code_txt = \"\"\"\nimport pandas as pd\nimport streamlit as st\n\nst.title(\"streamlit tutorials\")\nfor i in range(10):\n    st.write(i)\n\"\"\"\nst.code(code_txt)\n\n<\/pre>\n<p>\u8fd9\u5c06\u5728\u683c\u5f0f\u826f\u597d\u3001\u8bed\u6cd5\u7a81\u51fa\u663e\u793a\u7684\u5757\u4e2d\u663e\u793a\u4ee3\u7801\u3002<\/p>\n<p>\u5bf9\u4e8e\u90a3\u4e9b\u9700\u8981\u5305\u542b\u6570\u5b66\u65b9\u7a0b\u7684\u4eba\uff0cstreamlit \u652f\u6301 latex\uff1a<\/p>\n<pre>st.latex(r\"e = mc^2\")\nst.latex(r\"\\\\int_a^b x^2 dx\")\n\n<\/pre>\n<p>\u8fd9\u4e9b\u547d\u4ee4\u5c06\u76f4\u63a5\u5728\u60a8\u7684\u5e94\u7528\u7a0b\u5e8f\u4e2d\u6e32\u67d3 latex \u65b9\u7a0b\u3002<\/p>\n<p>\u8981\u5206\u9694\u5e94\u7528\u7a0b\u5e8f\u7684\u4e0d\u540c\u90e8\u5206\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 st.divider():<\/p>\n<pre>st.write(\"This is some text below the divider.\")\nst.divider()\nst.write(\"This is some other text below the divider.\")\n\n<\/pre>\n<p>\u5206\u9694\u7ebf\u5728\u5404\u90e8\u5206\u4e4b\u95f4\u6dfb\u52a0\u4e00\u6761\u6c34\u5e73\u7ebf\uff0c\u6709\u52a9\u4e8e\u5728\u89c6\u89c9\u4e0a\u5206\u89e3\u5185\u5bb9\u3002<\/p>\n<p>\u5728\u672c\u4ecb\u7ecd\u6027\u6559\u7a0b\u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86 streamlit \u7684\u57fa\u7840\u77e5\u8bc6\uff0c\u5305\u62ec\u5982\u4f55\u4f7f\u7528 st.write() \u663e\u793a\u6570\u636e\u548c\u6587\u672c\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528 st.write_stream() \u6d41\u5f0f\u4f20\u8f93\u6570\u636e\u3002\u6211\u4eec\u8fd8\u63a2\u7d22\u4e86\u5404\u79cd\u6587\u672c\u5143\u7d20\uff0c\u4ee5\u589e\u5f3a\u5e94\u7528\u7a0b\u5e8f\u7684\u7ed3\u6784\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<p>streamlit \u4f7f\u5f97\u53ea\u9700\u51e0\u884c\u4ee3\u7801\u5373\u53ef\u8f7b\u677e\u521b\u5efa\u4ea4\u4e92\u5f0f web \u5e94\u7528\u7a0b\u5e8f\u3002\u65e0\u8bba\u60a8\u662f\u6784\u5efa\u4eea\u8868\u677f\u3001\u6570\u636e\u63a2\u7d22\u5de5\u5177\u8fd8\u662f\u4efb\u4f55\u5176\u4ed6\u6570\u636e\u9a71\u52a8\u7684\u5e94\u7528\u7a0b\u5e8f\uff0cstreamlit \u90fd\u80fd\u63d0\u4f9b\u60a8\u5feb\u901f\u5165\u95e8\u6240\u9700\u7684\u5de5\u5177\u3002<\/p>\n<p>\u5728\u4e0b\u4e00\u4e2a\u6559\u7a0b\u4e2d\uff0c\u6211\u4eec\u5c06\u6df1\u5165\u7814\u7a76 streamlit \u4e2d\u7684\u5c0f\u90e8\u4ef6\u548c\u4ea4\u4e92\u529f\u80fd\u3002\u8bf7\u7ee7\u7eed\u5173\u6ce8\uff01<\/p>\n<p>\u5982\u679c\u60a8\u53d1\u73b0\u672c\u6559\u7a0b\u6709\u5e2e\u52a9\uff0c\u8bf7\u4e0d\u8981\u5fd8\u8bb0\u5206\u4eab\u5e76\u8ba2\u9605\u66f4\u591a\u5185\u5bb9\u3002\u4e0b\u4e00\u7bc7\u6587\u7ae0\u89c1\uff01<\/p>\n<p>\u5982\u679c\u60a8\u60f3\u652f\u6301\u6211\u7684\u5199\u4f5c\u6216\u8bf7\u6211\u559d\u676f\u5564\u9152\uff1ahttps:\/\/buymeacoffee.com\/bmours<\/p>\n<p>\u6587\u4e2d\u5173\u4e8e\u7684\u77e5\u8bc6\u4ecb\u7ecd\uff0c\u5e0c\u671b\u5bf9\u4f60\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\uff01\u82e5\u662f\u53d7\u76ca\u532a\u6d45\uff0c\u90a3\u5c31\u52a8\u52a8\u9f20\u6807\u6536\u85cf\u8fd9\u7bc7\u300aStreamlit \u90e8\u5206\u5199\u5165\u548c\u6587\u672c\u5143\u7d20\u300b\u6587\u7ae0\u5427\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u4e86\u89e3\u76f8\u5173\u6280\u672f\u6587\u7ae0\u3002<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Streamlit \u90e8\u5206\u5199\u5165\u548c\u6587&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-205168","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205168","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=205168"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205168\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=205168"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=205168"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=205168"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}