Skip to content

Instantly share code, notes, and snippets.

@howard-haowen
Created December 5, 2020 04:13
Show Gist options
  • Save howard-haowen/a2b8dfd7c3565f9511120ce19a2f5604 to your computer and use it in GitHub Desktop.
Save howard-haowen/a2b8dfd7c3565f9511120ce19a2f5604 to your computer and use it in GitHub Desktop.
Create-streamlit-app.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Create-streamlit-app.ipynb",
"provenance": [],
"collapsed_sections": [],
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/howard-haowen/a2b8dfd7c3565f9511120ce19a2f5604/create-streamlit-app.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "vWmc_s2ezvU0"
},
"source": [
"# Run streamlit app from a Google Colab Notebook\n",
"> Created by [Manuel Romero](https://twitter.com/mrm8488)"
]
},
{
"cell_type": "code",
"metadata": {
"id": "RvlYkCQ9vFiy",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 254
},
"outputId": "0fc8c222-84b4-4073-aadb-c6c2dbc18060"
},
"source": [
"!pip install -q streamlit"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"\u001b[K |████████████████████████████████| 7.1MB 2.8MB/s \n",
"\u001b[K |████████████████████████████████| 4.4MB 45.5MB/s \n",
"\u001b[K |████████████████████████████████| 522kB 50.5MB/s \n",
"\u001b[K |████████████████████████████████| 112kB 51.3MB/s \n",
"\u001b[K |████████████████████████████████| 102kB 10.5MB/s \n",
"\u001b[K |████████████████████████████████| 122kB 48.4MB/s \n",
"\u001b[?25h Building wheel for validators (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
" Building wheel for tornado (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
" Building wheel for blinker (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
" Building wheel for watchdog (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
" Building wheel for pathtools (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
"\u001b[31mERROR: google-colab 1.0.0 has requirement ipykernel~=4.10, but you'll have ipykernel 5.3.0 which is incompatible.\u001b[0m\n",
"\u001b[31mERROR: google-auth 1.7.2 has requirement cachetools<3.2,>=2.0.0, but you'll have cachetools 4.1.0 which is incompatible.\u001b[0m\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "uzvl_5Cv8HeX"
},
"source": [
"Reset the execution environment after streamlit installation"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "jnZ0QpzC0UiU"
},
"source": [
"## Download a streamlit app example\n",
"\n",
"This example comes from [How to Build a Simple Machine Learning Web App in Python](https://www.youtube.com/watch?v=8M20LyCZDOY) and it will create a **Simple Iris Flower Prediction App**"
]
},
{
"cell_type": "code",
"metadata": {
"id": "S1sic9Rt0T7l",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 217
},
"outputId": "cec70d41-2ef8-44b9-d97c-4661bd2cc13c"
},
"source": [
"!wget https://raw.githubusercontent.com/dataprofessor/code/master/streamlit/iris-ml-app.py"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"--2020-06-13 17:56:34-- https://raw.githubusercontent.com/dataprofessor/code/master/streamlit/iris-ml-app.py\n",
"Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.0.133, 151.101.64.133, 151.101.128.133, ...\n",
"Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|151.101.0.133|:443... connected.\n",
"HTTP request sent, awaiting response... 200 OK\n",
"Length: 1350 (1.3K) [text/plain]\n",
"Saving to: ‘iris-ml-app.py’\n",
"\n",
"\riris-ml-app.py 0%[ ] 0 --.-KB/s \riris-ml-app.py 100%[===================>] 1.32K --.-KB/s in 0s \n",
"\n",
"2020-06-13 17:56:34 (66.6 MB/s) - ‘iris-ml-app.py’ saved [1350/1350]\n",
"\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "encqGJ4u0FYG"
},
"source": [
"## Install ngrok"
]
},
{
"cell_type": "code",
"metadata": {
"id": "VSPUMEHYwqng",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 217
},
"outputId": "a0af7626-ecf4-4f29-ca43-996a244ca2bc"
},
"source": [
"!wget https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"--2020-06-13 17:56:52-- https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip\n",
"Resolving bin.equinox.io (bin.equinox.io)... 34.233.91.203, 18.232.237.110, 52.71.32.255, ...\n",
"Connecting to bin.equinox.io (bin.equinox.io)|34.233.91.203|:443... connected.\n",
"HTTP request sent, awaiting response... 200 OK\n",
"Length: 13773305 (13M) [application/octet-stream]\n",
"Saving to: ‘ngrok-stable-linux-amd64.zip’\n",
"\n",
"\r ngrok-sta 0%[ ] 0 --.-KB/s \r ngrok-stab 69%[============> ] 9.08M 45.0MB/s \rngrok-stable-linux- 100%[===================>] 13.13M 56.9MB/s in 0.2s \n",
"\n",
"2020-06-13 17:56:52 (56.9 MB/s) - ‘ngrok-stable-linux-amd64.zip’ saved [13773305/13773305]\n",
"\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "6679q_6fwsJH",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 54
},
"outputId": "bbe17694-7ead-4024-b5c3-034317ba4cd8"
},
"source": [
"!unzip ngrok-stable-linux-amd64.zip"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"Archive: ngrok-stable-linux-amd64.zip\n",
" inflating: ngrok \n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "qGCM2OjvxNit"
},
"source": [
"get_ipython().system_raw('./ngrok http 8501 &')"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "bxwv00hdxedU",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 35
},
"outputId": "8736d706-fd8d-47b7-e846-a9045c705600"
},
"source": [
"!curl -s http://localhost:4040/api/tunnels | python3 -c \\\n",
" 'import sys, json; print(\"Execute the next cell and the go to the following URL: \" +json.load(sys.stdin)[\"tunnels\"][0][\"public_url\"])'"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"Execute the next cell and the go to the following URL: http://e80f4a391ec8.ngrok.io\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "IFvZnzS4vr88",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 145
},
"outputId": "7518e6b1-dfe7-4c7e-bec7-a2b3896321d3"
},
"source": [
"!streamlit run /content/iris-ml-app.py"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"\u001b[0m\n",
"\u001b[34m\u001b[1m You can now view your Streamlit app in your browser.\u001b[0m\n",
"\u001b[0m\n",
"\u001b[34m Network URL: \u001b[0m\u001b[1mhttp://172.28.0.2:8501\u001b[0m\n",
"\u001b[34m External URL: \u001b[0m\u001b[1mhttp://35.185.118.46:8501\u001b[0m\n",
"\u001b[0m\n",
"\u001b[34m Stopping...\u001b[0m\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "WJf7BCm84KWe"
},
"source": [
"[![ko-fi](https://www.ko-fi.com/img/githubbutton_sm.svg)](https://ko-fi.com/Y8Y3VYYE)"
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment