rapid web development with python for absolute beginners

38
Rapid Web Development w/ Python for Absolute Beginners

Upload: fatih-karatana

Post on 09-Jan-2017

872 views

Category:

Software


4 download

TRANSCRIPT

Page 1: Rapid Web Development with Python for Absolute Beginners

Rapid Web Development w/ Pythonfor Absolute Beginners

Page 2: Rapid Web Development with Python for Absolute Beginners

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.

Page 3: Rapid Web Development with Python for Absolute Beginners

Python BasicsWhat 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.

Page 4: Rapid Web Development with Python for Absolute Beginners

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

Page 5: Rapid Web Development with Python for Absolute Beginners

Rapid Web Development w/ Python

Python BasicsGUIs

5

#guiPython comes with a standard object-oriented interface called tkinterfrom 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()

Page 6: Rapid Web Development with Python for Absolute Beginners

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

66

Page 7: Rapid Web Development with Python for Absolute Beginners

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

PythonDatabaseAPISpecification

# Write config to file db = engine()

sess = session_maker()

sess.connect()

sess.save() sess.commit()

sess.rollback() sess.close()

DatabaseError InterfaceError

ProgrammingError

Page 8: Rapid Web Development with Python for Absolute Beginners

Rapid Web Development w/ Python

Python Key Features8

Dynamic Typing: Python keeps track of the kinds of objects uses when the program runsAutomatic 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…

Page 9: Rapid Web Development with Python for Absolute Beginners

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.

Page 10: Rapid Web Development with Python for Absolute Beginners

Web applications are business strategies and policies implemented on the Web through the use of User, Business and Data services.

ServerClient

Get me somethingDo somethingSet somethingDelete something

200 OK400 Bad Request404 Not Found500 Internal Server Error

Page 11: Rapid Web Development with Python for Absolute Beginners

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

Page 12: Rapid Web Development with Python for Absolute Beginners

BootstrapjQueryAngularTwigJinja

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

GENDERModelView

Controller APP

Page 13: Rapid Web Development with Python for Absolute Beginners

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.

Page 14: Rapid Web Development with Python for Absolute Beginners

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

HATEOASRepresentation-OrientedHTTP 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.

Page 15: Rapid Web Development with Python for Absolute Beginners

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

Page 16: Rapid Web Development with Python for Absolute Beginners

Rapid Web Development w/ Python

Agile Web Development w/ PythonAgile Basics

16

IndividualsInteractions

Working Software

Customer Collaboration

Responding toChange

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.

Page 17: Rapid Web Development with Python for Absolute Beginners

Where is Python on the cycleDevelopmentSpeed

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 QualitySometimes, 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 SupportMain 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

Page 18: Rapid Web Development with Python for Absolute Beginners

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/

Page 19: Rapid Web Development with Python for Absolute Beginners

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.

Page 20: Rapid Web Development with Python for Absolute Beginners

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-fuWe know Agile

We know Web

We know Python

What could we do with all of these?

Page 21: Rapid Web Development with Python for Absolute Beginners

A spaceship to Mars!

Page 22: Rapid Web Development with Python for Absolute Beginners

No spaceship but:Dashboards

Monitoring systemsFacebook

TwitterYoutube

Scientific softwaresNASA uses it already!!!

Google uses too !!!

Accounting softwareie: FreshbooksCRUD systemsCloud Systems

Do it your own Amazon!

Page 23: Rapid Web Development with Python for Absolute Beginners

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

Page 24: Rapid Web Development with Python for Absolute Beginners

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 :)

Page 25: Rapid Web Development with Python for Absolute Beginners

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 DjangoDisqus Instagram The Guardian Knight Foundation MacArthur Foundation Mozilla

National Geographic Open Knowledge Foundation Pinterest NASA Open Stack

Page 26: Rapid Web Development with Python for Absolute Beginners

Rapid Web Development w/ Python

Python Web FrameworksStart your project: Falcon

26

#sample.py

importfalcon

importjson

classQuoteResource:

defon_get(self,req,resp):

"""HandlesGETrequests"""

quote={

'quote':'I\'vealwaysbeenmoreinterestedinthefuturethaninthepast.',

'author':'GraceHopper'

}

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

Page 27: Rapid Web Development with Python for Absolute Beginners

Rapid Web Development w/ Python

Python Web FrameworksStart your project: Flask

27

importflaskfromflaskimportFlaskfromflaskimportrender_templatefromflaskimportrequestfromflaskimportsessionfromhelper.ResourcesimportResources,utilitiesfromcontrollerimportController

