Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Labs/Lab9/Flask Lab 2.docx
Binary file not shown.
46 changes: 46 additions & 0 deletions Labs/Lab9/Flask Lab 2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
Flask Lab 2
In this lab we�ll be implementing a simple survey web app using Flask.
The general concepts we�ll be covering are:
* Routing
* Templating
* Passing Parameters
* Sessions
We will walk through this lab in class and for homework you should redesign the questions and the survey and get creative. Also, make the e-mail link work and send the survey results to us!
Requirements
1. Download the flaskLab2.zip file from bCourses>Files>Labs
Creating a login page
2. Open the �login.html� template in your text editor
a. Make this file an extension of the �base.html� template
b. Use the existing form and create form inputs that capture the user�s name and email
c. Make sure the HTML in this file is replacing the block called �content� in the �base.html� template
d. Make the form submit to the �/login� endpoint
Creating a session
3. Open the �views.py� file in your text editor
a. Find the route for �/login�
b. Within the �login()� function, if the request method is POST
i. set the session�s �username� key to whatever the user entered for their username
ii. Redirect the user to the �index� route
Extending and reusing content from the �base.html� template for the �survey.html� template
4. Open the �survey.html� template in your text editor
a. Make this file an extension of the �base.html� template
b. The HTML in this file for the form with the id of �logout� is currently replacing the entire contents of the <header> tag in the �base.html� template. Using the super() function, pull in the original contents of the <header> tag from the �base.html� template
c. Make sure the HTML in this file from the <h1> and down is replacing the block called �content� in the �base.html� template
Creating an Ajax request with the user�s form inputs from �survey.html�
5. Open the �interaction.js� script in your text editor
a. Notice that the button from the �survey.html� template with the id �submit-survey� has a click event binding
b. Also notice that there are variable declarations for each of the form input fields
c. Create a $.post() Ajax request within this �click� event handler
i. The url for this Ajax request should point to �submit-survey�
ii. The data parameter of this Ajax request should be an object whose key-value pairs correspond to the variables for each form input field
iii. The success function for this Ajax request should set the innerHTML of document.body.parentNode to the response data object
Rendering the survey results via Flask
6. Go back to the �views.py� file in your text editor
a. Find the route for �/submit-survey�
b. You�ll notice that an empty object has been assigned to the variable name �surveyResponse�
c. You�ll also notice that �fe-before� and �fe-after� keys in the �surveyResponse� object have been assigned values that correspond to values in the data object we passed in from step 5 above.
i. Assign the keys �color�, �food�, and �vacation� for the �surveyResponse� object to corresponding values from the passed-in data object in a similar fashion
7. You should now be able to do the following:
a. Log into this simple survey web app with a username and email
b. Take the survey
c. See the survey results displayed

Binary file added Labs/Lab9/flaskLab2/.spyderworkspace
Binary file not shown.
4 changes: 4 additions & 0 deletions Labs/Lab9/flaskLab2/app/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from flask import Flask

myapp = Flask(__name__)
from app import views
Binary file added Labs/Lab9/flaskLab2/app/__init__.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
48 changes: 48 additions & 0 deletions Labs/Lab9/flaskLab2/app/static/interactions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
$('#submit-survey').on('click', function submitSurvey() {
var background = $("input[name=background]").val();
var language = $("input[name=language]").val();
var course = $("input[name=course]").val();
var feBefore = $("input[name=front-end-before]").val();
var feAfter = $("input[name=front-end-after]").val();
$.post("submit-survey",
{background: background,
language: language,
course: course,
feBefore: feBefore,
feAfter: feAfter},
function(data) {
$("html").html(data);
});
});

$("#results-email-container").on('click', '#email-results-button', function emailResults() {
$.post("/send_email",
{
"destination_email": $("#visitor-email").val()
},
function response(data) {
$("#results-email-container").html(data.statusMsg)
},
"json"
)

console.log($(this));
});

$("#site-title-wrapper").on('click', function goHome() {
window.location.href = '/';
});

$(document).ready(function applySliderLabels() {
var currentValue = $("#fe-before").val();
$("#fe-before").next().html(currentValue);

currentValue = $("#fe-after").val();
$("#fe-after").next().html(currentValue);
});


$("input[type='range']").on('change', function updateLabel() {
var currentValue = $(this).val();
$(this).next().html(currentValue);
});
225 changes: 225 additions & 0 deletions Labs/Lab9/flaskLab2/app/static/main.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
/*----------------- CSS RESET ------------------*/

html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
font: inherit;
vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure,
footer, header, hgroup, menu, nav, section {
display: block;
}
body {
line-height: 1;
}
ol, ul {
list-style: none;
}
blockquote, q {
quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
content: '';
content: none;
}
table {
border-collapse: collapse;
border-spacing: 0;
}

/*----------------- CSS RESET ------------------*/

html, body {
background-color: #f9f9f9;
font-family: 'Open Sans', sans-serif;
font-size: 16px;
}

header {
background-color: #1abc9c;
box-shadow: 0 2px 6px 1px #a7a7a7;
color: #ecf0f1;
height: 5rem;
}

