Kensuke Kousaka's Blog

Notes for Developing Software, Service.

Note about Flask: Intro

Write down a note about Flask, Pyhton based lightweight Web Application Framework.

Build Flask environment

First, build environment to use Flask.

Build Python environment

If you don't install python yet, run folloging command to install Python 3.x.

# pacman -S python

And also install pip by running following commands.

$ curl -O
# python

Install Flask

Run follow command to install Flask using pip.

# pip install Flask

Use Flask

Create directory structure by following.

- HelloFlask/
  - FlaskApp/
    - static/
      - css/
        - sample.css
      - js/
        - sample.js
    - templates/
      - index.html

Create index.html, sample.js, sample.css by following.


<!DOCTYPE html>
<html lang="en">
   <meta charset="utf-8"></meta>
   <meta http-equiv="X-UA-Compatible" content="IE=edge"></meta>
   <meta content="width=device-width, initial-scale=1" name="viewport"></meta>
   <!-- Bootstrap CDN -->
   <link rel="stylesheet" href="">
   <link rel="stylesheet" href="">

   <link rel="stylesheet" href="../static/css/sample.css">
   <div class="container-fluid">
     <div class="row">
       <div class="col-sm-12">
         <h1 class="text-center" id="hello">Hello Flask</h1>
         <button class="btn btn-default" id="button">Change text</button>
   <script src=""></script>
   <script src=""></script>
   <script src="../static/js/sample.js"></script>



function init() {
  $("#button").click(function() {


div.container-fluid {
  text-align: center;

#hello {
  background-color: #F8F4E6;

Implement by following.

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from flask import Flask, render_template

app = Flask(__name__)

def index():
    return render_template('index.html')

if __name__ == '__main__':'', port=8080)

After editting, run python in the same place as Succeeded to run Flask if you got Running on (Press CTRL+C to quit) message.

Now, Flask is received request and return contents that previously created when connect to that URL with Web browser.

Change app host parameter that is defined in from to if you want to publish this system to the Internet.

This sample app is published at k3nsuk3/HelloFlask.