Rajandran R Creator of OpenAlgo - OpenSource Algo Trading framework for Indian Traders. 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. Strongly believe that market understanding and robust trading frameworks are the key to the trading success. Building Algo Platforms, Writing about Markets, Trading System Design, Market Sentiment, Trading Softwares & Trading Nuances since 2007 onwards. Author of Marketcalls.in

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

1 min read

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.

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

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.

Rajandran R Creator of OpenAlgo - OpenSource Algo Trading framework for Indian Traders. 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. Strongly believe that market understanding and robust trading frameworks are the key to the trading success. Building Algo Platforms, Writing about Markets, Trading System Design, Market Sentiment, Trading Softwares & Trading Nuances since 2007 onwards. Author of Marketcalls.in

[Live Coding Webinar] Build Your First Trading Bridge for…

In this course, you will be learning to build your own trading bridge using Python. This 60-minute session is perfect for traders, Python enthusiasts,...
Rajandran R
1 min read

How to Place Orders Concurrently using ThreadPoolExecutor – Python…

Creating concurrent orders is essential for active traders, especially those handling large funds, as it allows for executing multiple trade orders simultaneously, thereby maximizing...
Rajandran R
2 min read

Building a Google Gemini Digital Assistant Slack Bot :…

In an era where efficiency is key, integrating a digital assistant into Slack using Google Gemini's AI technology offers a transformative solution. This guide...
Rajandran R
10 min read

Leave a Reply

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