User:Ndhanir: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
Line 132: Line 132:


[[#References|[4]]] Django vs flash vs pyramid https://www.airpair.com/python/posts/django-flask-pyramid
[[#References|[4]]] Django vs flash vs pyramid https://www.airpair.com/python/posts/django-flask-pyramid
* [[Object-Oriented Design and Programming]]

Revision as of 09:50, 6 February 2016

Web.py Introduction

Web.py is a free and open source web application framework that is as simple as it is powerful. Web.py was originally published while Aaron swartz worked at reddit.com, where the site used it ass it grew to become one of the top 1000 sites according to Alexa and served millions of daily page views. "It's the anti-framework framework. web.py doesn't get in your way." explained founder Steve Huffman.

The web.py slogan is: "Think about the ideal way to write a web app. Write the code to make it happen.". The goal of web.py is to build the ideal way to make web apps. In web.py, Instead of exposing Python objects, it allows you to build HTTP responses. Instead of trying to make the database look like an object, web.py makes the database easier to use. And instead of coming up with yet another way to write HTML, the web.py template system tries to bring Python into HTML.

Some of the sites which uses web.py are

  • Frinki, a new social network in spanish.
  • oyster.com, a website that reviews hotels uses web.py for the entire website.
  • Make History, a project of the 9/11 memorial museum.

Python

Python is a multi-paradigm programming language: object-oriented programming and structured programming are fully supported, and there are a number of language features which support functional programming and aspect-oriented programming (including by metaprogramming). Python uses dynamic typing and a combination of reference counting and a cycle-detecting garbage collector for memory management. An important feature of Python is dynamic name resolution, which binds method and variable names during program execution.

Ruby's creator, Yukihiro Matsumoto, has said: "I wanted a scripting language that was more powerful than Perl, and more object-oriented than Python. That's why I decided to design my own language."

The world of Python web frameworks is full of choices. Django, Flask, Pyramid, Tornado, Bottle, Diesel, Pecan, Falcon, web.py, web2py and many more are competing for developer mindshare. The developer needs to cut the options down to one framework depending on the type of application.

Why Web.py?

The reasons for using web.py are

  • Simplicity
  • Freedom
  • writing clean code
  • minimalism
  • a solid web framework

Installation

To install web.py,

  • Firstly, download the following tar file:
wget http://webpy.org/static/web.py-0.37.tar.gz
  • Extract the downloaded tar file:
tar -zxvf web.py-0.37.tar.gz
  • Go to web.py-0.37 directory:
cd web.py-0.37/
  • Install and make it accessible to all the applications:
sudo python setup.py install

Web.py skeleton

Features of web.py

web.py has two interesting features

Databases

The database package lets you access various of different databases. Accessing different databases refers to connecting to multiple databases. However, its not an ORM. This feature is missing in Django (another web framework) from long time. This feature is helpful for the people who are good at SQl and don't like to use SQL as it is similar to sqlite3 package but doesn't use ORM.

Forms

A forms package is present in web.py which let's us create forms and validators. Ironically, it doesn't have built-in protection against CSRF. If you want to create a login form, you can use forms package to create.

Another interesting feature about web.py is its flexibility. It has flexible modules which can be used with another framework.

Hello world example

import web
urls = (
	'/', 'index'
	)

class index:
	def GET(self):
		return "Hello, ECE517!"

if __name__ == "__main__":
	app = web.application(urls, globals())
	app.run()

If the above example is considered, then we start the application by importing the web.py module using the following command

import web

The most important part of the website is its URL structure. web.py makes it easy to make great URLs.

urls = (
  '/', 'index'
)

The first part is a regular expressions that matches a URL, like /, /help/faq, /item/, etc. The parentheses say to capture that piece of the matched data for use later on. The second part is the name of a class to send the request to, like index, view, welcomes.hello (which gets the hello ECE517 of the welcomes module), or get_\1. \1 is replaced by the first capture of your regular expression; any remaining captures get passed to your function.

Web.py's URL handling scheme is simple yet powerful and flexible. At the top of each application, you usually see the full URL dispatching scheme defined as a tuple.

urls = (
    "/tasks/?", "signin",
    "/tasks/list", "listing",
    "/tasks/post", "post",
    "/tasks/chgpass", "chgpass",
    "/tasks/act", "actions",
    "/tasks/logout", "logout",
    "/tasks/signup", "signup"
)

The format of this tuple is: url-path-pattern, handler-class this pattern will repeat as more url patterns are defined

GET and POST methods

While most people don't notice it just browsing around, your browser uses a language known as HTTP for communicating with the World Wide Web. The details aren't important, but the basic idea is that Web visitors ask web servers to perform certain functions (like GET or POST) on URLs (like / or /foo?f=1).

GET is the one we're all familiar with, the one used to request the text of a web page. When you type harvard.edu into your web browser, it literally asks the Harvard web server to GET /. The second-most famous, POST, is often used when submitting certain kinds of forms, like a request to purchase something. You use POST whenever the act of submitting a request does something (like charge your credit card and process an order). This is key, because GET URLs can be passed around and indexed by search engines, which you definitely want for most of your pages but definitely don't want for things like processing orders (imagine if Google tried to buy everything on your site!).

class index:
    def GET(self):
        return "Hello, world!"

This GET function will now get called by web.py anytime someone makes a GET request for /.

Now we need to create an application specifying the urls and a way to tell web.py to start serving web pages:

if __name__ == "__main__": 
    app = web.application(urls, globals())
    app.run()

First we tell web.py to create an application with the URLs we listed above, looking up the classes in the global namespace of this file. And finally we make sure that web.py serves the application we created above.

Conclusion

The philosophy of Web.py -- a minimalist framework -- is not to abstract away the details of interacting with the Web, but to make that interaction easier. As a result, you'll find yourself writing HTTP GET function handlers directly. Likewise, the Web.py database system does not abstract away SQL; rather than hide the fact that you're querying a database, it hides the details of working with different databases. Web.py does define a template language, which -- like that of Web2py -- lets you embed arbitrary Python code in a Web page. Web.py is ideal if you're already familiar with building Web applications (perhaps you once wrote CGI-based applications). You'll get started quickly with Web.py, but you'll have to rely on your own wits to go beyond simple Web applications.

References

[1] web.py official website: http://webpy.org/

[2] python development story: http://faruk.akgul.org/blog/python-development-story-why-webpy/

[3] pillars of python-six web frameworks: http://www.infoworld.com/article/2622836/application-development/pillars-of-python--six-python-web-frameworks-compared.html?page=2

[4] Django vs flash vs pyramid https://www.airpair.com/python/posts/django-flask-pyramid