How to Run Python Flask App Online using Ngrok?


Ngrok is a tool that is used to create a secure tunnel between your local machine and the internet. It is used to test the web application and allows developers to expose their local web server to the internet without having to deploy it to a remote server. Python Flask allows you to create web applications locally but we might want to showcase it to the world by running it online. In this article, we will use the Ngrok tool to run the web application online without hosting it on any server.

The Steps to Run Python Flask App Online Using Ngrok are as follows −

Step 1: Install Ngrok

The first step is to download Ngrok in your local system. You can download Ngrok from its official website (https://ngrok.com/download). You can download it for your required operating System (Windows/macOS/Linux). Once you have downloaded the required system, simply extract the contents of the archive to the folder of your choice.

Step 2: Create a Flask App

Now once we have installed the Ngrok tool, we need to create a Flask App which we will use to Run using Ngrok. Create a new Python file `app.py` and write the code shown below to create a Flask app with a single route that returns the string “Hello World!” when the root URL is accessed.

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello():
   return 'Hello, World!'

if __name__ == '__main__':
   app.run(port=5000)

Step 3: Run the Flask App

After creating the Flask App we need to run the Flask App locally and check if the app is working correctly or not. To run the Flask app locally, simply run the below command in your terminal or command prompt.

Python app.py

To check if the App is working you can hit the `http://localhost:5000` on your browser and see the returned value by the App.

Step 4: Run Ngrok

To run Ngrok, open a new terminal window and navigate to the folder where you extracted the Ngrok archive. Then run the following command −

./ngrok http 5000

This command will start Ngrok and create a secure tunnel between your local machine and the internet. Ngrok will display a URL that you can use to access your Flask app from anywhere in the world.

Step 5: Access your Flask App Online

Once Ngrok is running, you can access your Flask app from anywhere by visiting the URL that Ngrok provided. For example, if Ngrok displayed the URL

https://c7d99d3c.ngrok.io, you can access your Flask app by visiting https://c7d99d3c.ngrok.io/ in your web browser.

Uses of Running Python Flask app using Ngrok

Running a Python Flask app using Ngrok has a wide range of applications in various scenarios. Some examples are −

  • Testing − When developers are working on a Flask application, they may want to test it on different devices to ensure that it works correctly and without any issues. Running a Flask app using Ngrok allows developers to test their applications on different devices, including mobile devices, without having to deploy the app to a remote server.

  • Demonstrating − Flask is a popular framework used for building web applications, and sometimes developers want to showcase their work to clients or stakeholders. Running a Flask app using Ngrok makes it easy to demonstrate the app to others without having to deploy it to a remote server.

  • Debugging − When developing a Flask application, developers may encounter errors or bugs that are difficult to reproduce locally. Running a Flask app using Ngrok allows developers to share the URL with other developers who can access the application remotely and help debug the issue.

  • Prototyping − Flask is a flexible framework that can be used for developing different types of applications. Running a Flask app using Ngrok allows developers to quickly prototype and test new features without having to deploy the app to a remote server.

  • Integration testing − Flask applications often interact with other third-party services, such as APIs. Running a Flask app using Ngrok allows developers to test the integration with third-party services in a real-world scenario.

Conclusion

In this article, we have discussed Running Python Flask App online using Ngrok. Using the Ngrok tool we can showcase our Flask app to anyone in the world without hosting it on any server. Ngrok simply exposes your local web server to the internet, making it ideal for testing and showcasing to anyone.

Updated on: 11-Jul-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements