Python Flask Tutorial: Build Your Flask Application - CNDRO.LLC
3162
post-template-default,single,single-post,postid-3162,single-format-standard,wp-custom-logo,theme-bridge,bridge-core-2.9.4,woocommerce-no-js,tribe-no-js,ehf-template-bridge,ehf-stylesheet-bridge-child,qode-page-transition-enabled,ajax_fade,page_not_loaded,,qode-title-hidden,qode_grid_1300,footer_responsive_adv,hide_top_bar_on_mobile_header,columns-4,qode-child-theme-ver-1.0.0,qode-theme-ver-27.8,qode-theme-bridge,qode_header_in_grid,wpb-js-composer js-comp-ver-6.7.0,vc_responsive,elementor-default,elementor-kit-2634

Python Flask Tutorial: Build Your Flask Application

Flask is a micro web framework written in Python. It is classified as a microframework because it does not require particular tools or libraries. It has no database abstraction layer, form validation, or any other components where pre-existing third-party libraries provide common functions.

Requirements

  • Python: Python must be installed on your machine, and it’s recommended you use the latest version of Python. Flask supports Python 3.7 and newer.
  • IDE: Must have an IDE to work with( VSCODE, Pycharm community IDE e.t.c.)
  • Set up a Virtual Environment: You can set up your virtual environment based on the provided OS Specifications.
1$ mkdir myproject
2$ cd myproject
3$ python3 -m venv venv
1> mkdir myproject
2> cd myproject
3> py -3 -m venv venv

 

  • Activate Virtual Environment: Before working on your project, you must activate the corresponding environment
$ . venv/bin/activate

 

> venv\Scripts\activate

 

  • Install Flask: After you have activated your environment, install flask with pip install flask

Create an Hello World Flask Application

Code

1# import our flask library
2from flask import Flask
3# create our app instance
4app = Flask(__name__)
5# we create our app route as /, we can use something else as well probably "/hello"
6@app.route("/")
7# we create our function hello here
8def hello():
9# we assign the return value which is "hello world"
10 return "Hello World!"
11# this is to automatically run the app
12if __name__ == "__main__":
13 app.run()

 

Running the Application

Web Interface

Creating HTML templates

todo
 |_ venv
 |_ app.py
 |_ templates
 |_____ index.html
1
2<!DOCTYPE html>
3<html>
4
5<head>
6 <title>My app test</title>
7</head>
8
9<body>
10<h2>Hello World</h2>
11
12<p>You're welcome to my Blog!!!.</p>
13
14</body>
15
16</html

 


Web Interface

1from flask import Flask, render_template
2
3app = Flask(__name__)
4
5
6@app.route("/")
7def index():
8 return render_template("index.html")
9
10if __name__ == "__main__":
11 app.run()

 

No Comments

Post A Comment