LECTURE 14 Web Frameworks. WEB DEVELOPMENT CONTINUED Web frameworks are collections of packages or modules which allow developers to write web applications.

Slides:



Advertisements
Similar presentations
JQuery MessageBoard. Lets use jQuery and AJAX in combination with a database to update and retrieve information without refreshing the page. Here we will.
Advertisements

Struts Portlet Copyright © Liferay, Inc. All Rights Reserved. No material may be reproduced electronically or in print without written permission.
Presenter: James Huang Date: Sept. 29,  HTTP and WWW  Bottle Web Framework  Request Routing  Sending Static Files  Handling HTML  HTTP Errors.
JavaScript FaaDoOEngineers.com FaaDoOEngineers.com.
Java Script Session1 INTRODUCTION.
MMDE5011 – INTERACTIVE MEDIA PRACTICE 1 WEEK 1: INTRODUCTION TO HTML5
Introduction to MVC Adding a View Page NTPCUG Tom Perkins, Ph.D.
SEO Best Practices with Web Content Management Brent Arrington, Services Developer, Hannon Hill Morgan Griffith, Marketing Director, Hannon Hill 2009 Cascade.
Ruby on Rails Model of MVC. Model-View-Controller Paradigm A way of organizing a software system Benefits: Isolation of business logic from the user interface.
1.  Understanding about How to Working with Server Side Scripting using PHP Framework (CodeIgniter) 2.
11 Getting Started with ASP.NET Beginning ASP.NET 4.0 in C# 2010 Chapters 5 and 6.
TEA/TUG + ALDOT(Mobile) = H(O+I) The TEA/TUG being hosted by ALDOT in Mobile causes Hurricanes to come to Alabama. The TEA/TUG being hosted by ALDOT in.
Introducing new web content management tools for Priority...
Python and Web Programming
Direct Congress Dan Skorupski Dan Vingo. Inner workings Reminder: MVC design pattern Procedural view: From request to response o Request passed to a view.
 What I hate about you things people often do that hurt their Web site’s chances with search engines.
UNIT-V The MVC architecture and Struts Framework.
CST JavaScript Validating Form Data with JavaScript.
DAT602 Database Application Development Lecture 15 Java Server Pages Part 1.
Ruby on Rails. What is Ruby on Rails? Ruby on Rails is an open source full-stack web framework. It is an alternative to PHP/MySQL. It can render templates,
M. Taimoor Khan * Java Server Pages (JSP) is a server-side programming technology that enables the creation of dynamic,
Reading Data in Web Pages tMyn1 Reading Data in Web Pages A very common application of PHP is to have an HTML form gather information from a website's.
Lecture 6 – Form processing (Part 1) SFDV3011 – Advanced Web Development 1.
JavaScript & jQuery the missing manual Chapter 11
Chapter 5 Java Script And Forms JavaScript, Third Edition.
Server-side Scripting Powering the webs favourite services.
Creating Effective School and PTA Websites Sam Farnsworth Utah PTA Technology Specialist
E-Commerce: Introduction to Web Development 1 Dr. Lawrence West, Management Dept., University of Central Florida Topics What is a Web.
WB_FrontPage_How To CS3505. Front Page 4 Microsoft Web Page Authoring tool 4 Available to students at no charge see helpdesk 4 Provides support for building.
1 Accelerated Web Development Course JavaScript and Client side programming Day 2 Rich Roth On The Net
Universiti Utara Malaysia Chapter 3 Introduction to ASP.NET 3.5.
Ruby on Rails Your first app. Rails files app/ Contains the controllers, models, views and assets for your application. You’ll focus on this folder for.
JavaScript, Fourth Edition Chapter 5 Validating Form Data with JavaScript.
Forms and Server Side Includes. What are Forms? Forms are used to get user input We’ve all used them before. For example, ever had to sign up for courses.
HTML Basics BCIS 3680 Enterprise Programming. Web Client/Server Architecture 2  Your browser (the client) requests a Web page from a remote computer.
First Indico Workshop WEB FRAMEWORKS Adrian Mönnich May 2013 CERN.
Google App Engine Using Templates Jim Eng Thanks to Chuck Severance
Dynamic web content HTTP and HTML: Berners-Lee’s Basics.
Django 101 By: Jason Sumner. Django Overview Django was started in 2003, released under BSD in 2005, and the Django Software Foundation was established.
Java server pages. A JSP file basically contains HTML, but with embedded JSP tags with snippets of Java code inside them. A JSP file basically contains.
Google App Engine Data Store ae-10-datastore
Module: Software Engineering of Web Applications Chapter 2: Technologies 1.
Web-Applications: TurboGears BCHB Lecture 25 12/02/2015BCHB Edwards.
Chapter 12© copyright Janson Industries Java Server Faces ▮ Explain the JSF framework ▮ SDO (service data objects) ▮ Facelets ▮ Pagecode classes.
Basic ActionScript and PHP Cis 126. Getting Started set up a basic folder structure so we can keep our files organized. Mirror this structure on your.
 Packages:  Scrapy, Beautiful Soup  Scrapy  Website  
Introduction  “M” “V” “C” stands for “MODEL” “VIEW” “CONTROLLER”. ASP.NET MVC is an architecture to develop ASP.NET web applications in a different manner.
Chapter 6 Chapter 6 Server Side Programming (JSP) Part 1 1 (IS 203) WebProgramming (IS 203) Web Programming.
PHP Form Processing * referenced from
LECTURE 13 Intro to Web Development. WEB DEVELOPMENT IN PYTHON In the next few lectures, we’ll be discussing web development in Python. Python can be.
: Information Retrieval อาจารย์ ธีภากรณ์ นฤมาณนลิณี
LECTURE 2 Python Basics. MODULES So, we just put together our first real Python program. Let’s say we store this program in a file called fib.py. We have.
Ruby on Rails. Web Framework for Ruby Designed to make it easier to develop, deploy, and maintain web applications Design with Model-View-Controller –almost.
Entity Framework Database Connection with ASP Notes from started/getting-started-with-ef-using-mvc/creating-an-
Modern Development Technologies in SharePoint SHAREPOINT SATURDAY OMAHA APRIL, 2016.
World Wide Web has been created to share the text document across the world. In static web pages the requesting user has no ability to interact with the.
BIT 286: Web Applications ASP.Net MVC. Objectives Applied MVC overview Controllers Intro to Routing Views ‘Convention over configuration’ Layout files.
LECTURE 14 Web Frameworks. WEB DEVELOPMENT CONTINUED Web frameworks are collections of packages or modules which allow developers to write web applications.
Chapter 1 Getting Started with ASP.NET Objectives Why ASP? To get familiar with our IDE (Integrated Development Environment ), Visual Studio. Understand.
Section 10.1 Define scripting
Chapter 5 Validating Form Data with JavaScript
Intro to WordPress (Using XAMPP)
Data Virtualization Tutorial: Introduction to SQL Script
ASP MVP Web applications and Razor
Lecture 14 Web Frameworks.
Play Framework: Introduction
Recitation – Week 8 Pranut jain.
Intro to PHP & Variables
Engineering Innovation Center
Inside a PMI Online Course
Presentation transcript:

LECTURE 14 Web Frameworks

WEB DEVELOPMENT CONTINUED Web frameworks are collections of packages or modules which allow developers to write web applications with minimal attention paid to low-level details like protocols, sockets and process management. Common operations implemented by web frameworks: URL routing Output format templating. Database manipulation Basic security

WEB FRAMEWORKS Python has a number of web framework options, but the two most popular are: Django Follows MVC pattern. Most popular. Steeper learning curve. More features built-in. Flask “Micro”-framework: minimal approach. You can get things up and going much faster. Less built-in functionality. Also a popular option.

INTRO TO FLASK We’ll be using Flask due to its ease of use. Django requires a bit more setup and familiarity with the MVC pattern, but once you’re familiar with Flask, you’ll find that learning Django is easier. So, we have the core logic behind our website completed (namely, ticket_scraper.py). Let’s put this aside for a moment and create our website.

INTRO TO FLASK The very first thing is to create a directory structure for all of the files and components of our website and set up our virtual env. The root is ticket_app. ticket_app/ app/ static/ templates/ tmp/

INTRO TO FLASK The very first thing is to create a directory structure for all of the files and components of our website and set up our virtual env. The root is ticket_app. ticket_app/ app/ static/ templates/ tmp/ ticket_app$ virtualenv t_app ticket_app$ source t_app/bin/activate (t_app)ticket_app$ pip install flask For more info on the reasons for this structure, check out this link from the Flask docs.link

INTRO TO FLASK ticket_app/ app/ __init__.py static/ templates/ tmp/ The very first thing we’ll do is create a Flask application object inside of app/__init__.py Import appears at end to avoid circular dependencies: views depends on my_app. from flask import Flask my_app = Flask(__name__) # Create an application object called my_app from app import views # Import the views module from the app package

INTRO TO FLASK Now, let’s create our first view. Views are handlers for requests and are mapped to one or more URLs. ticket_app/ app/ __init__.py views.py static/ templates/ tmp/ Notice the use of decorators!We’ve created a view which displays a simple string to the user when they request URLs ‘/’ and ‘/index’ from app def index(): return "Welcome to the Taylor Swift Concert Ticket-Price Showdown!"

INTRO TO FLASK One last thing we need to do to actually see some stuff. Create the script run.py to start up the development web server with our little application. ticket_app/ run.py app/ __init__.py views.py static/ templates/ tmp/ Note the line up top – we’re telling the shell to use our isolated Python interpreter when we run./run.py. #!t_app/bin/python from app import my_app my_app.run(debug=True)

INTRO TO FLASK Now let’s see what we have. ticket_app/ run.py app/ __init__.py views.py static/ templates/ tmp/ ticket_app$ chmod a+x run.py ticket_app$./run.py After the server initializes, we can check out our site by connecting to localhost:5000.

INTRO TO FLASK Now let’s see what we have. ticket_app/ run.py app/ __init__.py views.py static/ templates/ tmp/ ticket_app$ chmod a+x run.py ticket_app$./run.py We associated both ‘/’ and ‘/index’ with our view function index().

TEMPLATING So far, our only view function merely outputs a single string. ticket_app/ run.py app/ __init__.py views.py static/ templates/ tmp/ from app def index(): return "Welcome to the Taylor Swift Concert Ticket-Price Showdown!"

TEMPLATING We could just as easily return html to be rendered in the browser. ticket_app/ run.py app/ __init__.py views.py static/ templates/ tmp/ from app def index(): return ''' Welcome to the Taylor Swift Concert Ticket-Price Showdown! '''

TEMPLATING We could just as easily return html to be rendered in the browser. ticket_app/ run.py app/ __init__.py views.py static/ templates/ tmp/

TEMPLATING That method is so ugly though. So let’s separate the presentation from the logic by creating a template base.html inside of templates/. ticket_app/ run.py app/ __init__.py views.py static/ templates/ base.html tmp/ We’re using the Jinja2 templating engine, which is included with Flask. Taylor Swift Concert Ticket-Price Showdown Welcome to the Taylor Swift Concert Ticket-Price Showdown! {% block content %} {% endblock %}

TEMPLATING Jinja2 templates allow us to do a lot more than just write HTML. {{ arg }} corresponds to template arguments. I can pass arg=val to my template. {% %} encompasses control statements ( if, for, block, etc). e.g. {% if arg %} arg {% endif %} {% block content %}{% endblock %} identifies a portion of the html (called “content”) in which more content could be inserted.

TEMPLATING That method is so ugly though. So let’s separate the presentation from the logic by creating a template base.html inside of templates/. ticket_app/ run.py app/ __init__.py views.py static/ templates/ base.html tmp/ Now we have a template called base.html which has some basic elements but also includes a control statement which allows us to derive and extend the template. Taylor Swift Concert Ticket-Price Showdown Welcome to the Taylor Swift Concert Ticket-Price Showdown! {% block content %} {% endblock %}

TEMPLATING We’ve created a basic template which we can extend. So now I’ll create templates/index.html which derives from base.html and adds some more content. ticket_app/ run.py app/ __init__.py views.py static/ templates/ base.html index.html tmp/ Now we have a template called index.html which inherits from base.html and specifies the matching block statement “content”. {% extends base.html %} {% block content %} Find the best ticket prices in town with the Taylor Swift Concert Ticket-Price Showdown! {% endblock %}

TEMPLATING One last thing we need to do. Let’s update our views to access our template. ticket_app/ run.py app/ __init__.py views.py static/ templates/ base.html index.html tmp/ Invokes Jinja2 templating engine from flask import render_template from app def index(): return render_template('index.html')

TEMPLATING

FORMS Obviously, we don’t just want to display text to the user, we want to allow them to search for tickets. Naturally, we turn our attention to forms. ticket_app/ run.py app/ __init__.py views.py static/ templates/ base.html index.html tmp/ (t_app)ticket_app$ pip install flask-wtf Let’s install the WTForms extension which integrates WTForms, a form validation and rendering library, with Flask.

FORMS When using extensions with Flask, typically you will have to perform some kind of configuration. This can be done in the “config.py” file at the root of your project. ticket_app/ config.py run.py app/ __init__.py views.py static/ templates/ base.html index.html tmp/ WTF_CSRF_ENABLED = True SECRET_KEY = ‘somesuperdupersecretkey’ config.py For the WTForms extension, we need to specify whether cross-site request forgery protection is enabled and the key to use to validate the form.

FORMS Now, we just modify __init__.py to set up our Flask object to use the settings in our config file. ticket_app/ config.py run.py app/ __init__.py views.py static/ templates/ base.html index.html tmp/ from flask import Flask my_app = Flask(__name__) my_app.config.from_object(config) from app import views

FORMS Let’s create our first form. All we want right now is to be able to accept some string which represents the event name and the location. We’ll create a forms.py inside of app/. ticket_app/ config.py run.py app/ __init__.py views.py forms.py static/ templates/ base.html index.html tmp/

FORMS Let’s create our first form. All we want right now is to be able to accept some string which represents the event name and the location. ticket_app/ config.py run.py app/ __init__.py views.py forms.py static/ templates/ base.html index.html tmp/ from flask.ext.wtf import Form from wtforms import StringField, SelectField from wtforms.validators import DataRequired class EventSearch(Form): event_name = StringField('event_name', validators=[DataRequired()]) location = SelectField('location', choices=[('tallahassee', 'Tallahassee'), ('orlando', 'Orlando')])

FORMS Next, we create a new template for the page on which we will display the form. ticket_app/ config.py run.py app/ __init__.py views.py forms.py static/ templates/ base.html index.html search.html tmp/ For CSRF protection {% extends "base.html" %} {% block content %} {{title}} {{form.hidden_tag()}} Please enter the event you’d like to find tickets for: {{form.event_name(size=80)}} Select a location: {{form.location}} {% endblock %}

FORMS Lastly, to see our form in action, we need to create a route for it. ticket_app/ config.py run.py app/ __init__.py views.py forms.py static/ templates/ base.html index.html search.html tmp/ from flask import render_template from app import my_app from app.forms def index(): return methods=['GET', 'POST']) def search(): form = EventSearch() return render_template('search.html', title='Search for an Event', form=form)

FORMS

Now, we’re displaying a form so let’s actually grab some input. ticket_app/ config.py run.py app/ __init__.py views.py forms.py static/ templates/ base.html index.html search.html tmp/ form.validate_on_submit() returns false if the user hasn’t entered data, true if they have and validators are met. from flask import methods=['GET', 'POST']) def search(): form = EventSearch() if form.validate_on_submit(): event = form.event_name.data location = form.location.data return render_template('search.html', title='Search for an Event', form=form)

