CS2021- Week 10 Models and Views. Model, View, Controller. Web Development Model, Views, Controller Templates Databases

Similar documents
Building Python web app on GAE

Hons. B.Sc. Degree in Software Engineering/Development. Web and Cloud Development

webapp2 Documentation

CS4HS Using Google App Engine. Michael Parker

Chapter 19: Twitter in Twenty Minutes

CS2021-Week 9 - Forms. HTML Forms. Python Web Development. h?ps:// cs253/unit-2html. Form for Submitting input:

App Engine Web App Framework

App Engine Web App Framework

Google App Engine Data Store. Google is BIG. Advanced Stuff.

Google App Engine Using Templates

Rapid Development with Django and App Engine. Guido van Rossum May 28, 2008

Google & the Cloud. GData, Mashup Editor, AppEngine. Gregor Hohpe Software Engineer Google, Inc. All rights reserved,

CS 350 COMPUTER/HUMAN INTERACTION. Lecture 6

Stamp Builder. Documentation. v1.0.0

Taking Fireworks Template and Applying it to Dreamweaver

widgets, events, layout loosely similar to Swing test browser, or plugin for testing with real browser on local system

web.py Tutorial Tom Kelliher, CS 317 This tutorial is the tutorial from the web.py web site, with a few revisions for our local environment.

Lab 1: Introducing HTML5 and CSS3

AGENDA. EMBEDDING FONTS [ Font Files & CSS font-family ] :: Online Font Converter :: ADD font-family css code to style.css

Building Scalable Web Apps with Python and Google Cloud Platform. Dan Sanderson, April 2015

A Sample Approach to your Project

Introduction to using HTML to design webpages

Customizing Blueball Simplex Design User Guide v1.0

generates scaffolding/framework for models, views

Jaesun Han (NexR CEO & Founder)

Class 9 / Instructor: Ira Epstein

Installing and Running the Google App Engine On a Macintosh System

(Simple) JavaScript Framework Homework

JSN PageBuilder 3 Configuration Manual Introduction

Form Overview. Form Processing. The Form Element. CMPT 165: Form Basics

CCS Lab FAQ: Using Google App Engine to host websites

Introduction to WEB PROGRAMMING

Santa Tracker. Release Notes Version 1.0

Microsoft Expression Web Quickstart Guide

To illustrate how to set TAG styles the <body> and <h1> tags (for the BODY and the HEADING 1 styles) will be adjusted.

Website Development (WEB) Lab Exercises

Seminar report Google App Engine Submitted in partial fulfillment of the requirement for the award of degree Of CSE

CSS worksheet. JMC 105 Drake University

Virto SharePoint Forms Designer for Office 365. Installation and User Guide

Introducing ASP.NET Web Pages Updating Database Data

HTML/CSS Lesson Plans

Getting Started with Eric Meyer's CSS Sculptor 1.0

welcome to BOILERCAMP HOW TO WEB DEV

The Django Web Framework Part II. Hamid Zarrabi-Zadeh Web Programming Fall 2013

Chapter 1. Introduction to web development and PHP. 2010, Mike Murach & Associates, Inc. Murach's PHP and MySQL, C1

Web-based IDE to create Model and Controller Components for MVC-based Web Applications on CakePHP

Introduction to web development with PHP

한재선 KAIST 정보미디어경영대학원겸직교수 & NexR 대표이사

CSCB20 Week 7. Introduction to Database and Web Application Programming. Anna Bretscher Winter 2017

DELIZIOSO RESTAURANT WORDPRESS THEME

Introduction to Multimedia. MMP100 Spring 2016 thiserichagan.com/mmp100

E-Business Systems 1 INTE2047 Lab Exercises. Lab 5 Valid HTML, Home Page & Editor Tables

Exploring Computer Science Web Final - Website

By the end of this chapter, you will have a very basic, but fully functional blogging system.

Unifer Documentation. Release V1.0. Matthew S

G I F T U N I V E R S I T Y

IBM Bluemix Node-RED Watson Starter

Dreamweaver: Web Forms

Lecture 10(-ish) Web [Application] Frameworks

Unveiling the Basics of CSS and how it relates to the DataFlex Web Framework

PBWORKS - Student User Guide

CSS Module in 2 Parts

nacelle Documentation

How to lay out a web page with CSS

CS50 Quiz Review. November 13, 2017

SocialMiner Configuration

Signals Documentation

webkitpony Documentation

week8 Tommy MacWilliam week8 October 31, 2011

Creating Forms. Starting the Page. another way of applying a template to a page.

CSCI 1320 Creating Modern Web Applications. Content Management Systems


yawrap Documentation Release Michal Kaczmarczyk

Aware IM Version 8.2 Aware IM for Mobile Devices

App Engine: Datastore Introduction

Tomasz Szumlak WFiIS AGH 23/10/2017, Kraków

While editing a page, a menu bar will appear at the top with the following options:

LECTURE 14. Web Frameworks

Editing Webpages in N/Vu

Make Conversations With Customers More Profitable.

How to Make a Contact Us PAGE in Dreamweaver

FORMS. The Exciting World of Creating RSVPs and Gathering Information with Forms in ClickDimensions. Presented by: John Reamer

Table Basics. The structure of an table

Virto SharePoint Forms Designer for Office 365. Installation and User Guide

Study Guide 2 - HTML and CSS - Chap. 6,8,10,11,12 Name - Alexia Bernardo

3344 Database Lab. 1. Overview. 2. Lab Requirements. In this lab, you will:

cwhois Manual Copyright Vibralogix. All rights reserved.

django-xross Documentation

COSC 2206 Internet Tools. CSS Cascading Style Sheets

HTML Summary. All of the following are containers. Structure. Italics Bold. Line Break. Horizontal Rule. Non-break (hard) space.

Lecture B3 Style : Algorithmic Thinking. Computing and Art : Nature, Power, and Limits CC 3.12: Fall 2007

CSS Review. Objec(ves. Iden(fy the Errors. Fixed CSS. CSS Organiza(on

Web Structure and Style. MB2030 Section 2 Class 4

Using Dreamweaver CS6

Clojure Web Security. FrOSCon Joy Clark & Simon Kölsch

yawrap Documentation Release Michal Kaczmarczyk

HTML and CSS MIS Konstantin Bauman. Department of MIS Fox School of Business Temple University

Problem Description Earned Max 1 HTML / CSS Tracing 20 2 CSS 20 3 PHP 20 4 JS / Ajax / JSON 20 5 SQL 20 X Extra Credit 1 TOTAL Total Points 100

Python web frameworks

ITSE 1401 Web Design Tools Lab Project 4 (Expression Web 4 - Units M, N, O, P) Last revised: 1/9/14

Transcription:

CS2021- Week 10 Models and Views Web Development Model, Views, Controller Templates Databases Model, View, Controller The MVC pa@ern is simple and very useful in web development. MVC pa@ern forces one to think of the applicafon in terms of these three modules - Model : The core logic of the

applicafon. This maintains the state and data that the applicafon represents. Controller : The funcfons of the user interface that manipulate the applicafon. View : The user interface which displays informafon about the model to the user. Templates DRY out Views (Don t Repeat Yourself) Templates h@ps://www.udacity.com/wiki/ cs253/appendix-b

Jinja2 is a powerful templa3ng engine modeled a6er Django's templa3ng system. Templates help to separate your logic (or model) from your

presenta3on (or views). Here's the basic 3- Step process to get it working in AppEngine. Step 1: Modify 'app.yaml

Open app.yaml from your AppEngine project directory in the editor of your choice. You need to do a few things here: 1) change the run3me aoribute to python27, 2) add threadsafe: true,

and 3) add the Jinja2 library in the configura3on. Here's what your app.yaml might look like a6er you're done: application: foobar version: 1 runtime: python27 api_version: 1 threadsafe: true libraries: - name: jinja2 version: latest handlers:

