Rajandran R Creator of OpenAlgo Sketchmaker, RagZ. Building GenAI Applications. Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, High Liquid Stock Derivatives. Trading the Markets Since 2006 onwards. Using Market Profile and Orderflow for more than a decade. Designed and published 100+ open source trading systems on various trading tools. Now the recent curiosity on building Trading AI Agents.

Host your Python Flask Web Application using pyngrok and Ngrok Free Domain

1 min read

Host a Python Flask app with pyngrok and an Ngrok free domain

Ngrok offers several significant advantages for developers, especially when it comes to testing applications or hosting machine learning models. Ngrok allows you to expose local web applications behind NATs and firewalls to the public internet over secure tunnels. This is particularly useful for testing webhooks, APIs, and web applications without deploying them to a public server, and the Ngrok free domain gives you a fixed public address.

This guide will walk you through from downloading Ngrok to running your Flask app accessible through a public URL provided by Ngrok.

Prerequisites

  • Python and Flask installed on your computer
  • Basic knowledge of Flask web development
  • A free account on Ngrok

Installing the Python Libraries

Python for traders, the quick way in

BEGINNER FRIENDLY · RATED 5.0

Python for traders, the quick way in

A crash course that gets you from zero to writing your own market data scripts.

  • Python basics with market examples
  • Fetch, clean and chart price data
  • Lifetime access
Start learning →by Marketcalls
pip install flask pyngrok

Step 1: Download ngrok

  1. Visit the Ngrok website and sign up or log in.
  2. Navigate to the download section and select the version for Windows.
  3. Download the ZIP file.
  4. Copy the Auth Token and keep it seperately later this will be used to setup the ngrok.yml config file

Step 2: Extract the ZIP File

  1. Once downloaded, extract the ZIP file to a folder of your choice, such as C:\ngrok\. This folder will contain the ngrok.exe executable.

Step 3: Add ngrok Path to the Environmental Variable

For Windows users:

  1. Unzip the ngrok executable to a directory, e.g., C:\ngrok\.
  2. Right-click on ‘This PC’ or ‘Computer’ on your desktop and select ‘Properties’.
  3. Click on ‘Advanced system settings’.
  4. Click on the ‘Environment Variables‘ button.
  5. In the ‘System variables’ section, find the ‘Path‘ variable and click ‘Edit’.
  6. Click ‘New’ and add the path to the directory where you unzipped ngrok (e.g., C:\ngrok\).
  7. Click ‘OK’ to close the dialogs and apply the changes.

Now, ngrok can be run from the command line regardless of the current directory.

Setting up the ngrok free domain

  1. Go to the Ngrok portal, navigate to Domains -> New Domain, and create your free domain.

Setting up the Ngrok Config File

  1. Open the command prompt in Windows.
  2. Enter the command ngrok config edit.
  3. This opens the ngrok config file. Enter your Auth Token, Tunnel Name, hostname, and addr as follows:
authtoken: <Your Auth Token>
version: 2
tunnels:
  flask:
    proto: http
    hostname: <your-chosen-hostname>.ngrok-free.app
    addr: 127.0.0.1:5000

Replace <Your Auth Token> with your actual Ngrok authentication token and <your-chosen-hostname> with the hostname you’ve set up.

Creating a Simple Flask App

Create a simple Flask application as follows:

from flask import Flask
from pyngrok import ngrok

app = Flask(__name__)

@app.route("/")
def myportal():
    return "Welcome to Marketcalls!"

if __name__ == '__main__':
    # Setup ngrok
    public_url = ngrok.connect(name='flask').public_url
    print(" * ngrok URL: " + public_url + " *")
    app.run()

This app will start a Flask server and make it accessible through an Ngrok URL.

Running Your Flask App

  1. Ensure you have pyngrok installed using pip install pyngrok.
  2. Run your Flask app script. It will automatically start an Ngrok tunnel and print the public URL to the console.

Visit the provided URL in your browser to access your Flask application from anywhere

By following these steps, you’ve successfully hosted your Flask application using Pyngrok with an Ngrok free domain. This setup allows you to expose your local development server to the internet securely and conveniently, facilitating testing and sharing your work with others.

Python Elite membership

MEMBERSHIP

Python Elite membership

15 Python, AI and algo trading programs in one plan, including QuantFlow and AI Bootcamp live mentorship.

  • Python from fundamentals to trading apps
  • Backtesting, broker APIs and OpenAlgo
  • Live QuantFlow and AI Bootcamp sessions
View Python Elite →by Marketcalls

Learn by watching: free videos every week

Market Profile, order flow, Python, AI and OpenAlgo tutorials on YouTube.

Rajandran R Creator of OpenAlgo Sketchmaker, RagZ. Building GenAI Applications. Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, High Liquid Stock Derivatives. Trading the Markets Since 2006 onwards. Using Market Profile and Orderflow for more than a decade. Designed and published 100+ open source trading systems on various trading tools. Now the recent curiosity on building Trading AI Agents.

Leave a Reply

Get Notifications, Alerts on Market Updates, Trading Tools, Automation & More