Git Product home page Git Product logo

cssi-7.1-gae-review's Introduction

Google App Engine Review

You will use these files (which you should have created on Day 6) to learn more about Google App Engine. Specifically, we will spend today learning how to make our templates dynamic by adding logic and using variables.

Let's break down each part of our controller, in this case helloworld.py

###Importing Libraries

import jinja2
import os
import webapp2

This is where we import our libraries.

  • Jinja2 is our templating library.
  • OS allows us to access our machine in order to determine our local directory.
  • Webapp2 is the framework that allows us to easily build all three parts of our MVC.

###Defining the Jinja Environment

jinja_environment = jinja2.Environment(loader=
    jinja2.FileSystemLoader(os.path.dirname(__file__))) 

This line is doing a lot. In Python, file is a special variable that indicates the current file, in this case, helloworld.py. This line opens the directory (dirname) that our templates are in. Then when we call jinja2.Environment, we are telling our code how we want to load those templates (by using Jinja!).

###The Handler

class HelloHandler(webapp2.RequestHandler):
	def get(self):
		template = jinja_environment.get_template('templates/hello.html')
		self.response.out.write(template.render())

This is the heavy-hitter of our code, our handler. Each handler is a class with one or more methods that handle the request and generate a response. In this case, HelloHandler is set up to handle GET requests. The response could be any Python code and it is your job as the developer to tell the handler how to respond. In our example, our handler responds by rendering the hello.html template.

###Setting Up Routes

routes = [  ('/.*', HelloHandler),]

In this line of code, we set up our routes, the * means that ALL urls will route to the HelloHandler.

###Using WSGIA

app = webapp2.WSGIApplication(routes, debug=True)`

Finally, we make sure that we use the WSGIA app, which helps our web server interact with or app. WSGIA is beyond the scope of our class, but feel free to learn more here.

View Google App Engine Review on Learn.co and start learning to code for free.

cssi-7.1-gae-review's People

Contributors

crepric avatar nanselmo avatar sarogers avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

google-cssi

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.