Skip to content

Instantly share code, notes, and snippets.

@alexdlaird
Last active December 27, 2023 17:19
Show Gist options
  • Save alexdlaird/d701d32f2396fa76d867cf152b9d8fe2 to your computer and use it in GitHub Desktop.
Save alexdlaird/d701d32f2396fa76d867cf152b9d8fe2 to your computer and use it in GitHub Desktop.
pyngrok - Colab HTTP Example.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "code",
"metadata": {
"id": "15FGYusaCg2s"
},
"source": [
"!pip install flask\n",
"!pip install pyngrok"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "vPc_bodwBQmM",
"cellView": "code"
},
"source": [
"import getpass\n",
"import os\n",
"import threading\n",
"\n",
"from flask import Flask\n",
"from pyngrok import ngrok, conf\n",
"\n",
"print(\"Enter your authtoken, which can be copied from https://dashboard.ngrok.com/get-started/your-authtoken\")\n",
"conf.get_default().auth_token = getpass.getpass()\n",
"\n",
"app = Flask(__name__)\n",
"\n",
"# Open a ngrok tunnel to the HTTP server\n",
"public_url = ngrok.connect(5000).public_url\n",
"print(\" * ngrok tunnel \\\"{}\\\" -> \\\"http://127.0.0.1:{}/\\\"\".format(public_url, 5000))\n",
"\n",
"# Update any base URLs to use the public ngrok URL\n",
"app.config[\"BASE_URL\"] = public_url\n",
"\n",
"# ... Update inbound traffic via APIs to use the public-facing ngrok URL\n",
"\n",
"\n",
"# Define Flask routes\n",
"@app.route(\"/\")\n",
"def index():\n",
" return \"Hello from Colab!\"\n",
"\n",
"# Start the Flask server in a new thread\n",
"threading.Thread(target=app.run, kwargs={\"use_reloader\": False}).start()"
],
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment