PageRenderTime 55ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/day-02/app-engine-part-1.md

https://gitlab.com/learn-co-curriculum/cssi-teacher-bootcamp
Markdown | 121 lines | 95 code | 26 blank | 0 comment | 0 complexity | b9b3c731f0e38ee3000d54cee4ca1ce9 MD5 | raw file
  1. ## Intro to App Engine
  2. ### GAE as a Connector
  3. * Platform as a Service (Paas) to build/run applications hosted on Google's servers
  4. * App Engine connects the back-end (the Python scripts) and front-end (the HTML/CSS/JavaScript) for you with minimal work on your end
  5. ### Creating a New App
  6. * In Launcher, Create New app
  7. * App Name should be only lower case, no underscores! (they will have no name it pig-latin-name)
  8. * Pick Directory
  9. * Look at files together
  10. * Launch App
  11. * Explain Ports
  12. ### Responding to Requests using Handlers and Routes
  13. A user can make a request (ask students about the two different kinds) and we need to "handle" them, based on which type of request it was and which resource it was trying to access.
  14. * Routes read the urls and decide which handler to execute
  15. * Within the handler, you can define a get(self) or a get(post) method.
  16. ```
  17. class PointlessHandler(webapp2.RequestHandler):
  18. def get(self):
  19. self.response.write("<html><form action='' method='get'><button name='get-pressed' value='true'>Make Get Request</button></form><form action='' method='post'><button name='post-pressed' value='true'>Make Post Request</button></form></html>")
  20. def post(self):
  21. self.response.write('You pressed the POST button!')
  22. ```
  23. ```python
  24. class MainHandler(webapp2.RequestHandler):
  25. def get(self):
  26. self.response.write('Hello world!')
  27. class CountHandler(webapp2.RequestHandler):
  28. def get(self):
  29. for i in range(1, 11):
  30. self.response.write(i)
  31. app = webapp2.WSGIApplication([
  32. ('/', MainHandler),
  33. ('/count', CountHandler),
  34. ], debug=True)
  35. ```
  36. MainHandler and CountHandler both respond in different ways. Ask students:
  37. * What MainHandler will do? (displays "Hello World")
  38. * What will CountHandler do? (print 1-10).
  39. * What url will run the CountHandler? (/count)
  40. ####STUDENT PRACTICE: Routes & Handlers
  41. Students will work in pairs to add a pigLatin handler, just like the count handler, they can add the method they wrote from Day 1.
  42. ### Templates
  43. Right now our controller can respond with text only. We can add elements and styling to our pages by using templates.
  44. Templates true power comes because each HTML page can dynamically change.
  45. ####Make HTML Page
  46. Your HTML page should be created in a new Templates folder
  47. ```html
  48. <!DOCTYPE html>
  49. <html>
  50. <head>
  51. <title>Howdy</title>
  52. </head>
  53. <body>
  54. <h1>Hello World!</h1>
  55. <img height="100px" width="150px" src="https://blackgeoscientists.files.wordpress.com/2014/06/helloworld.jpg" alt="A cute Pic of a Dude on the World">
  56. <h2>How are ya?</h2>
  57. </body>
  58. </html>
  59. ```
  60. #### Add Jinja2
  61. 1. Add jinja 2 to .yaml file and main.py
  62. Controller needs to go get that page and combine it with any data - a templating engine is needed to do all this cleanly.
  63. Jinja2 is one of the more popular and most popular templating engines for Python
  64. ```
  65. libraries:
  66. - name: jinja2
  67. version: latest
  68. ```
  69. 2. Import jinj2 and os in main.py
  70. ```python
  71. import jinja2
  72. import os
  73. import webapp2
  74. ```
  75. 3. Set up Jinja Environment
  76. Jinja 2 needs to know where your HTML file is located. Set up Jinja directory to match current direction of the main.py file
  77. ```python
  78. jinja_environment = jinja2.Environment(
  79. loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))#this little bit sets jinja's relative directory to match the directory name(dirname) of the current __file__, in this case, helloworld.py
  80. ```
  81. 4. Use get_template method on your jinja_environment path
  82. Get the template you want by using the get_template method, and passing the path to the HTML file. Then render that template. Render is the magic verb that combines the HTML with any embedded logical code or data
  83. ```python
  84. import jinja2
  85. import os
  86. import webapp2
  87. jinja_environment = jinja2.Environment(
  88. loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))#this little bit sets jinja's relative directory to match the directory name(dirname) of the current __file__, in this case, helloworld.py
  89. class MainHandler(webapp2.RequestHandler):
  90. def get(self):
  91. hello_template = jinja_environment.get_template('templates/hello.html')
  92. self.response.write(hello_template.render())
  93. ```
  94. ####STUDENT PRACTICE: Rendering Templates
  95. Students will work in pairs to make Pig Latin template and render it.