Recitation – Week 8 Pranut jain.

Slides:



Advertisements
Similar presentations
EIONET Training Zope Page Templates Miruna Bădescu Finsiel Romania Copenhagen, 28 October 2003.
Advertisements

Sharpen Your MVC Views with Razor By Jon Marozick.
Introduction to MVC Adding a View Page NTPCUG Tom Perkins, Ph.D.
The Web Warrior Guide to Web Design Technologies
Web Applications Development Using Coldbox Platform Eddie Johnston.
PHP (2) – Functions, Arrays, Databases, and sessions.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
1 Parts of a Loop (reminder) Every loop will always contain three main elements: –Priming: initialize your variables. –Testing: test against some known.
DT228/3 Web Development JSP: Directives and Scripting elements.
Introduction to JavaScript for Python Programmers
Introduction to scripting
JSP Standard Tag Library
Introduction to PHP and Server Side Technology. Slide 2 PHP History Created in 1995 PHP 5.0 is the current version It’s been around since 2004.
CS346 - Javascript 1, 21 Module 1 Introduction to JavaScript CS346.
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
Using JavaBeans and Custom Tags in JSP Lesson 3B / Slide 1 of 37 J2EE Web Components Pre-assessment Questions 1.The _____________ attribute of a JSP page.
Week 9 PHP Cookies and Session Introduction to JavaScript.
® IBM Software Group © 2007 IBM Corporation JSP Expression Language
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
CMPS 211 JavaScript Topic 1 JavaScript Syntax. 2Outline Goals and Objectives Goals and Objectives Chapter Headlines Chapter Headlines Introduction Introduction.
Copyrighted material John Tullis 10/17/2015 page 1 04/15/00 XML Part 3 John Tullis DePaul Instructor
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
הרצאה 4. עיבוד של דף אינטרנט דינמי מתוך Murach’s PHP and MySQL by Joel Murach and Ray Harris.  דף אינטרנט דינמי משתנה עפ " י הרצת קוד על השרת, יכול להשתנות.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
JAVA BEANS JSP - Standard Tag Library (JSTL) JAVA Enterprise Edition.
1 Java Server Pages A Java Server Page is a file consisting of HTML or XML markup into which special tags and code blocks are inserted When the page is.
JavaScript Introduction and Background. 2 Web languages Three formal languages HTML JavaScript CSS Three different tasks Document description Client-side.
LECTURE 14 Web Frameworks. WEB DEVELOPMENT CONTINUED Web frameworks are collections of packages or modules which allow developers to write web applications.
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
© 2010 Robert K. Moniot1 Chapter 6 Introduction to JavaScript.
Introduction to JavaScript MIS 3502, Fall 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 9/29/2016.
JavaScript: Conditionals contd.
PHP using MySQL Database for Web Development (part II)
Module 1 Introduction to JavaScript
JavaScript, Sixth Edition
Introduction to Dynamic Web Programming
Chapter 6 JavaScript: Introduction to Scripting
IBM Rational Rhapsody Advanced Systems Training v7.5
Play Framework: Introduction
Introduction to Scripting
PHP Introduction.
Intro to PHP & Variables
Un</br>able’s MySecretSecrets
Invoking Java Code from JSP
Web Systems Development (CSC-215)
Functions BIS1523 – Lecture 17.
CS190/295 Programming in Python for Life Sciences: Lecture 6
PHP.
T. Jumana Abu Shmais – AOU - Riyadh
HYPERTEXT PREPROCESSOR BY : UMA KAKKAR
Using Templates and Library Items
Intro to PHP.
Training & Development
How to organize and document your classes
Peter Seibel Practical Common Lisp Peter Seibel
IPC144 Introduction to Programming Using C Week 4 – Lesson 2
JavaScript: Introduction to Scripting
PHP an introduction.
CIS 136 Building Mobile Apps
Mr. Justin “JET” Turner CSCI 3000 – Fall 2016 Section DA MW 4:05-5:20
Web Programming and Design
Web Programming and Design
 A function is a named sequence of statement(s) that performs a computation. It contains  line of code(s) that are executed sequentially from top.
SEEM 4540 Tutorial 4 Basic PHP based on w3Schools
Presentation transcript:

Recitation – Week 8 Pranut jain

Plan for Today Flask Discussion Contd. Flask(Jinja) Templates SQLAlchemy Setup.

Static Files Dynamic web applications also need static files. That’s usually where the CSS and JavaScript files are coming from. Just create a folder called static in your package or next to your module and it will be available at /static on the application. http://flask.pocoo.org/docs/0.12/quickstart/

Rendering Templates To render a template you can use the render_template() method. All you have to do is provide the name of the template and the variables you want to pass to the template engine as keyword arguments. Flask will look for templates in the templates folder. from flask import render_template @app.route('/hello/') @app.route('/hello/<name>') def hello(name=None): return render_template('hello.html', name=name) http://flask.pocoo.org/docs/0.12/quickstart/

Redirects and Errors To redirect a user to another endpoint, use the redirect() function; to abort a request early with an error code, use the abort() function: from flask import abort, redirect, url_for @app.route('/') def index(): return redirect(url_for('login')) @app.route('/login') def login(): abort(401) this_is_never_executed() http://flask.pocoo.org/docs/0.12/quickstart/

Error Handler By default a black and white error page is shown for each error code. If you want to customize the error page, you can use the errorhandler() decorator: from flask import render_template @app.errorhandler(404) def page_not_found(error): return render_template('page_not_found.html'), 404 Note the 404 after the render_template() call. This tells Flask that the status code of that page should be 404 which means not found. http://flask.pocoo.org/docs/0.12/quickstart/