footer {
background-color: #16a085;
bottom: 0;
color: #ecf0f1;
height: 7rem;
margin-top: 2em;
text-align: center;
width: 100%;
}

footer p {
height: 7rem;
line-height: 7rem;
}

h1, h3 {
padding: 4px;
}

h1 {
font-size: 2rem;
font-weight: bold;
}

h3 {
font-size: 1.4rem;
}

a {
text-decoration: none;
}

input[type="text"], input[type="range"] {
border: 1px solid #bdc3c7;
border-radius: 2px;
margin-left: 1rem;
vertical-align: middle;
}

input[type=range]{
-webkit-appearance: none;
}

input[type=range]::-webkit-slider-thumb {
-webkit-appearance: none;
border: none;
height: 1.2rem;
width: 1.2rem;
border-radius: 50%;
background: #1abc9c;
margin-top: -.5rem;
}

input[type=range]::-webkit-slider-runnable-track {
background: #ccc;
height: .2rem;
}

.action-button {
background-color: #1abc9c;
border: none;
border-bottom: 3px solid #16a085;
border-radius: 2px;
color: #ecf0f1;
display: inline-block;
font-size: 1rem;
font-weight: bold;
height: 3rem;
padding: 4px;
width: 6rem;
}

.action-button:hover {
background-color: #16a085;
}

#site-title-wrapper {
display: inline-block;
height: 5rem;
width: 15%;
}

#site-icon-wrapper {
display: inline-block;
margin-left: 1rem;
margin-top: -.5rem;
vertical-align: middle;
width: 2.5rem;
}

#site-icon-wrapper img {
display: inline-block;
max-height: 100%;
max-width: 100%
}

#site-title {
display: inline-block;
font-weight: bold;
height: 5rem;
line-height: 5rem;
margin-left: .1rem;
}

#username {
margin-right: .7rem;
}

#logout {
float: right;
height: inherit;
line-height: 5rem;
margin-right: 1rem;
}

#logout-button {
background-color: #ecf0f1;
border-bottom-color: #bdc3c7;
color: #222222;
height: 2.5rem;
width: 5rem;
}

#logout-button:hover {
background-color: #bdc3c7;
}

#content {
margin-top: 2rem;
text-align: center;
width: 100%;
}

.main-container {
background: #ecf0f1;
border-radius: 4px;
box-shadow: 0px 2px 10px 2px #95a5a6;
display: block;
min-height: 50vh;
margin: 2rem 0 0 25%;
padding: 1rem;
text-align: left;
width: 50%;
}

.survey-item, .result-item, #results-email-container {
box-sizing: border-box;
display: block;
margin: 1.5rem 0;
padding: 4px;
}

.survey-item span {
font-size: 1rem;
margin-left: 1rem;
}

#email-results-button, #goHome {
height: 2rem;
line-height: 2rem;
margin-left: 1rem;
text-align: center;
}

33 changes: 33 additions & 0 deletions Labs/Lab9/flaskLab2/app/templates/base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!DOCTYPE html>

<html>
<head>
<meta charset="UTF-8">
<title>{% block title %}{% endblock %} - Ape Ask</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<link href='https://fonts.googleapis.com/css?family=Open+Sans:400,700' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="{{ url_for('static', filename='main.css') }}">
{% block styles %}{% endblock %}
</head>

<body>

<header>
{% block header %}
<div id="site-title-wrapper">
<div id="site-icon-wrapper">
<img src="http://icons.iconarchive.com/icons/iconsmind/outline/512/Gorilla-icon.png">
</div>
<h3 id="site-title">Ape Ask</h3>
</div>
{% endblock %}
</header>
<div id="content">{% block content %}{% endblock %}</div>

<footer>
<p>&copy; Copyright 2016 Ape Ask, all rights reserved</p>
</footer>

<script type="text/javascript" src="{{ url_for('static', filename='interactions.js') }}"></script>
</body>
</html>
18 changes: 18 additions & 0 deletions Labs/Lab9/flaskLab2/app/templates/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{% extends "base.html" %}

{% block title %}
Login
{% endblock %}

{% block content %}
<h1>Hi There! Welcome to Ape Ask, the leading online survey site.</h1>
<div class="main-container">
<h3>Let's get you logged in</h3>
<form action="login" method='post'>
<label for="login" method="post">
<label for="username">username</label><input type="text" name="username"></input>
<label for="email">email</label><input type="text" name="email"></input>
<input class="action-button" id="login" type="submit" value="Submit">
</form>
</div>
{% endblock %}
13 changes: 13 additions & 0 deletions Labs/Lab9/flaskLab2/app/templates/not_authorized.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>

<html>
<head>
<title>Oops!</title>
</head>
<body>
<h1>Uh Oh! You're not authorized</h1>
<img src="https://cammy-marketing.s3.amazonaws.com/2015/03/1425373005/safe-cracking-burglar.jpg">
<br>
<a href="/">Take me back home, already!</a>
</body>
</html>
Loading