How To Create Or Run Python Flask App Online Using Ngrok

Spread the love

Flask is a small framework by most standards—small enough to be called a “microframework,” and small enough that once you become familiar with it, you will likely be able to read and understand all of its source code. But being small does not mean that it does less than other frameworks. Flask was designed as an extensible framework from the ground up; it provides a solid core with the basic services, while extensions provide the rest. Because you can pick and
choose the extension packages that you want, you end up with a lean stack that has no bloat and does exactly what you need.

Python Flask is a module of Python that allows us to build up web-based applications, APIs, etc. Flask has three main dependencies. The routing, debugging, and Web Server Gateway Interface (WSGI) subsystems come from Werkzeug; the template support is provided by Jinja2, and the command-line integration comes from Click. These dependencies
are all authored by Armin Ronacher, the author of Flask.

Creating and executing applications are very simple on local machines. But if we want to create an application that we can share with other users, developers, or peers to check the behavior in real-time before deploying it to the production environment, the process becomes time-consuming.

In this article, we will create a web-based application using the Python Flask module and share the app with anyone using a public URL provided by ngrok/ flask-ngrok.

Python Flask Installation
Python Flask Installation

Installation | flask-ngrok | Python Flask

This library is compatible with Python 3.6+ versions.

pip install flask
pip install flask-ngrok

On Google-Colab/ Jupyter Notebook (In this article)

!pip install flask
!pip install flask-ngrok

Basic Structure Of Application

Initialization

All Flask applications must create an application instance. The web server passes all requests it receives from clients to this object for handling, using a protocol called Web Server Gateway Interface (WSGI, pronounced “wiz-ghee”). An application instance is an object of class Flask, usually created as follows:

from flask import Flask
app = Flask(__name__)

Please Note: The only required argument to the Flask class constructor is the name of the main module or package of the application. For most applications, Python’s name variable is the correct value for this argument.

Understand Routes

Clients such as web browsers send requests to the webserver, which in turn sends them to the Flask application instance. The Flask application instance needs to know what code it needs to run for each URL requested, so it keeps a mapping of URLs to Python functions.

The association between a URL and the function that handles it is called a route.

The most convenient way to define a route in a Flask application is through the app.route decorator which is exposed by the application instance.

The following example shows how a route is declared using this decorator:

@app.route("/")
def hello():
    return "Hello Friends! from Pykit.org. Thank you! for reading this article."

Interested in learning Python, read ‘Python API Requests- A Beginners Guide On API Python 2022‘.

Creating A Simple Hello World Application Using Flask

# import Flask from flask module
from flask import Flask

# import run_with_ngrok from flask_ngrok to run the app using ngrok
from flask_ngrok import run_with_ngrok
  
app = Flask(__name__) #app name
run_with_ngrok(app)
  
@app.route("/")
def hello():
    return "Hello Friends! from Pykit.org. Thank you! for reading this article."
  
if __name__ == "__main__":
    app.run()

Run the app by executing the Cell (or using python app_name.py).

Python Flask Pykit
Python Flask

Click on the URL (or copy and paste the URL in the new tab). A warning page will appear showing that the link may contain harmful content. Ignore this warning (because you know about this URL and its contents) by clicking on ‘visit this unsafe site’.

Python Flask Application
Python Flask Application

Congratulations! you have successfully created an application using Python’s Flask library and flask-ngrok which you can access from anywhere using the URL.

Note: The public URL will be accessible only till the time you are running the app in your system. The moment you stop or terminate the process, you will get an error message “Tunnel e1bc-34-73-108-200.ngrok.io not found ERR_NGROK_3200”.

In order to use the ngrok, we require to install ngrok and an AuthToken which we can get from here. I have explained each and every step in my Notebook file about “How to Setup Ngrok in Google Colab?”.

Github Repository

You can also build chatbots using this Python and can run the Chatbot using Flask and Ngrok. Check this article, if you want to read How to build a chatbot using NLP (NLTK)? If you are interested in projects related to NLP you can check this article on How To Build Email Spam Classification Model?

Further, if you want to explore more about Data Science and build your career as an Analytics Professional, then you can these free courses offered by top institutes and universities from all over the world.

If you want to learn more about Flask-based web applications, you can download free ebooks using these simple tricks explained here.

Conclusion

In this article, we have discussed Python Flask and successfully installed the Python Flask and flask-ngrok modules in Google Colab. Further, we have imported the modules then created a simple “Hello World” application and run the application using ngrok tunnel. We have also accessed the public URL using our browser.

Please support our effort by commenting below. Feel free to contact us in case of any queries.

Interested in making money online? Read this article on How To Make Money Online 2022: A Beginner’s Guide On NFT


Spread the love
3

Related Posts

This Post Has 6 Comments

Leave a Reply

Your email address will not be published.

© 2022 Pykit - All Rights Reserved!