Sessions Session object allows you to store information specific to a user from one request to the next. This is implemented on top of cookies for you and signs the cookies cryptographically. Check hello5.py http://flask.pocoo.org/docs/0.12/quickstart/

Templates A Jinja template is simply a text file. Jinja can generate any text-based format (HTML, XML, CSV, LaTeX, etc.). A Jinja template doesn’t need to have a specific extension: .html, .xml, or any other extension is just fine. A template contains variables and/or expressions, which get replaced with values when a template is rendered; and tags, which control the logic of the template.  The next few slides would be talking about templates, please don’t get confused between Flask and Template slides. http://jinja.pocoo.org/docs/2.9/templates

Default Jinja delimiters {% ... %} for Statements {{ ... }} for Expressions to print to the template output {# ... #} for Comments not included in the template output # ... ## for Line Statements http://jinja.pocoo.org/docs/2.9/templates

Variables Template variables are defined by the context dictionary passed to the template. You can use a dot (.) to access attributes of a variable in addition to the standard Python __getitem__ “subscript” syntax ([]). The following lines do the same thing: {{ foo.bar }} {{ foo['bar'] }} http://jinja.pocoo.org/docs/2.9/templates

It’s important to know that the outer double-curly braces are not part of the variable, but the print statement. If you access variables inside tags don’t put the braces around them. If a variable or attribute does not exist, you will get back an undefined value. What you can do with that kind of value depends on the application configuration: the default behavior is to evaluate to an empty string if printed or iterated over, and to fail for every other operation. http://jinja.pocoo.org/docs/2.9/templates

Filters Variables can be modified by filters. Filters are separated from the variable by a pipe symbol (|) and may have optional arguments in parentheses. Multiple filters can be chained. The output of one filter is applied to the next. For example, {{ name|striptags|title }} will remove all HTML Tags from variable name and title- case the output (title(striptags(name))). http://jinja.pocoo.org/docs/2.9/templates

Tests Tests can be used to test a variable against a common expression. To test a variable or expression, you add is plus the name of the test after the variable. For example, to find out if a variable is defined, you can do name is defined, which will then return true or false depending on whether name is defined in the current template context. Tests can accept arguments, too. If the test only takes one argument, you can leave out the parentheses. For example, the following two expressions do the same thing: {% if loop.index is divisibleby 3 %} {% if loop.index is divisibleby(3) %} http://jinja.pocoo.org/docs/2.9/templates

Comments To comment-out part of a line in a template, use the comment syntax which is by default set to {# ... #}. {# note: commented-out template because we no longer use this {% for user in users %} ... {% endfor %} #} http://jinja.pocoo.org/docs/2.9/templates

Escaping The easiest way to output a literal variable delimiter ({{) is by using a variable expression: Way 1: {{ '{{‘ }} Way 2: {% raw %} <ul> {% for item in seq %} <li>{{ item }}</li> {% endfor %} </ul> {% endraw %} http://jinja.pocoo.org/docs/2.9/templates

Template Inheritance Template inheritance allows you to build a base “skeleton” template that contains all the common elements of your site and defines blocks that child templates can override. http://jinja.pocoo.org/docs/2.9/templates

Base Template base.html defines a simple HTML skeleton document that you might use for a simple two- column page. The {% block %} tags define four blocks that child templates can fill in. All the block tag does is tell the template engine that a child template may override those placeholders in the template. http://jinja.pocoo.org/docs/2.9/templates

Child Template Child.html The {% extends %} tag is the key here. It tells the template engine that this template “extends” another template. When the template system evaluates this template, it first locates the parent. The extends tag should be the first tag in the template. Everything before it is printed out normally and may cause confusion. Note that since the child template doesn’t define the footer block, the value from the parent template is used instead. You can access templates in subdirectories with a slash: {% extends "layout/default.html" %} http://jinja.pocoo.org/docs/2.9/templates

Super Blocks It’s possible to render the contents of the parent block by calling super. This gives back the results of the parent block: {% block sidebar %} <h3>Table Of Contents</h3> ... {{ super() }} {% endblock %} http://jinja.pocoo.org/docs/2.9/templates

Block Nesting and Scope Blocks can be nested for more complex layouts. However, per default blocks may not access variables from outer scopes: {% for item in seq %} <li>{% block loop_item %}{{ item }}{% endblock %}</li> {% endfor %} http://jinja.pocoo.org/docs/2.9/templates

This example would output empty <li> items because item is unavailable inside the block. The reason for this is that if the block is replaced by a child template, a variable would appear that was not defined in the block or passed to the context. Solution: {% for item in seq %} <li>{% block loop_item scoped %}{{ item }}{% endblock %}</li> {% endfor %} When overriding a block, the scoped modifier does not have to be provided. http://jinja.pocoo.org/docs/2.9/templates

pip install Flask-SQLAlchemy Model (SQLAlchemy) Database tools. To install: pip install Flask-SQLAlchemy Run the cmd as admin if the installation fails. What you can also do is grant admin privileges to pip to prevent doing this everytime.

Getting stated with SQLAlchemy Add this to your flask app: from flask_sqlalchemy import SQLAlchemy Once created, that object then contains all the functions and helpers from both sqlalchemy and sqlalchemy.orm. Furthermore it provides a class called Model that is a declarative base which can be used to declare models. Good reference to get started: http://flask-sqlalchemy.pocoo.org/2.3/quickstart/