Python and the Web
Plone Conference 2016
@brandon_rhodes

The Social Network (2010)

“We don’t even know
what it is yet.”
know.png
“We don’t even know
what it is yet.”
python-logo-inkscape.svg

Web → ← Python

python-logo-inkscape.svg

What does Python offer?

Reflection
Object oriented
Dynamic
Simple

Reflection

A program can see
itself in the mirror
dir()
getattr()
type()
import cherrypy

class HelloWorld(object):
    def index(self):
        return "Hello World!"
    index.exposed = True
views = {}
for name, object in cls.__dict__.items():
    exposed = getattr(object, 'exposed', False)
    if exposed:
        views[name] = object

Object oriented

= subclassing

# "Object based": hides complexity

def monitor():
    ...

t = threading.Thread(target=monitor)
# "Object oriented": subclassing

class MonitorThread(threading.Thread):
    def run(self):
        ...
Object oriented: you get
invited to write new methods
for existing classes

(And, it eats a level of indentation)

Dynamic

Code objects — modules,
classes, functions — can
change at runtime

What error should this code raise?

# "from framework import request"?

def my_view():
    return (
        'Hi, you sent {} cookies.'
        .format(len(request.cookies))
    )
# framework.py

...
    # Magically create a global!
    views_module.request = Request(...)
...
In a dynamic language,
you can wind up with code and
objects that don’t even exist
in your raw source code
Q: Does it make your
code more readable?

Simple

“Things should be as simple
as possible, but no simpler.”
— Einstein, quoted in
“every company gets about
three innovation tokens”
— Dan McKinley,
“MySQL is boring.
Postgres is boring.
PHP is boring.
Python is boring.”
Q: What makes Python
wonderful for the web?
Reflection
Object oriented
Dynamic
Simple
Q: What makes Python
wonderful for the web?
Reflection
Object oriented
Dynamic
Simple
Django
Flask
Bottle
Pyramid
Morepath
Flask 23,132
Django 21,712
Bottle 4,038
Pyramid 2,122
Morepath 263
Flask 23,132
Django 21,712

Bottle 4,038
Pyramid 2,122

Morepath 263

Flask 23,132
Django 21,712
web.py 4,089
Bottle 4,038
Pyramid 2,122
web2py 1,218
Morepath 263
CherryPy 201
What do the
top frameworks
have in common?
Flask 23,132
Django 21,712
web.py 4,089
Bottle 4,038

Flask and Django

Django

Python’s default first framework
ORM: persist data in a database
Admin interface: database visible
Forms library

Forms library!?

Multi-trailer systems

two-trailers.png
Trade-off: yield control
in return for features

Forms library

Flask

The go-to second framework
Will people keep writing
Python for the web?
What’s the competition?

JavaScript

Python — 2 vs 3, C Python vs PyPy
JavaScript — Node.js, ES6 → ES5

Python

reflection
generators
iterators
classes
modules

JavaScript

reflection
generators
iterators
classes
modules

It does now

ES6

reflection
generators
iterators
classes
modules
“Everything that rises
must converge”

JavaScript

> 40 - '30'

JavaScript

> 40 - '30'
10

JavaScript

> 40 - '30'
10

> 40 + '30'

JavaScript

> 40 - '30'
10

> 40 + '30'
"4030"

Python

>>> 40 - '30'
Traceback (most recent call last):
  ...
TypeError: unsupported operand type(s)

TypeScript

TypeScript → ES5!

JavaScript




JavaScript

Dynamic,
High-level,
Open source!

Q

What will happen
to Python?
Python has always
played 2nd fiddle to
some other web language
PHP, VB Script, Ruby, JS
What are Python’s advantages?
Becoming the world’s
default language
Often the language
learned in school
Simple syntax is perfect for
the “occasional programmer”

Burgeoning community

girls.png
The Web and Python met
when both were immature

Web → ← Python

“We don’t even know
what it is yet.”
know.png
“We don’t even know
what it is yet.”

Thank you!

@brandon_rhodes