app=Flask(__name__)

@app.route('/')

defindex():

try:

controller=Controller()

title=Resources.APP["title"]

tasks=controller.get_task_list()

users=controller.get_users_list()

returnrender_template(“index.html",title=title,tasks=tasks,users=users)

exceptBaseExceptionasexception:

printexception.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

Page 28: Rapid Web Development with Python for Absolute Beginners

Rapid Web Development w/ Python

Python Web FrameworksA Kindly Benchmark

28

Ref: http://klen.github.io/py-frameworks-bench/#results

Page 29: Rapid Web Development with Python for Absolute Beginners

Rapid Web Development w/ Python

•Python Web FrameworksMake your own choice based on what you need

29

Light Rich FeatureSpeed

Page 30: Rapid Web Development with Python for Absolute Beginners

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

Page 31: Rapid Web Development with Python for Absolute Beginners

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!

Page 32: Rapid Web Development with Python for Absolute Beginners

Rapid Web Development w/ Python

Develop-Test-Deploy32

importunittestimportsysimportos

#importStatisticsclasstocallitsmethodandtestthem.fromappimportStatistics

classTestStatistics(unittest.TestCase):"""Teststatisticsapptests"""statistics=Statistics()malformed_host_file=PARENT_DIR+"/statistics/tests/data/HostState.txt"malformed_instance_file=PARENT_DIR+"/statistics/tests/data/InstanceState.txt"

deftestHostFileFormer(self):"""Checkfileformatisformerormalformed@returnvoid"""withopen(self.statistics.host_file)asfile_to_test:self.assertTrue(self.statistics.check_file(file_to_test.read()))

deftestInstanceFileFormer(self):"""Checkfileformatisformerormalformed@returnvoid"""withopen(self.statistics.instance_file)asfile_to_test:self.assertTrue(self.statistics.check_file(file_to_test.read()))

deftestMalformedHostFile(self):"""Checkfileformatisformerormalformed@returnvoid"""withopen(self.malformed_host_file)asfile_to_test:self.assertFalse(self.statistics.check_file(file_to_test.read()))

deftestMalformedInstanceFile(self):"""Checkfileformatisformerormalformed@returnvoid"""withopen(self.malformed_instance_file)asfile_to_test:self.assertFalse(self.statistics.check_file(file_to_test.read()))

deftestWriteTarget(self):"""Checkiftargetedfileiswritten@returnvoid"""dummy_content=["HostClustering:8,0.75","DatacentreClustering:8,0.36","AvailableHosts:3,2,5,10,6"]self.assertTrue(self.statistics.write_target(dummy_content))

defrun():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

Page 33: Rapid Web Development with Python for Absolute Beginners

Rapid Web Development w/ Python

Develop-Test-Deploy33

fromsetuptoolsimportsetup,find_packagesfromcodecsimportopenfromosimportpath

here=path.abspath(path.dirname(__file__))withopen(path.join(here,'README.md'),encoding='utf-8')asf:long_description=f.read()

setup(name='basic-todo',version='1.0.0',description='AsamplePythonproject',long_description=long_description,url='https://github.com/fatihzkaratana/basic-todo',author='FatihKaratana',author_email='[email protected]',license='MIT',classifiers=['DevelopmentStatus::3-Alpha','IntendedAudience::Developers','Topic::SoftwareDevelopment::BuildTools','License::OSIApproved::MITLicense','ProgrammingLanguage::Python::2','ProgrammingLanguage::Python::2.6','ProgrammingLanguage::Python::2.7','ProgrammingLanguage::Python::3','ProgrammingLanguage::Python::3.2','ProgrammingLanguage::Python::3.3','ProgrammingLanguage::Python::3.4',],keywords='sampleflaskrestfulwebapplicationdevelopment',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

Page 34: Rapid Web Development with Python for Absolute Beginners

Coffee?

Page 35: Rapid Web Development with Python for Absolute Beginners

?

Page 36: Rapid Web Development with Python for Absolute Beginners

DEMO TimeCabin crew take-off positions please!

https://github.com/fatihzkaratana/basic-todo

Page 37: Rapid Web Development with Python for Absolute Beginners

Rapid Web Development w/ Python

CredentialsActually, something about me :)

37

LabrisNetworksODTU TeknokentGalyum 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

Page 38: Rapid Web Development with Python for Absolute Beginners

Thanks for participatePython Turkey