Get started with Flask Python

Get started in Web development with Python Flask 

 

    Python is known as a programming language used in data science and machine learning and A.I . But we can use Python in web development by using one its framework like Flask Django FastApi …  


    So today we are going to start learning how to use Python (Flask) to build a website
    So lets started  
1) First we need to install Flask module by typing in the command line :
$ pip install Flask or $ pip3 install Flask
2) we create a folder for our first website then we go into this folder and create a python-file like app.py
3)we open this file on our text editor or ide (vscode,vim,pycharm...)
4 . Alright now let's write some code
A- we need to import the flask module by :

From flask import Flask 

B- we create an instance to initialize our application :
app = Flask(__name__)

You can change ‘app’ to anything you want
C- we create a route for the index page :
@app.route(‘/’)

This is the url we are going to control (‘www.exemple.com/’)
D-we create a function to control this url:

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

So now if the user opens the index page the website is going to return a text ‘Hello , World!’
E- now we are going to set the host and port we want the application run on:

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

F- Full code:



From flask import Flask 
app = Flask(__name__)
@app.route('/')
def hello_world(): 
   return 'Hello, World!'
if __name__=='__main__':
    app.run()
 

You may like these posts

1 Comments

  1. maiet0044
    Thank's