Rapid Web Development with Python for Absolute Beginners
31. Oct 2015•0 gefällt mir•1,391 views
Downloaden Sie, um offline zu lesen
Melden
Software
This slide covers Python basics, Python key features, Web development basis, RESTful architecture key points, Agile Web Development, Python web framework basis and fundamentals.
2. Rapid Web Development w/ Python
Python is, What? You know already, since you're hereWhat does make Python different?
2
Software Quality
Readability, coherence, reusable, deep support
Developer Productivity:
Less to type, less to debug, runs immediately
Program Portability
Moves between all major platforms, write once run more, easy-to-use package
management, own interpreter
Support Libraries
A large set of prebuilt libraries, homegrown libraries and 3rd party libraries too.
3. Python Basics
What can We do with Python?
Systems Programming
GUIs
Internet Scripting
Database Programming
Rapid Prototyping
Numeric & Scientific Programming
Gaming, Images
Web Services
AI
Serial Port COM.
4. Rapid Web Development w/ Python
Python BasicsSystems Programming
4
Sockets Files Processes
#create an INET, STREAMing socket
serversocket = socket.socket(
socket.AF_INET, socket.SOCK_STREAM)
#bind the socket to a public host,
# and a well-known port
serversocket.bind((socket.gethostname(), 80))
#become a server socket
serversocket.listen(5)
# Write config to file
conf_file = open(self.system_config_file,
"wb")
config_parser.write(conf_file)
conf_file.close()
# Read file by ‘with open’
with open(‘config.gile’, ‘r’) as f:
rd = f.read()
# import multiprocessing package
from multiprocessing import Process
def pr(name):
print ‘hello’, name
if __name__ == ‘__main__’:
p = Process(target=f, args(‘bob’,))
p.start() # run method pr by process
p.join() # ends process
# further details https://docs.python.org/2/
library/multiprocessing.html
5. Rapid Web Development w/ Python
Python BasicsGUIs
5
#gui
Python comes with a standard object-oriented interface called tkinter
from Tkinter import *
class Application(Frame):
def say_hi(self):
print "hi there, everyone!"
def createWidgets(self):
self.QUIT = Button(self)
self.QUIT["text"] = "QUIT"
self.QUIT["fg"] = "red"
self.QUIT["command"] = self.quit
self.QUIT.pack({"side": "left"})
self.hi_there = Button(self)
self.hi_there["text"] = "Hello",
self.hi_there["command"] = self.say_hi
self.hi_there.pack({"side": "left"})
def __init__(self, master=None):
Frame.__init__(self, master)
self.pack()
self.createWidgets()
root = Tk()
app = Application(master=root)
app.mainloop()
root.destroy()
6. Python comes with standard Internet
modules that allow Python programs
to perform a wide variety of
networking tasks, in client and server
modes.
A large collection of 3rd party tools
are available for doing Internet
programming in Python.
Some of Web development
framework packages:
• Django
• Flask
• Plone
• Zope
• TurboGears
• Pyramid
• Web2py
Python BasicsInternet Scripting & Web Services
6
6
7. Rapid Web Development w/ Python
Python BasicsDatabase Programming
7
PEP 0249
There are Python interfaces to all commonly used
relational database systems such as Sybase,
Oracle, ODBC, MySQL, PostgreSQL, SQLite and
more.
Recently, Python gets some other interfaces for
NoSQL databases such as MongoDB, Cassandra,
CouchDB, ElasticSearch.
Pick the best fit for your project(s) and need(s).
Professional Skills
psycopg2
sqlalchemy
sqlite3
django-orm
Rating: 4 of 5 stars
Python
Database API
Specification
# Write config to file
db = engine()
sess = session_maker()
sess.connect()
sess.save()
sess.commit()
sess.rollback()
sess.close()
DatabaseError
InterfaceError
ProgrammingError
8. Rapid Web Development w/ Python
Python Key Features8
Dynamic Typing: Python keeps track of the kinds of objects uses when the program runs
Automatic Memory Management: Python automatically allocates objects and collect garbage
when it is done already.
Programming-In-The-Large Support: Python includes tools such as modules, classes and
exceptions. These modules allow us to organise systems into components.
Built-in Object Types: lists, tuples, dictionaries and more…
Built-in Tools: Concatenation, slicing, sorting, mapping, filtering and more…
Library Utilities & 3rd Party Libs: Python contains a large set of libraries either built-in and 3rd
party for complex, logical, scientific, security, internet operations and more…
9. Easy to learn for
newbies.
Programming
Language seems
designed
Best for Small task
with the help of
predefined and
keywords and
commands.
Asynchronous coding
Multiparadigm
approach
Great Object Oriented
Approach
cleaner Syntax
Everything is an object
Force programmer to
follow certain
convention
Codes wont work if
indentation is incorrect
Speed can be an issue
Design restrictions
Encoding, UTF-8 is a
damn headache
Python is not in Web
browsers.
Python is hard to
secure.
Python global
interpreter lock,
means only one
thread can access
Python internals at a
time.
10. Web applications are business strategies and policies implemented on the Web through the use of User,
Business and Data services.
ServerClient
Get me something
Do something
Set something
Delete something
200 OK
400 Bad Request
404 Not Found
500 Internal Server Error
11. Rapid Web Development w/ Python
Web Application DevelopmentKey-Points
11
3 tiers: User Services, Business Services, Data Services.
The User Service tier creates a visual gateway for the
consumer to interact with the application. This can range
from basic HTML and DHTML to complex COM
components and Java applets.
Business Services tier can range from Web scripting in
ASP/PHP/JSP to server side programming such as TCL,
CORBA and PERL, PYTHON, that allows the user to
perform complex actions through a Web interface.
Data services store, retrieve and update information at a
high level. Databases, file systems, and writeable media
are all examples of Data storage and retrieval devices.
Ref: http://www.sitepoint.com/development-guide-success/
Client-Server Topology
HTML, JS, CSS
Back-end Services, Cache
Template Rendering
Model-View-Controller
Cross-Browsers(deprecated)
Mobile-First approach / UX
12. Bootstrap
jQuery
Angular
Twig
Jinja
WTForms
Define your model
which will be stored in
Database, filesystem,
media server
whatever…
Make your Web
application rich with
Javascript libraries,
template engines,
mobile-first designs…
NAME
SURNAME
DOB
GENDERModel
View
Controller APP
13. RESTful Architecture - Representational State Transfer
CORBA
WSDL / SOAP
XML
REST
Web had become so pervasive in the past 18
years.
programmers started to realize that they
could use the concepts of REST to build
distributed services and model service-
oriented architectures (SOAs).
The idea of SOA is that application
developers design their systems as a set of
reusable, decoupled, distributed services
Nowadays, though, when you think of SOA,
you think of SOAP-based web services.
14. Rapid Web Development w/ Python
RESTful Web ArchitectureDistributed - Stateless - Familiarity - Interoperability - Scalability
14
The idea behind it is that you stick to the
finite set of operations of the application
protocol you’re distributing your services
upon. This means that you don’t have an
“action” parameter in your URI and use
only the methods of HTTP for your web
services.
The Uniform, Constrained Interface Addressability
HATEOAS
Representation-Oriented
HTTP content negotiation is a very powerful
tool when writing web services. With the
Accept header, a client can list its
preferred response formats. Ajax clients can
ask for JSON, Java for XML, Ruby for YAML.
Another thing this is very useful for is
versioning of services.
In the REST world, addressability is
managed through the use of URIs. When
you make a request for information in
your browser, you are typing in a URI.
Each HTTP request must contain the URI
of the object you are requesting
information from or posting information
to.
The final principle of REST is the idea of
using Hypermedia As The Engine Of
Application State (HATEOAS).
Hypermedia is a document-centric
approach with the added support for
embedding links to other services and
information within that document format.
15. Agile web development is not a specific process,
action, or a daylong exercise.
Agile is a mindset, an attitude with which a project is
undertaken.
Agile Web Projects w/ Python
16. Rapid Web Development w/ Python
Agile Web Development w/ PythonAgile Basics
16
Individuals
Interactions
Working
Software
Customer
Collaboration
Responding to
Change
self-organization and
motivation are important, as
are interactions like co-location
and pair programming.
working software is more
useful and welcome than just
presenting documents to
clients in meetings.
requirements cannot be fully
collected at the beginning of
the software development
cycle, therefore continuous
customer or stakeholder
involvement is very
important.
agile methods are focused
on quick responses to
change and continuous
development.
17. Where is Python on the cycle
Development Speed
Since Python, let developers develop
fast, rapidly test for environment-free,
deliver quickly by easy-packaging. That
is completely what Agile wants, isn’t it?
Software Quality
Sometimes, making something fast
causes some unexpected
consequences, thus our goal may be far
away where we are now. Python, keeps
quality for us
Rich Library Support
Main goal is speed in Agile, and
customer requirements never end.
Python built-in & 3rd party libs help us to
no need to discover the world from the
scratch
.py
18. Rapid Web Development w/ Python
Agile Web Development w/ PythonComparison between traditional & Agile
18
Company B2 Company C3 Others4
Ref: http://sixrevisions.com/web-development/agile/
19. Some of Agile Manifesto Principles
Early and continuous delivery Sustainable Development
Face-to-face conversationWelcome changing requirements
Early and continuous delivery
helps us to keep customer(s),
POs involved in the development
cycle.
Software lifecycle never ends
but you complete tasks and
deliver early micro-products
before milestones.
Since, we have not done yet with
the development, we can easily
adapt any change on
requirement to the progress.
Communication is the heart of
teams and it helps the team to
make sure about every single
issue.
20. Rapid Web Development w/ Python
Agile Web Development w/ PythonPython - Libs - Frameworks - Rapid Development
20
Python has own unique features,
so do Agile! Since both of these two aim
same goals and benefits, we can mix’s into a
single progress and improve acceleration,
quality, stability and reliability of what we do.
I know kung-fu
We know Agile
We know Web
We know Python
What could we do
with all of these?
22. No spaceship but:
Dashboards
Monitoring systems
Facebook
Twitter
Youtube
Scientific softwares
NASA uses it already!!!
Google uses too !!!
Accounting software
ie: Freshbooks
CRUD systems
Cloud Systems
Do it your own Amazon!
23. Rapid Web Development w/ Python
Python-Web-FrameworksA little about popular ones.
23
django
Tornado
Full-Stack Frameworks
Falcon
CubicWeb
Reahl
Zope2
Aiohttp
Bottle
Micro Frameworks
Flask
Pyramid
Muffin
Wheezy Web
24. Rapid Web Development w/ Python
Python Web Frameworks24
The Web framework for perfectionists with
deadlines
Django's "batteries included" approach
makes it easy for developers who know
Python already to dive in to web
applications quickly without needing to
make a lot of decisions about their
application's infrastructure ahead of time.
Django has for templating, forms, routing,
authentication, basic database
administration, and more built in.
Django Falcon
Flask
Falcon is a ridiculously fast, minimalist
Python web framework for building cloud
APIs and app backends.
The Falcon web framework encourages
the REST architectural style, meaning
(among other things) that you think in
terms of resources and state transitions,
which map to HTTP verbs.
Flask is a "microframework" primarily
aimed at small applications with simpler
requirements.
Though Flask has a shorter history, it has
been able to learn from frameworks that
have come before and has set its sights
firmly on small projects. It is clearly used
most often in smaller projects with just
one or two functions.
My picks :)
25. Rapid Web Development w/ Python
Python Web FrameworksStart your project: Django
25
$ django-admin startproject mysite
$ python manage.py migrate
$ python manage.py runserver
$ Performing system checks...
0 errors found
October 31, 2015 - 15:50:53
Django version 1.8, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Sites using Django
Disqus
Instagram
The Guardian
Knight Foundation
MacArthur Foundation
Mozilla
National Geographic
Open Knowledge
Foundation
Pinterest
NASA
Open Stack
26. Rapid Web Development w/ Python
Python Web FrameworksStart your project: Falcon
26
# sample.py
import falcon
import json
class QuoteResource:
def on_get(self, req, resp):
"""Handles GET requests"""
quote = {
'quote': 'I've always been more interested in the future than in the past.',
'author': 'Grace Hopper'
}
resp.body = json.dumps(quote)
api = falcon.API()
api.add_route('/quote', QuoteResource())
Features
• Highly-optimized,
extensible code base
• Intuitive routing via URI
templates and resource
classes
• Easy access to headers and
bodies through request
and response classes
• Does not use WebOb
(some of us do indeed
consider this a feature)
• Idiomatic HTTP error
responses via a handy
exception base class
• DRY request processing
using global, resource, and
method hooks
• Snappy unit testing
through WSGI helpers and
mocks
• CPython 2.6/2.7, PyPy,
Jython 2.7, and CPython
27. Rapid Web Development w/ Python
Python Web FrameworksStart your project: Flask
27
import flask
from flask import Flask
from flask import render_template
from flask import request
from flask import session
from helper.Resources import Resources, utilities
from controller import Controller
app = Flask(__name__)
@app.route('/')
def index():
try:
controller = Controller()
title = Resources.APP["title"]
tasks = controller.get_task_list()
users = controller.get_users_list()
return render_template(“index.html", title=title, tasks=tasks, users=users)
except BaseException as exception:
print exception.message
if __name__ == '__main__':
app.run(host=Resources.APP["host"],
port=Resources.APP["port"],
debug=Resources.APP["debug"])
Features
• 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
28. Rapid Web Development w/ Python
Python Web FrameworksA Kindly Benchmark
28
Ref: http://klen.github.io/py-frameworks-bench/#results
29. Rapid Web Development w/ Python
•Python Web FrameworksMake your own choice based on what you need
29
Light Rich FeatureSpeed
30. Rapid Web Development w/ Python
Python Web FrameworksPicking Strategy
30
MVC
module-based
Aggregate support unit tests asset/package
management
multilingual &
localisation
ORM support documentation &
Long-term support
31. Rapid Web Development w/ Python
Develop-Test-Deploy31
Development
More test
Test
Deploy
After we made our Web projects we need to deliver it, but
Deploy your project by packing with setup.py, run it on a Web server with WSGI,
Apache, nginx. gunicorn…
Since, we work as Agile and we deliver early & often, keep testing so far.
First, we have to test it, even start with testing first!
32. Rapid Web Development w/ Python
Develop-Test-Deploy32
import unittest
import sys
import os
# import Statistics class to call its method and test them.
from app import Statistics
class TestStatistics(unittest.TestCase):
"""
Test statistics app tests
"""
statistics = Statistics()
malformed_host_file = PARENT_DIR + "/statistics/tests/data/HostState.txt"
malformed_instance_file = PARENT_DIR + "/statistics/tests/data/InstanceState.txt"
def testHostFileFormer(self):
"""
Check file format is former or malformed
@return void
"""
with open(self.statistics.host_file) as file_to_test:
self.assertTrue(self.statistics.check_file(file_to_test.read()))
def testInstanceFileFormer(self):
"""
Check file format is former or malformed
@return void
"""
with open(self.statistics.instance_file) as file_to_test:
self.assertTrue(self.statistics.check_file(file_to_test.read()))
def testMalformedHostFile(self):
"""
Check file format is former or malformed
@return void
"""
with open(self.malformed_host_file) as file_to_test:
self.assertFalse(self.statistics.check_file(file_to_test.read()))
def testMalformedInstanceFile(self):
"""
Check file format is former or malformed
@return void
"""
with open(self.malformed_instance_file) as file_to_test:
self.assertFalse(self.statistics.check_file(file_to_test.read()))
def testWriteTarget(self):
"""
Check if targeted file is written
@return void
"""
dummy_content = ["HostClustering: 8, 0.75","DatacentreClustering: 8, 0.36","AvailableHosts: 3,2,5,10,6"]
self.assertTrue(self.statistics.write_target(dummy_content))
def run():
suite = unittest.TestLoader().loadTestsFromTestCase(TestStatistics)
unittest.TextTestRunner(verbosity=2).run(suite)
if __name__ == "__main__":
suite = unittest.TestLoader().loadTestsFromTestCase(TestStatistics)
unittest.TextTestRunner(verbosity=2).run(suite)
Test Sample
33. Rapid Web Development w/ Python
Develop-Test-Deploy33
from setuptools import setup, find_packages
from codecs import open
from os import path
here = path.abspath(path.dirname(__file__))
with open(path.join(here, 'README.md'), encoding='utf-8') as f:
long_description = f.read()
setup(
name='basic-todo',
version='1.0.0',
description='A sample Python project',
long_description=long_description,
url='https://github.com/fatihzkaratana/basic-todo',
author='Fatih Karatana',
author_email='fatih@karatana.com',
license='MIT',
classifiers=[
'Development Status :: 3 - Alpha',
'Intended Audience :: Developers',
'Topic :: Software Development :: Build Tools',
'License :: OSI Approved :: MIT License',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.6',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.2',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
],
keywords='sample flask restful web application development',
packages=find_packages(exclude=['contrib', 'docs', 'tests']),
install_requires=['Flask>=0.10'],
extras_require={
'dev': ['check-manifest'],
'test': ['coverage'],
},
package_data={},
data_files=[('tasks', ['db/tasks.json']),('users', ['db/users.json'])],
entry_points={
'console_scripts': [
'basic-todo=app:main',
],
},
)
setup.py
36. DEMO Time
Cabin crew take-off positions please!
https://github.com/fatihzkaratana/basic-todo
37. Rapid Web Development w/ Python
CredentialsActually, something about me :)
37
Labris Networks
ODTU Teknokent
Galyum Blok K.1 N.1
fatih[/at/]karatana.com
twitter.com/fatihzkaratana github.com/fatihzkaratana
Fatih Karatana, Computer Engineer
Software Architect & Data Visualization Team Lead at Labris Networks
Current
Labris Networks, Ankara
Previous
Turksat A.S., Ankara
High Level Software, Lefkoşa
Outsource Software Ltd, Gazimağusa
Innovia Digital, Lefkoşa
Education
Cyprus International University