logo


Tag: web development

Python Web Development

Introduction


pythonQuote

Web apps are often created using a framework. Frameworks make it easier to develop web apps that are scalable, reliable and maintainable. It avoids recreating the same code over and over again.
Common features are:


  • URL Routing

  • Output templates

  • Database management

  • Session management

  • Security against common attacks


A framework may offer some or all of these features.

For example, the Flask web application framework does not have database support and you would need a separate module to use a database. The Django web application framework supports databases by default.

Why use a web framework?


As you are doing web development, you want to avoid spending time on programming things that have already been solved. On the other hand, if you are an experienced web developer a web framework may not offer everything you need.

What Python web frameworks exist?


Django and Flask are the most popular web frameworks. However, you may want to evaluate the frameworks. An overview:


The most popular python web application framework is Django, followed by Flask.

python web development # of projects on Github mentioning a framework.

Django


Django is the most used Python web framework. It takes care of many things so you can focus on the web app development. Sites built withDjango have dealt with high traffic spikes such as 50 thousands hits per second.

Database access is achieved through an Object-relational mapper: You define your data models in Python and Django deals with the actual database management systems (SQL). However, if you need to you can write your own SQL Queries with Django. URL routing is supported by Django. It encourages beautiful URL design such as ending without .php or .asp.

Features:


  • Object-relational mapper

  • URLs routing and views

  • Template engine

  • Forms

  • Authentication

  • Admin

  • Internationalization

  • Security


If you want to know more about Django, read here.

Did you know the websites of NASA, Bitbucket and Pinterest were made with Django?

Flask


flask-logo
Flask is a Python micro framework which is modular by design. The framework is intended to build web apps. Flask does not have a specific database system or ORM system. If you want to use a database, you’ll have to use extensions. Flask is often combined with SQLAlchemy for database use.

Flask is very easy to get running, a minimal app would be:

from flask import Flask
app = Flask(__name__)

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

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

The framework supports URL routing, template (using Jinja2), session management and has some out of the box security.

Features:

  • URL Routing and views

  • Template engine

  • Session management

  • Logging


If you want to know more about Flask, read here.
Did you know Flask started as an April Fools joke?

Python Hosting


To run your app on the web, you will need hosting. Unless you want to do hosting yourself, you need a party to host.
Hosting servers:

Django Getting started

Django Tutorial: Building a note taking app

Flask getting started: Hello World

In this tutorial you’ll learn how to build a web app with Python.

We’ll use a micro-framework called Flask. It has a small core but is extensible with many plugins such as SQLAlchemy, Babel, CouchDB, MongoDB etc.

Some Flask example apps are:


Related course
Python Flask: Make Web Apps with Python

Why Flask?

  • easy to use.
  • built in development server and debugger
  • integrated unit testing support
  • RESTful request dispatching
  • uses Jinja2 templating
  • support for secure cookies (client side sessions)
  • 100% WSGI 1.0 compliant
  • Unicode based
  • extensively documented

Download Flask Examples

Installing Flask

Install Flask using the command below:

pip install Flask

Flask hello world app


Create a file called hello.py


from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
return "Hello World!"

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

Finally run the web app using this command:

$ python hello.py
* Running on http://localhost:5000/

Open http://localhost:5000/ in your webbrowser, and “Hello World!” should appear.

Download Flask Examples