Tutorial: web apps using web.py

Or how to deploy a web app prototype with less the 30min and only one cup of coffee.

Web.py isn’t by far the most robust, fast or reliable python web platform. But it’s really cool for doing something really boring: Object Oriented Web App Prototypes. Whether you’re coding your fuure app using Django, Node.js or just winning a Hacketon, here are some cool tips to get your app running super fast.

Hello World

import web

# Controllers are classes
class index:
  # HTTP methods are (WOW) methods in the class
  def GET(self):
    return "Hello, world!"

# Web app class
app = web.application()

# Add URL mapping for the class
app.add_mapping("/", index)

if __name__ == "__main__":
  # Run test server at http://localhost:8080
  app.run()

Include templates

/run.py
import web

# Template renderer (disable cache for devel)
render = web.template.render('templates/', cache=False)

class index:
  def GET(self):
    # Templates are render methods
    return render.main(title="My First App" content="Hello World")

app = web.application()
app.add_mapping("/", index)

if __name__ == "__main__":
  app.run()
/templates/main.html
$def with (title,content)

<!DOCTYPE html>
<html>;
  <head>
<title>$title</title>
  </head>
  <body>
<div>
<h1>$:content</h1>
</div>
</body>
</html>

Include modular HTML files