Python flask tutorial: Hello world

This tutorial helps you get started with Python flask. It explains what you need to write your first Python flask program. It starts with the basics: you will learn where you can find and download the tools needed, how to install these tools and it will take you all the way to building and runing your code. If you have never written a Python flask program before this is the place to start. We have created this article, so you can get started with Python flask, and move on to our sms examples. If you are already familiar with Python flask, you can jump directly to one of the following SMS projects.

Python flask sms examples:

Python flask send sms with the HTTP rest api (code sample)
Python flask send multiple sms with the HTTP rest api (code sample)
Python flask schedule sms with the HTTP rest api (code sample)
Python flask receive sms with the HTTP rest api (code sample)
Python flask delete sms with the HTTP rest api (code sample)
Download the latest Python flask sms api library from Github

What is Python flask

Python flask is a programming language. It is similar to a natural language, like English. It is used to talk to a computer. The major difference between a natural language and a programming language is that, programming languages have a more rigorous structure, to help the computer understand it better.

What is a Python flask Hello world program

The Python flask hello world program is the most simple program you can write. It simply prints out the sententce: Hello world on the computer screen. The Hello World program is the first program developers write in any programming language.

How to write your first program in Python flask

To write your first program in Python flask:

  1. Check prerequisites
  2. Setup Python
  3. Setup Flask
  4. Open Notepad
  5. Write Hello World program in Python flask
  6. Save the file with .py file extension
  7. Run your Python flask Hello World program
  8. Check the output in the browser

Prerequisites

Here is what you need to get started. To write your first computer program in Python flask, you need a Windows computer, the IDLE programming environment and example code presented below.

  • Windows 10 computer
  • Python
  • Ozeki Hello World example project

Python

Python

You may download Python flask from the following URL: https://www.python.org/downloads/

Install Python

To write your first Python flask program, you need to install Python. The following video shows you how this installation can be done.

Install Flask

To write your first Python flask program, you need to install Flask. The following video shows you how this installation can be done.

Write your 'hello world' program in Python flask

The first program you write in any programming language is the 'Hello world' program. The sole aim of this program is to print the term 'Hello world' to the computer screen. In this example the program consists of one line of code: The line: Print("") prints the text.

from flask import Flask
app = Flask(__name__)

@app.route("/")

def hello():
    return "Hello world!"

if __name__ == "__main__":
    app.run()

Write the "Hello World!" code

To write the "Hello World!" code we are going to use Notepad. This is a piece of code that returns a string data type. This variable contains the hello world message (Figure 1). The code consists of a global namespace class and and a function. First we import flask from the Flask library. The function is automatically executed without input, due to the if statement at the end.

write code
Figure 1 - Write code

Create your first Python flask project

After Python flask has been installed, you need to open it, and save the file, after you wrote the code.

Save as

To save your newly written Python flask code, first you need to click on the File menu (Figure 2). Then, click on Save as... This will bring up a new explorer window, in which you can save your code.

save as
Figure 2 - Save as

Click save

Once you have clicked save a new window pops up (Figure 3). Here you may name your file and specify the type as .py. You can also choose a directory where it is saved to. Click on Save to save your file.

save
Figure 3 - Click to save

Run the Python flask 'hello world' program

To run the hello world program, you need to press Enter.

Run the code

The first step to run your code is opening the Command Prompt (Figure 4). Change the dirctory to where your python file is located. Type in hello.py and press enter to run your code. The code will start running on your local IP address. Copy this address and paste it into your browser's URL bar to view the output of your code.

run the code
Figure 4 - Run the Python flask Hello world code

What happens if I make a mistake in Python flask

If you make a mistake, when you write your instructions in Python flask, you will get a Syntax error.

In the code below we will create a mistake intentionally by not putting a semicolon after the Hello World line. You will see, how the computer reacts, how we fix the error, and how we run the computer program successfully.

What is syntax error?

Syntax error means I don't understand. If you talk to somebody in English and he does not understand what you say, he will reply with "I don't understand". If you talk to a computer in Python flask and the computer does not understand what you say, he will reply with "Syntax error".

How to handle a syntax error in Python flask?

In the code below we will create a syntax error intentionally by not putting a semicolon after the Hello World line. You will see, how the computer reacts, how we fix the error, and how we run the computer program successfully.

Errors in Python

To demonstrate how errors occour in Python, we are going to remove a colon from the end of the if statement. If you run your code now in the Command Prompt, an error message appears (Figure 5). This message contains information about the nature of the error and where it occured. A syntax error in particular, is what we just did. Every statement has its proper syntax and will fail to run without it. These messages are helpful in identifying and fixing mistakes.

error
Figure 5 - Error

Summary

The reason why we produced this tutorial is to show you how to install the tools for using Python flask and to take you through the steps of writing and running a Hello World code. We wrote this document to introduce you the Python flask basics and to help you to understand how the Python flask programs work. Now you can use the Ozeki Python flask SMS solutions which is worth spending those few minutes to set up, because the delivery events will be presented in the forms of events, errors are handled properly so you get a transparent messaging system.

The Ozeki SMS Gateway and the offered SMS solutions are perfect for organizing SMS messages in your Python flask application. Continue reading on the Ozeki website, check out the tutorial about how to send SMS from Python flask.

Download the Ozeki SMS Gateway now and set up the system!

More information