- url: /.* script: main.app Templates in GAE Step 2: Move your HTML to a separate file For the sake of this example, let's create a directory in your project folder called templates. Place your HTML code into a file called index.html, underneath your templates folder. Step 3: Modify your main Python script to use a Jinja2 template import os import webapp2 import jinja2 #copy this to set up jinja environment

template_dir = os.path.join(os.path.dirname( file ), 'templates') jinja_env = jinja2.environment(loader = jinja2.filesystemloader(template_di r), autoescape=true) class MainPage(webapp2.RequestHandler): def get(self): template_values = {'name': 'SomeGuy', 'verb': 'extremely enjoy'} template = jinja_env.get_template('index.html' ) self.response.out.write(template.re nder(template_values)) Databases and Python TradiFonal databases are organized around tables with

schemas that name columns. We insert rows into tables and Queries such as SELECT return sets of rows referenced by cursors. Cursors can be used to operate and manipulate whole sets of rows. In Python we have lists and dicfonaries so there must be some mapping and glue to link python with a database applicafon. Let s start with construcfon of low level table in Python using the module from collec)ons

import namedtuple. The namedtuple class allows us to use sorted tuples with named columns. ImplemenFng a Table in Python with namedtuples from collecfons import namedtuple # make a basic Link class Link = namedtuple('link', ['id', 'submi@er_id', 'submi@ed_fme', 'votes', 'Ftle', 'url']) # list of Links to work with

links = [ Link(0, 60398, 1334014208.0, 109, "C overtakes Java as the No. 1 programming language in the TIOBE index.", "h@p://pixelstech.net/ arfcle/index.php? id=1333969280"), Link(1, 60254, 1333962645.0, 891, "This explains why technical books are all ridiculously thick and overpriced", "h@p:// prog21.dadgum.com/65.html"), Link(23, 62945, 1333894106.0,

351, "Learn Haskell Fast and Hard", "h@p://yannesposito.com/ Scratch/en/blog/Haskell-the- Hard-Way/"), Link(2, 6084, 1333996166.0, 81, "Announcing Yesod 1.0- a robust, developer friendly, high performance web framework for Haskell", "h@p:// www.yesodweb.com/blog/ 2012/04/announcingyesod-1-0"), Link(3, 30305, 1333968061.0,

270, "TIL about the Lisp Curse", "h@p:// www.winestockwebdesign.com/ Essays/Lisp_Curse.html"), Link(4, 59008, 1334016506.0, 19, "The Downfall of ImperaFve Programming. FuncFonal Programming and the MulFcore RevoluFon", "h@p://fpcomplete.com/ the-downfall-of-imperafveprogramming/"), Link(5, 8712, 1333993676.0, 26, "Open Source - Twi@er

Stock Market Game - ", "h@p:// www.twitstreet.com/"), Link(6, 48626, 1333975127.0, 63, "First look: Qt 5 makes JavaScript a first-class cifzen for app development", "h@p://arstechnica.com/ business/news/2012/04/an-indepth-look-at-qt-5-makingjavascript-a-first-class-cifzen-fornafve-cross-plarormdevelopme.ars"), Link(7, 30172, 1334017294.0, 5, "Benchmark of DicFonary

Structures", "h@p:// lh3lh3.users.sourceforge.net/ udb.shtml"), Link(8, 678, 1334014446.0, 7, "If It's Not on Prod, It Doesn't Count: The Value of Frequent Releases", "h@p:// bits.shu@erstock.com/?p=165"), Link(9, 29168, 1334006443.0, 18, "Language proposal: dave", "h@p:// davelang.github.com/"), Link(17, 48626, 1334020271.0, 1, "LispNYC and EmacsNYC

meetup Tuesday Night: Large Scale Development with Elisp ", "h@p://www.meetup.com/ LispNYC/events/47373722/"), Link(101, 62443, 1334018620.0, 4, "research!rsc: Zip Files All The Way Down", "h@p:// research.swtch.com/zip"), Link(12, 10262, 1334018169.0, 5, "The Tyranny of the Diff", "h@p:// michaelfeathers.typepad.com/ michael_feathers_blog/2012/04/ the-tyranny-of-the-diff.html"),

Link(13, 20831, 1333996529.0, 14, "Understanding NIO.2 File Channels in Java 7", "h@p://java.dzone.com/ arfcles/understanding-nio2- file"), Link(15, 62443, 1333900877.0, 1244, "Why vector icons don't work", "h@p://www.pushingpixels.org/2011/11/04/aboutthose-vector-icons.html"), Link(14, 30650, 1334013659.0, 3, "Python - Gewng Data Into

Graphite - Code Examples", "h@p:// coreygoldberg.blogspot.com/ 2012/04/python-gewng-datainto-graphite-code.html"), Link(16, 15330, 1333985877.0, 9, "Mozilla: The Web as the Plarorm and The Kilimanjaro Event", "h@ps:// groups.google.com/forum/? fromgroups#!topic/ mozilla.dev.planning/ Y9v46wFeejA"), Link(18, 62443, 1333939389.0, 104,

"github is making me feel stupid(er)", "h@p:// www.serpenfne.com/blog/ 2012/04/08/github-is-makingme-feel-stupider/"), Link(19, 6937, 1333949857.0, 39, "BitC RetrospecFve: The Issues with Type Classes", "h@p://www.bitc-lang.org/ pipermail/bitc-dev/2012-april/ 003315.html"), Link(20, 51067, 1333974585.0, 14, "Object Oriented C: Classlike Structures",

"h@p:// cecilsunkure.blogspot.com/ 2012/04/object-oriented-c-classlike-structures.html"), Link(10, 23944, 1333943632.0, 188, "The LOVE game framework version 0.8.0 has been released - with GLSL shader support!", "h@ps://love2d.org/ forums/viewtopic.php? f=3&t=8750"), Link(22, 39191, 1334005674.0, 11, "An open le@er to language designers: Please kill your sacred cows. (megarant)",

"h@p://joshondesign.com/ 2012/03/09/open-le@erlanguage-designers"), Link(21, 3777, 1333996565.0, 2, "Developers guide to Garage48 hackatron", "h@p://marfngryner.com/ developers-guide-to-garage48- hackatron/"), Link(24, 48626, 1333934004.0, 17, "An R programmer looks at Julia", "h@p://www.rbloggers.com/an-r-programmerlooks-at-julia/")]

Databases and Python # make and populate a table of reddit links Import sqlite3 db = sqlite3.connect(':memory:') db.execute('create table links ' + '(id integer, submi@er_id integer, submi@ed_fme integer, ' + 'votes integer, Ftle text, url

text)') for l in links: db.execute('insert into links values (?,?,?,?,?,?)', l) # db is an in-memory sqlite database that can respond to sql queries using the # execute() funcfon. cursor1 = db.execute("select * from links where id = 5") cursor2 = db.execute( select * from links where submi@er_id = 5 OR votes > 23 ) cursor3 = db.execute( select * from links where votes > 10

order by votes ) Queries in Python def query(): results = [] c = db.execute("select id from links where submitter_id = 62443 order by submitted_time asc") results = [t[0] for t in c] return results Google App Engine Datastore We will be using the Google App Engine Datastore. This is a modern, non-relafonal database provided by App Engine. There are a couple of things you need to know about

Google App Engine Datastore. What we have been referring to as tables are known as enffes in Google App Engine Datastore. From a programmers point of view GAE Datastore enffes are somewhat analogous to set of Python DicFonaries each with a unique id and a common set of keys. The Python App Engine SDK includes a data modeling library for represenfng Datastore enffes as instances of Python classes, and for storing and retrieving those instances in Datastore. See h@ps://cloud.google.com/appengine/ docs/python/datastore/enffes GQL In the App Engine Datastore we have something a li@le different then standard SQL database. Here we call it GQL and it is basically a simplified version of SQL that

