Last active
June 5, 2025 16:21
-
-
Save alexdlaird/8aea9f23c4b139f61d8a887b4e6aaef6 to your computer and use it in GitHub Desktop.
Flask integration example for pyngrok, full documentation found at https://pyngrok.readthedocs.io/en/latest/integrations.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# USE_NGROK=True FLASK_APP=server.py flask run | |
import os | |
import sys | |
from flask import Flask | |
def init_webhooks(base_url): | |
# Update inbound traffic via APIs to use the public-facing ngrok URL | |
pass | |
def create_app(): | |
app = Flask(__name__) | |
# Initialize our ngrok settings into Flask | |
app.config.from_mapping( | |
BASE_URL="http://localhost:5000", | |
USE_NGROK=os.environ.get("USE_NGROK", "False") == "True" and os.environ.get("WERKZEUG_RUN_MAIN") != "true" | |
) | |
if app.config["USE_NGROK"] and os.environ.get("NGROK_AUTHTOKEN"): | |
# pyngrok will only be installed, and should only ever be initialized, in a dev environment | |
from pyngrok import ngrok | |
# Get the dev server port (defaults to 5000 for Flask, can be overridden with `--port` | |
# when starting the server | |
port = sys.argv[sys.argv.index("--port") + 1] if "--port" in sys.argv else "5000" | |
# Open a ngrok tunnel to the dev server | |
public_url = ngrok.connect(port) | |
print(f" * ngrok tunnel \"{public_url}\" -> \"http://127.0.0.1:{port}\"") | |
# Update any base URLs or webhooks to use the public ngrok URL | |
app.config["BASE_URL"] = public_url | |
init_webhooks(public_url) | |
# ... Initialize Blueprints and the rest of our app | |
return app |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment