Creating a Python Web Application using Python Flask

Creating a Python Web Application using Python Flask

There is no better technique than using Python Flask outside of NodeJS in terms of speed, development, and flexibility when developing a web application and performing a large performance.

For the development environment, I recommend using the Ubuntu Virtual Machine hosted inside the Virtual Box. For instructions on how to install and build your own Linux virtual machines.

Visual Studio Code is used here as an IDE, other strong recommendations are the basis for Eclipse Web development with Python Addon or Pycharm Community.

You can also use AWS EC2 as your development environment, which gives you the added benefit of installing gunicorn and nginx to serve and run the page.

Also, in this, we will be setting up an AWS account, creating an IAM user that can talk to a nosql dynamo database, installing the AWS python SDK named BOTO, then importing a bootstrap theme, and using the Python Jinja framework to manage views.

Once you get your development environment set up in such a way you can run Python, whether it be Windows or the VM solution, next we need to install Flask via PIP.

This can be done by running a “pip install flask” in a Terminal. Now create a new folder for your project and open the folder in your IDE. Now we need to set up a project folder to host a simple page in the environment.


Let’s create these sub-folders

app — app module

app/controllers — these will host back end python methods in a modular way.

app/static — This creates a front-end web page.

app/templates — It hosts a file, index.html, Flask uses this directory to render pages, using the built-in template engine.

We can use it to make a server-side template, which is fine, but then we don't get the amazing power of Angular to run on the client-side.


Now let us create these files:

run.py — entry script

app / __ init__.py - It states that Python is an app module and contains  logic that runs when the module is bootstrap for the run.py</li>

app/views.py — this will be a demo view controller, which we will eventually move into the controller's directory as we build out the app

And add the code below to these files

app/__init__.py

# app/__init__.py

from flask import Flask

app = Flask(__name__)

# It's import this after creating app

# so that views can then import app

from app import views

run.py

#!/usr/bin/env python

# Look for a folder named app with the variable named app

# and provide a reference to this object

from app import app

# __Init__ py is an example of a flask object in the app

# Is a run method that will start the web application as a daemon process

app.run(debug=True)

app/views.py

from app import app

# Decorate index function with flask routing method

# routes

@app.route('/')

@app.route('/index')

def index():

return "Hello, World!" 

From Windows, you can run “python run.py”. 

Also by running our application with default=True,