Introduction Flask

ninja hatori
Nov 7 · 5 min read

Introductuion :

Python is currently one of the most popular programming languages in the technology industry. The ease of syntax and large community is one of the reasons python is quite popular in today’s technology industry.
Along with the popularity of python, there are many packages that we can use to create the web. One way to create a website in python is to use the package flask. Flask is a microframework that we can use to build a web application.

Requirements :

  1. install python3
# install python3 in linux
sudo apt-get install python3.7

2.install pip

# install pip package manager in python
sudo apt-get install python3-pip

3.install flask with pip

# install flask
pip install flask

4.install vscode for text editor

5.install browser

Flask basic :

now you can create new project and create new environment and create hello world app with flask.

#create environment
sudo virtualenv myenv
#activate your environment
source myenv/bin/activate
#create new file
sudo touch app.py

now create new our first view function that must be written hello world.

#import package flask
from flask import Flask
#create new route
@app.route('/')
def index():
return "Hello, World!"

open in browser

http://127.0.0.1:5000/

Flask render template :

A display on a web application is written in HTML (Hyper Text Markup Language) language. In the previous flask deepening tutorial we discussed a bit about jinja. An engine that is used by flask to manipulate the html script that we will display to the user.

now create new folder templates and create new file index.html and create route for index.html.

#import package flask, render_template
from flask import Flask
#create new route
@app.route('/')
def index():
return render_template ('index.html')

in index.html you can following the code.

<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>Document</title></head><body>   <h1>hello world</h1></body></html>

open browser.

http://127.0.0.1:5000/

Flask parsing data in url :

in the flask parsing data in url There are 3 ways, namely parsing integer, string and argument.

#parsing string@app.route('/name/<string:name>')def name(name):    return "name is : {}".format(name)

open in browser.

http://127.0.0.1:5000/name/name=elliot
#parsing int@app.route('/parsing/<int:number>')def number(number):    return "number is : {}".format(number)

open in browser.

http://127.0.0.1:5000/number/100
#parsing argument@app.route('/argumentparser')def argumentparser():data = request.args.get("number")     return "what is the number parser is : {}".format(data)

open in browser.

http://127.0.0.1:5000/argumentparser?number=100

Loop in flask :

Flask is one of the web development frameworks written in Python. Through flask, a loop can be run in the HTML code using jinja template and automatically HTML code can be generated using this.

in app.py following the code.

#app.py@app.route('/')def index():     #looping     fruit = ['banana', 'strawbery', 'apple', 'mango', 'orange']     return render_template('index.html', nilai=fruit)

in index.html following the code.

#index.html<div>   <!-- for in flask with jinja2 -->   {% for data in nilai %}     <li>{{data}}</li>   {% endfor %}</div>

open in browser.

http://127.0.0.1:5000/

Condition in flask :

in app.py following the code.

#app.py@app.route('/')def index():#condition    condition = "happy"    return render_template('index.html', condition=condition)

in index.html following the code.

#index.html<!-- condition in flask with jinja2 --><div>   {% if condition == "happy" %}     <p>i am so happy</p>   {% else %}     <p>i am not happy</p>    {% endif %}</div>

open in browser.

http://127.0.0.1:5000/

Extends layout in flask :

In this flask we cover the extending capability of Flask Templates. The idea of extending is to allow for custom templates “within” other templates. So, you will generally have something like your header and footer in your main template, with things like your navbar, logo/banner, and then your footer information. Then, in the body area, you have the body data actually coming from another template that “extends” the main template.

create main_layout.html in the templates directory and create about.html.

in the main_layout.html

#main_layout.html<ul>    <li><a href="">About</a></li>    <li><a href="">Profile</a></li>    <li><a href="">Gallery</a></li>    <li><a href="">Video</a></li>    {% block content %} {% endblock %}</ul>

in the about.html.

<!-- extend layout -->{% extends "main_layout.html" %}{% block content %}   <p>about page</p>{% endblock %}

open the browser.

http://127.0.0.1:5000/about

Session in flask :

Session is yet another way to store user-specific data between requests. It works similar to cookies. To use session you must set the secret key first. The session object of the flask package is used to set and get session data. The session object works like a dictionary but it can also keep track modifications.

now you can import package session and create secret_key in the flask.

#import package session
from flask import Flask, render_template, request, session
#create secret_key
app.config['SECRET_KEY'] = "tesSession90"

now you start create route for session login, view, and logout.

in login session.

#session start@app.route('/halaman/<int:nilai>')def login(nilai):    session["nilaiku"] = nilai    return "success set session"

open in browser.

http://127.0.0.1:5000/halaman/1

in view session.

#session views@app.route('/halaman/views')def view_session():    data = session['nilaiku']    return "session set is : {}".format(data)

open in browser

http://127.0.0.1:5000/halaman/views

in logout session.

#session destroy@app.route('/halaman/logout')def logout():    session.pop('nilaiku')    return "success logut"

open in browser

http://127.0.0.1:5000/halaman/logout

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade