{"id":204109,"date":"2025-05-29T12:48:36","date_gmt":"2025-05-29T04:48:36","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204109\/"},"modified":"2025-05-29T12:48:36","modified_gmt":"2025-05-29T04:48:36","slug":"%e4%bd%bf%e7%94%a8-streamlit-%e5%88%b6%e4%bd%9c-web-%e5%ba%94%e7%94%a8%e7%a8%8b%e5%ba%8f%e6%98%af%e5%a6%82%e6%ad%a4%e7%ae%80%e5%8d%95","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204109\/","title":{"rendered":"\u4f7f\u7528 Streamlit \u5236\u4f5c Web \u5e94\u7528\u7a0b\u5e8f\u662f\u5982\u6b64\u7b80\u5355"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u4f7f\u7528 Streamlit \u5236\u4f5c Web \u5e94\u7528\u7a0b\u5e8f\u662f\u5982\u6b64\u7b80\u5355<\/h1>\n<p>\u672c\u7bc7\u6587\u7ae0\u5411\u5927\u5bb6\u4ecb\u7ecd\u300a\u4f7f\u7528 Streamlit \u5236\u4f5c Web \u5e94\u7528\u7a0b\u5e8f\u662f\u5982\u6b64\u7b80\u5355\u300b\uff0c\u4e3b\u8981\u5305\u62ec\uff0c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u4ef7\u503c\uff0c\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241028\/1730100379671f3c9b9d2fc.jpg\" class=\"aligncenter\"><\/p>\n<p>streamlit \u5728\u6570\u636e\u79d1\u5b66\u5bb6\u4e2d\u5f88\u53d7\u6b22\u8fce\uff0c\u56e0\u4e3a\u60a8\u901a\u5e38\u4e0d\u9700\u8981\u524d\u7aef\u77e5\u8bc6\u3002<\/p>\n<p>\u5b83\u4eec\u63d0\u4f9b\u7b80\u5355\u4e14\u6613\u4e8e\u5b9e\u73b0\u7684\u5143\u7d20\u548c\u5c0f\u90e8\u4ef6\uff0c\u65e0\u9700\u7f16\u5199\u592a\u591a\u4ee3\u7801\u3002<\/p>\n<p>\u6211\u5728 ml\/ai \u9879\u76ee\u4e2d\u591a\u6b21\u4f7f\u7528\u4e86 streamlit\uff0c\u4f53\u9a8c\u975e\u5e38\u68d2\u3002\u4f60\u53ef\u4ee5\u66f4\u4e13\u6ce8\u4e8e\u7f16\u5199\u903b\u8f91\uff0c\u524d\u7aef\u90e8\u5206\uff08\u8bbe\u8ba1\u3001\u5e03\u5c40\u7b49\uff09\u7531streamlit\u5904\u7406\u5f97\u5f88\u597d\u3002<\/p>\n<p>\u6211\u4f7f\u7528 streamlit \u548c python \u521b\u5efa\u4e86\u4e00\u4e2a\u6f14\u793a web \u5e94\u7528\u7a0b\u5e8f\uff0c\u4ee5\u4fbf\u60a8\u53ef\u4ee5\u7406\u89e3\u6211\u5728\u8bf4\u4ec0\u4e48\u3002<\/p>\n<p>\u8fd9\u4e2a\u7f51\u7edc\u5e94\u7528\u7a0b\u5e8f\u662f\u5c06\u56fe\u50cf\u683c\u5f0f\u8f6c\u6362\u4e3a\u53e6\u4e00\u79cd\u683c\u5f0f\uff0c\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u7684\u56fe\u50cf\u662f png \u683c\u5f0f\uff0c\u60a8\u53ef\u4ee5\u5c06\u5176\u8f6c\u6362\u4e3a jpeg \u56fe\u50cf\u3002<\/p>\n<p>\u4ee5\u4e0b\u4ee3\u7801\u5236\u4f5c\u4e86 web \u5e94\u7528\u7a0b\u5e8f\u7684\u7528\u6237\u754c\u9762\u3002<\/p>\n<pre>import streamlit as st\nfrom imgconvrtr import convert_img_format\nfrom pil import image\n\n# webpage setup\nst.set_page_config(page_title=\"image convrtr\")\nst.title(\"image converter\")\nst.write(\"convert your images in one _click_\")\n\n# file uploader\nuploaded_file = st.file_uploader(\n    \"upload an image\",\n    type=[\"png\", \"jpg\", \"jpeg\", \"jfif\", \"bmp\"]\n)\n\nif uploaded_file is not none:\n    # show the uploaded image\n    img = image.open(uploaded_file)\n    st.image(img, caption=\"uploaded image\", use_column_width=true)\n\n    # show original image format\n    st.write(f\"original format: {img.format}\")\n\n    # output format selection\n    format_options = [\"png\", \"jpeg\", \"jfif\", \"bmp\"]\n    output_format = st.selectbox(\"choose output format\", format_options)\n\n    # convert the image\n    if img.format != output_format:\n        if st.button(\"convert\"):\n            converted_img = convert_img_format(uploaded_file, output_format.lower())\n            st.write(f\"image converted to {output_format}\")\n\n            # download button\n            st.download_button(\n                label=f\"download as {output_format}\",\n                data=converted_img,\n                file_name=f\"image.{output_format.lower()}\",\n                mime=f\"image\/{output_format.lower()}\"\n            )\n    else:\n        st.write(\"select a different format... yo!\")\n<\/pre>\n<p>\u73b0\u5728\u60a8\u5df2\u7ecf\u5927\u81f4\u4e86\u89e3\u4e86\u8fd9\u4e2a\u7f51\u7edc\u5e94\u7528\u7a0b\u5e8f\u7684\u7528\u9014\u3002\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u8df3\u5230\u8ba8\u8bba\u6b64\u4ee3\u7801\u4e2d\u4f7f\u7528\u7684\u7ec4\u4ef6\u3002<\/p>\n<p>\u4e00\u5f00\u59cb\uff0c\u4f60\u53ef\u4ee5\u770b\u5230 st.title \u548c st.write \u7b49\u9875\u9762\u5143\u7d20\uff0c\u5206\u522b\u7528\u4e8e\u8bbe\u7f6e\u9875\u9762\u6807\u9898\u548c\u5728\u9875\u9762\u4e0a\u663e\u793a\u6587\u672c\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u60a8\u53ef\u4ee5\u770b\u5230\u4e00\u4e2a\u7528\u4e8e\u4e0a\u4f20\u6587\u4ef6\u7684\u5c0f\u90e8\u4ef6\uff08\u5728\u672c\u4f8b\u4e2d\u7528\u4e8e\u4e0a\u4f20\u56fe\u50cf\uff09\u3002\u770b\u770b\u521b\u5efa\u6587\u4ef6\u4e0a\u4f20\u5668\u662f\u591a\u4e48\u5bb9\u6613\u3002<\/p>\n<p>st.image \u7528\u4e8e\u663e\u793a\u7528\u6237\u4e0a\u4f20\u7684\u56fe\u7247\u3002<\/p>\n<p>\u7136\u540e\u6211\u4eec\u6709\u4e00\u4e2a\u4e0b\u62c9\u83dc\u5355\u6765\u9009\u62e9\u4f7f\u7528\u9009\u62e9\u6846\uff08st.selectbox\uff09\u5c0f\u90e8\u4ef6\u521b\u5efa\u7684\u5404\u79cd\u683c\u5f0f\u3002<\/p>\n<p>\u73b0\u5728\uff0c\u60a8\u53ef\u4ee5\u770b\u5230\u6211\u4eec\u6709\u4e24\u4e2a\u6309\u94ae\uff08st.button \u548c st.download_button\uff09\u3002\u5b83\u4eec\u90fd\u662f\u4e00\u6837\u7684\uff0c\u4f46\u90fd\u662f\u4e3a\u4e86\u65b9\u4fbf\u3002<\/p>\n<p>st.button \u663e\u793a\u6211\u4eec\u5728\u8fd9\u91cc\u7528\u4e8e\u56fe\u50cf\u8f6c\u6362\u7684\u6309\u94ae\u5c0f\u90e8\u4ef6\u3002<\/p>\n<p>\u5f53\u7528\u6237\u9700\u8981\u76f4\u63a5\u4ece\u5e94\u7528\u7a0b\u5e8f\u4e0b\u8f7d\u6587\u4ef6\u65f6\uff0cst.download_button \u975e\u5e38\u6709\u7528\u3002<\/p>\n<p>streamlit \u63d0\u4f9b\u4e86\u8bb8\u591a\u7528\u4e8e\u4e0d\u540c\u76ee\u7684\u7684\u5143\u7d20\u548c\u5c0f\u90e8\u4ef6\u3002<\/p>\n<p>\u73b0\u5728\u5982\u679c\u4f60\u60f3\u5c1d\u8bd5\u8fd9\u4e2a\u7f51\u7edc\u5e94\u7528\u7a0b\u5e8f\uff0c\u4f60\u9700\u8981\u5b89\u88c5\u6240\u9700\u7684\u5e93\uff1a<\/p>\n<pre>pip install streamlit pillow\n<\/pre>\n<p>\u8fd9\u662f\u56fe\u50cf\u8f6c\u6362\u51fd\u6570\uff1a<\/p>\n<pre>from pil import image\nimport io\n\n# function to convert image format\ndef convert_img_format(image_file, frmat):\n    with image.open(image_file) as img:\n        output_img = io.bytesio()\n        img.save(output_img, format=frmat.upper())\n        output_img.seek(0)\n        return output_img\n<\/pre>\n<p>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f\uff1a<\/p>\n<pre>streamlit run &lt;script_name&gt;.py\n<\/pre>\n<p>\u5c06 &lt;scipt_name&gt; \u66ff\u6362\u4e3a\u5b9e\u9645\u811a\u672c\u540d\u79f0\u3002<\/p>\n<hr>\n<p><strong>\u73b0\u5728\u5c31\u8fd9\u4e9b\u3002<\/strong><br \/><strong>\u7ee7\u7eed\u7f16\u7801<\/strong><\/p>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300a\u4f7f\u7528 Streamlit \u5236\u4f5c Web \u5e94\u7528\u7a0b\u5e8f\u662f\u5982\u6b64\u7b80\u5355\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8\u516c\u4f17\u53f7\u5427\uff01<\/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>\u4f7f\u7528 Streamlit \u5236\u4f5c &#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-204109","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204109","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=204109"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204109\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204109"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204109"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204109"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}