FORMS If we gather data, and all of the validators are met, we still render search, but with post info. ticket_app/ config.py run.py app/ __init__.py views.py forms.py ticket_scraper.py static/ templates/ base.html index.html search.html tmp/ from flask import render_template from app.ticket_scraper import get_posts methods=['GET', 'POST']) def search(): posts = [] form = EventSearch() if form.validate_on_submit(): event = form.event_name.data location = form.location.data posts = get_posts(event, location) return render_template('search.html', title='Search for an Event', form=form, posts=posts)

FORMS So here’s what we’re doing: When a user navigates to /search, they can enter their search criteria. When a form is submitted, we use the search information to call our scraping module. Then, we render the search template again with the post information. Now, we need to update the template for search.html to display post information. Note: this is not an ideal setup – we’re mixing backend logic with our application. We’ll call ticket_scraper directly to build our site for now.

DISPLAYING RESULTS To remind ourselves, here’s the contents of search.html. {% extends "base.html" %} {% block content %} {{title}} {{form.hidden_tag()}} Please enter the event youd like to find tickets for: {{form.event_name(size=80)}} Select a location: {{form.location}} {% endblock %}

DISPLAYING RESULTS search.html We enclose our additions to search.html using an if condition – if posts is empty, we don’t display the table of results. This happens when a user first navigates to the search page.... {% if posts %} Title Date Venue Number of Tickets Price/Ticket {% for p in posts %} {{ p[3] }} {{ p[0] }} {{ p[1] }} {{ p[2] }} {{ p[4] }} {% endfor %} {% endif %} {% endblock %}

DISPLAYING RESULTS search.html The post information we gathered using the ticket_scraper module includes: Title Date Venue Number of Tickets Price/Ticket... {% if posts %} Title Date Venue Number of Tickets Price/Ticket {% for p in posts %} {{ p[3] }} {{ p[0] }} {{ p[1] }} {{ p[2] }} {{ p[4] }} {% endfor %} {% endif %} {% endblock %}

DISPLAYING RESULTS We loop through the posts list, which contains the results of ticket_scraper in the form of tuples. For every row in our results table, we’ll print out the information gathered from a post.... {% if posts %} Title Date Venue Number of Tickets Price/Ticket {% for p in posts %} {{ p[3] }} {{ p[0] }} {{ p[1] }} {{ p[2] }} {{ p[4] }} {% endfor %} {% endif %} {% endblock %}

DISPLAYING RESULTS

Well, that looks…terrible. But at least we’re displaying the data! Let’s make it look nicer. We’ll add an external style sheet to our static folder and update our base.html. ticket_app/ config.py run.py app/ __init__.py views.py forms.py ticket_scraper.py static/ style.css templates/ base.html index.html search.html tmp/ Taylor Swift Concert Ticket-Price Showdown! Welcome to the Taylor Swift Concert Ticket-Price Showdown! {% block content %}{% endblock %}

DISPLAYING RESULTS We can use a {{ … }} delimeter which will tell Jinja2 to evaluate the expression inside. Inside, we’ll make a call to url_for which generates a URL to the target. ticket_app/ config.py run.py app/ __init__.py views.py forms.py ticket_scraper.py static/ style.css templates/ base.html index.html search.html tmp/ Taylor Swift Concert Ticket-Price Showdown! Welcome to the Taylor Swift Concert Ticket-Price Showdown! {% block content %}{% endblock %}

DISPLAYING RESULTS Let’s add a navigation menu while we’re at it. Taylor Swift Concert Ticket-Price Showdown! Welcome to the Taylor Swift Concert Ticket-Price Showdown! Home Search {% block content %}{% endblock %}

DISPLAYING RESULTS

We’re probably not going to be featured on CSS Zen Garden or anything, but it looks better. So that’s zero to Flask in 40 slides. Some important things to note: Flask has a TON of extensions: check As I said, we built the “back-end” functionality right into the app here. A smarter way would be to have a reactor loop that picks up scraping requests placed into a queue by the Flask app, and returns the results when it can. This way, we can perform non-blocking scraping in the case of a bunch of users accessing the site at once. We’ve been serving our site using Flask’s development server (app.run()), which is great for development but you should use a full server for deployment. Let’s explore self-hosting with Gunicorn and Nginx.