翻译: 使用 GPT-4 将Jupyter Notebook 转换为Streamlit

GPT-4 提升Streamlit 应用系列

1 .  在几分钟内将 Jupyter 笔记本转换为 Streamlit 应用程序

如果从头开始创建 Streamlit 应用程序很有趣,那么将 Jupyter 笔记本转换为 Streamlit 应用程序就更有趣了!

GPT-4 可以无缝地自动执行大部分转换步骤,例如:

  • 用 Streamlit 语法替换传统的 Python 语法。
  • 删除特定于 Jupyter 的导入并将其替换为特定于 Streamlit 的导入。
  • 不包括 Jupyter 特定的组件,例如 cell magics (%%) 和注释。

下面是一个 Jupyter 笔记本。我要求 GPT-4 将其转换为 Streamlit 应用程序。

Jupyter Notebook --- GPT-4 处理之前:

json 复制代码
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6d9d961b-ab13-41eb-97fc-d8cdef4643eb",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a52d5c00-fe6c-4aca-bc9d-53d0d5e712ae",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Generate random data\n",
    "data = np.random.randn(1000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b5ffc30a-e93c-44f6-8e8f-f680b5d2689e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Cell magic for inline plots\n",
    "%%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a0a2f8b7-79ba-4600-86e8-d76ae0f74cae",
   "metadata": {},
   "outputs": [],
   "source": [
    "# User input for number of bins\n",
    "bins = int(input(\"Enter number of bins for histogram: \"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "72e7e336-bb35-4967-b9a0-f07e1cee7918",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Plot histogram\n",
    "plt.hist(data, bins=bins)\n",
    "plt.title(\"Random Data Histogram\")\n",
    "plt.xlabel(\"Value\")\n",
    "plt.ylabel(\"Frequency\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ca6c914c-6eb1-441e-b334-27d20dff08aa",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}

prompt

请把下面的jupyter notebook代码转换为 Streamlit应用程序

转换后的 Streamlit 应用程序 --- 经过 GPT-4 处理:

python 复制代码
import streamlit as st
import numpy as np
import matplotlib.pyplot as plt

# App title
st.title("Random Data Histogram")

# Generate random data
data = np.random.randn(1000)

# User input for number of bins
bins = st.slider("Enter number of bins for histogram:", min_value=1, max_value=100, value=10)

# Plot histogram
fig, ax = plt.subplots()
ax.hist(data, bins=bins)
ax.set_title("Random Data Histogram")
ax.set_xlabel("Value")
ax.set_ylabel("Frequency")

# Display the plot
st.pyplot(fig)

要运行这个Streamlit应用程序:

  1. 将脚本保存为一个Python文件(例如,histogram_app.py)。
  2. 如果尚未安装Streamlit,请安装它:pip install streamlit
  3. 使用Streamlit运行应用程序:streamlit run histogram_app.py

这个Streamlit应用程序将显示一个随机生成的数据直方图。用户可以使用滑块与应用程序进行交互,以调整直方图中的箱子数量。根据用户的输入,直方图会自动更新。

运行结果如下:

代码

https://github.com/zgpeace/pets-name-langchain/tree/develop

参考

https://blog.streamlit.io/take-your-streamlit-apps-to-the-next-level-with-gpt-4/

相关推荐
bst@微胖子1 小时前
Python高级语法之selenium
开发语言·python·selenium
王小义笔记1 小时前
Postman如何流畅使用DeepSeek
开发语言·测试工具·lua·postman·deepseek
查理零世2 小时前
【蓝桥杯集训·每日一题2025】 AcWing 6118. 蛋糕游戏 python
python·算法·蓝桥杯
魔尔助理顾问3 小时前
一个简洁高效的Flask用户管理示例
后端·python·flask
java1234_小锋3 小时前
一周学会Flask3 Python Web开发-request请求对象与url传参
开发语言·python·flask·flask3
流星白龙5 小时前
【C++】36.C++IO流
开发语言·c++
诚信爱国敬业友善6 小时前
常见排序方法的总结归类
开发语言·python·算法
nbsaas-boot7 小时前
Go 自动升级依赖版本
开发语言·后端·golang
架构默片7 小时前
【JAVA工程师从0开始学AI】,第五步:Python类的“七十二变“——当Java的铠甲遇见Python的液态金属
java·开发语言·python
陈敬雷-充电了么-CEO兼CTO8 小时前
DeepSeek核心算法解析:如何打造比肩ChatGPT的国产大模型
人工智能·神经网络·自然语言处理·chatgpt·大模型·aigc·deepseek