Skip to content

Latest commit

 

History

History
492 lines (317 loc) · 8.17 KB

workshop5.rst

File metadata and controls

492 lines (317 loc) · 8.17 KB
title:Beginner's Workshop 5
Author: Alexander Loechel
event:PyLadies Munich - Beginner's Workshop - Web Development
keywords:Python, PyLadies, Workshop
data-transition-duration:1500
css-all:css/workshop3.css
auto-console:Yes

id:title
class:slide title-slide centered level-1
data-x:0
data-y:0

Welcome to

images/pyladies-munich.png

Beginner's workshop 5

Web Development

Note

  • Test Note

id:recap-ws1
class:slide level-1
data-x:r-2500
data-y:1000

Recap Workshop 1-4

  • What is Python
  • Setup & Tools
  • Theory

id:intro-pyramid
class:slide level-1
data-x:r+1000
data-y:1000

Web Development with Pyramid

images/pyramid-logo.png


id:step1-setup
class:slide level-1
data-x:r+0
data-y:r+1000

Step 1: Setup environment

  • Install Python 3.4
  • Check Python:
$ python3 -V
Python 3.4.x
  • Setup a Virtual Environment:
$ pyvenv trypyramid
$ cd trypyramid
$ source bin/activate
  • Install Pyramid Web Framework
$ pip install pyramid

id:step2-one-file-app
class:slide level-1
data-x:r+0
data-y:r+1000

Step 2: One File Pyramid App
Hello World

Write this into a file app.py:

 1 from wsgiref.simple_server import make_server
 2 from pyramid.config import Configurator
 3 from pyramid.response import Response
 4 
 5 def hello(request):
 6     return Response('Hello world!')
 7 
 8 if __name__ == '__main__':
 9     config = Configurator()
10     config.add_route('hello_world', '/')
11     config.add_view(hello, route_name='hello_world')
12     app = config.make_wsgi_app()
13     server = make_server('0.0.0.0', 8080, app)
14     server.serve_forever()

http://trypyramid.com/


id:step3-start-app
class:slide level-1
data-x:r+0
data-y:r+1000

Step 3: Start the app

$ python app.py

Open http://127.0.0.1:8080/ in Browser

Output: Hello World!


id:one-file-app
class:slide level-1
data-x:r+0
data-y:r+1000

Explanation of "One File Pyramid App"

 1 # framework imports
 2 from wsgiref.simple_server import make_server
 3 from pyramid.config import Configurator
 4 from pyramid.response import Response
 5 
 6 # hello view on route 'hello_world'
 7 def hello(request):
 8     return Response('Hello world!')
 9 
10 # Base call on file
11 if __name__ == '__main__':
12     # Base Configuration
13     config = Configurator()
14     config.add_route('hello_world', '/')
15     config.add_view(hello, route_name='hello_world')
16 
17     # Building and starting the app
18     app = config.make_wsgi_app()
19     server = make_server('0.0.0.0', 8080, app)
20     server.serve_forever()

id:excurses
class:slide level-1
data-x:r+1500
data-y:1000

Excurses

  • Philosophy
    • Be lazy - Don't repeat yourself - Don't reinvent the wheel
    • Standing on the shoulders of giants
    • Test Driven Development & Documentation
  • Tools
    • git
    • Python Eggs

id:phil
class:slide level-1
data-x:r-500
data-y:r+1000

Philosophy
three great virtues of a programmer

  • Laziness: The quality that makes you go to great effort to reduce overall energy expenditure. It makes you write labor-saving programs that other people will find useful and document what you wrote so you don't have to answer so many questions about it.
  • Impatience: The anger you feel when the computer is being lazy. This makes you write programs that don't just react to your needs, but actually anticipate them. Or at least pretend to.
  • Hubris: The quality that makes you write (and maintain) programs that other people won't want to say bad things about.

—"Programming Perl", Larry Wall, 2nd Edition, O'Reilly, 1996


id:dry
class:slide level-1
data-x:r+0
data-y:r+1000

Philosophy
DRY - Don't repeat yourself

Don't repeat yourself

Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

—Andy Hunt, Dave Thomas; The Pragmatic Programmer

DRY vs WET

  • DRY: Don't repeat yourself
  • WET: write everything twice / we enjoy typing

It applies for one program and also to best practice of frameworks


id:reinvent
class:slide level-1
data-x:r+0
data-y:r+1000

Philosophy
Don't reinvent the wheel

To reinvent the wheel is to duplicate a basic method that has already previously been created or optimized by others.
  • Why should I write and maintain code that others alrady have developed
  • Using Best practices
  • Standing on the shoulders of giants - we don't have to reinvent the wheel, we use it

id:tests
class:slide level-1
data-x:r+0
data-y:r+1000

Test Driven Development & Documentation

  • Specification
  • Testing of Specification
  • Documentation

id:git
class:slide level-1
data-x:r+1000
data-y:2000

Tool: GIT

git: a distributed version control system

  • git init
  • git clone url
  • git status
  • git add
  • git rm
  • git commit
  • git checkout
  • git branch
  • git push

id:eggs
class:slide level-1
data-x:r+0
data-y:r+1000

Python Eggs/Wheels

Eggs/Wheels are the Python Packaging Container


id:scaffolds
class:slide level-1
data-x:r+0
data-y:r+1000

Scaffolds / Templates / Skeletons

Concept of Code Generation to bootstrap software components


id:normal-pyramid-app
class:slide level-1
data-x:r+1000
data-y:1000

Starting a Pyramid App

Steps:

  1. Virtual Environment (pyvenv)
  2. install pyramid
  3. use scaffold to create base app / egg structure
  4. check into VCS
  5. start app in development mode and start developing

id:venv
class:slide level-1
data-x:r+0
data-y:r+1000

Virtual Environment

$ pyvenv base_app
$ cd base_app
$ source bin/activate

Install Pyramid

$ pip install pyramid

id:bootstrap
class:slide level-1
data-x:r+0
data-y:r+1000

Bootstrap App with scaffold

# generate app from scaffold
# (for all avaliable scaffolds see: `pcreate --list`)
$ pcreate -s starter base_app
...
$ cd base_app

# Check generated Code into VCS
$ git init .
$ git add setup.py README.txt base_app/
$ git commit -m "initial version from scaffold"

# build app
$ python setup.py develop

id:start
class:slide level-1
data-x:r+0
data-y:r+1000

Start App

$ pserve --reload development.ini
Starting subprocess with file monitor
Starting server in PID xxxx.
serving on http://0.0.0.0:6543

open http://127.0.0.1:6543/ in Browser


id:test_app
class:slide level-1
data-x:r+0
data-y:r+1000

Test App

$ python setup.py test

id:next-meeting
class:slide centered level-1
data-x:0
data-y:6000

Next Workshop

images/pyladies-munich.png

Thursday June 25th 2015 18:30

Web Development
Templating / Schema / Forms


id:overview
data-x:0
data-y:3000
data-scale:8