only works in the Datastore. The main difference between GQL and SQL is that all queries begin with SELECT *. There is no way to select individual columns. This also means we cannot do joins. ASCII Chan We will build a website called ASCII Chan a message board for sharing ASCII art. We will have a form that takes a Ftle, some ASCII art and a submit bu@on. A user can submit this and below the form the users will see ASCII art that has been submi@ed by other people. import os import webapp2 import jinja2 from google.appengine.ext import db template_dir = os.path.join(os.path.dirname( file ), 'templates') jinja_env = jinja2.environment(loader = jinja2.filesystemloader(template_dir), autoescape=true) class Handler(webapp2.RequestHandler): def write(self, *a, **kw): self.response.out.write(*a, **kw) def render_str(self, template, **params): t = jinja_env.get_template(template) return t.render(params)

def render(self, template, **kw): self.write(self.render_str(template, **kw)) class MainPage(Handler): def get(self): self.write("asciichan!") app = webapp2.wsgiapplication([('/', MainPage)], debug=true) Create the Form <!DOCTYPE html> <html> <head> <title>/ascii/</title> </head> <body> <h1>/ascii/</h1> </body> </html> <!-- Once that is working we next add the following form to the body: --> <form method="post"> <label> <div>title</div> <input type="text" name="title"> </label> <label> <div>art</div> <textarea name="art"></textarea> </label> <input type="submit">

</form> Handler for the form def post(self): title = self.request.get("title") art = self.request.get("art") if title and art: self.write("thanks!") else: error = "we need both a title and some artwork!" self.render("front.html", error = error) Add an error message at the booom of the form ( front.html ): <form method="post"> <label> <div>title</div><input type="text" name="title"> </label> <label> <div>art</div><textarea name="art"></textarea> </label> <div class="error"> {{ error }}</div> <input type="submit">

</form> Adding Parameters to preserve input in forms def post(self): title = self.request.get("title") art = self.request.get("art") if title and art: self.write("thanks!") else: error = "we need both a title and some artwork!" self.render_front(title, art, error) Modified MainPage Handler CreaFng EnFFes in GAE Datastore The way to define an enfty in Google App Engine is to define a class derived from db.model:

from google.appengine.ext import db class Art(db.Model): title = db.stringproperty(requir ed = True) art = db.textproperty(required = True) created = db.datetimeproperty(auto _now_add = True) Working with EnFFes # Extend the success case in post Form Handler:

if title and art: a = Art(title = title, art = art) a.put() self.redirect("/") Queries in render_front funcfon def render_front(self, title="", art="", error=""): arts = db.gqlquery("select * FROM Art ORDER BY created DESC") self.render("front.html", title=title, art=art, error = error, arts = arts) <!-- front.html --> <body> <h1>/ascii/</h1> <form method="post"> <label> <div>title</div> <input type="text" name="title" value=""> </label> <label> <div>art</div>

<textarea name="art"></textarea> </label> <div class="error"></div> <input type="submit"> </form> <hr> {% for art in arts %} <div class="art"> <div class="art-title">{ {art.title}}</ div> <pre class="art-body">{ {art.art}}</pre> </div> {% endfor %} </body> Styling <style type="text/css"> body { font-family: sans-serif; width: 800px; margin: 0 auto; padding: 10px; } error { color: red;

} label { display: block; font-size: 20px; } input[type=text] { width: 400px; font-size: 20px; padding: 2px; } textarea { width: 400px; height: 200px; font-size: 17px; font-family: monospace; } input[type=submit] { font-size: 24px; }

hr { margin: 20px auto; }.art +.art { margin-top: 20px; }.art-ftle { font-weight: bold; font-size: 20px; }.art-body { margin: 0; font-size: 17px; } </style> Homework for Problem

Set 3 Build a Blog h@ps://www.udacity.com/ course/viewer#!/c-cs253/ l-48198869/e-48508425/ m-48532665