Hello World

I was thinking of doing something with the web so decided to start from scratch and tried a hello world program using Flask.

Basically, a single file is required named as app.py to perform the task which is as follows:

# app.pyfrom flask import Flask           # import flask
app = Flask(__name__) # create an app instance

@app.route("/") # at the end point /
def hello(): # call method hello
return "Hello World!" # which returns "hello world"
if __name__ == "__main__": # on running python app.py
app.run() # run the flask app

To run the application, use the following command on terminal:

python app.py
Output after running the app

On hitting the URL from your browser you will see the following:

Output on the browser

Adding name with Hello World

@app.route("/Talha")              # at the end point /<name>
def hello_name(): # call method hello_name
return "Hello World! I am Talha"

Let’s hit http://localhost:5000/Talha. we will get a 404 error.

This is because we are running the server in a production mode. For development purposes, we use something called as debug mode. When debug=Trueis set the server restarts as we add new code to our Flask Application. In order to set the debug mode do the following

  1. Modify the line app.run() to app.run(debug=True).
  2. Stop the running server and restart it again.

You will see “debugger is active” which means that the debug mode has taken effect. Now you can go on and edit your file as much as you want, and the server will be restarted.

After running in development mode

Let’s hit http://localhost:5000/Talha. we will get the following:

Output on the browser after code modification

Code can be accessed using this repository.

References

--

--

--

MS Thesis Student, CVGL, LUMS http://pk.linkedin.com/in/talhahanifbutt

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Site Reliability Engineering

Tools hung on a wall

What to expect from the initial UI refresh rollout

[TryHackMe]- Ignite Walk-Through

Linear Probing Method in Hashing

Video Chat app using Gatsby and twilio-1

Creating a simple URL shortener using Flutter and Firebase

Retro time isn’t over yet!

I’m joining #100DaysOfCode (and why you should too)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Talha Hanif Butt

Talha Hanif Butt

MS Thesis Student, CVGL, LUMS http://pk.linkedin.com/in/talhahanifbutt

More from Medium

How ML Hybrid Parser Beats Traditional Parser

Deploying and Monitoring Made Easy with TeachableHub and WhyLabs

Using TensorFlow 1.x on Ampere architecture GPUs (i.e. RTX 30 series)

Start to work quickly with GPUs in Python for Data Science projects.