Skip to content
Open

Lab 9 #213

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 not shown.
Binary file added Labs/Lab8-Flask_Simple_Form/lab-flask1.zip
Binary file not shown.
4 changes: 4 additions & 0 deletions Labs/Lab8-Flask_Simple_Form/lab-flask1/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 not shown.
Binary file not shown.
9 changes: 9 additions & 0 deletions Labs/Lab8-Flask_Simple_Form/lab-flask1/app/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from app import myapp
from flask import request,render_template

@myapp.route('/')
@myapp.route('/index')
def index():
print("This is a log ..comes on your console")
return "Hello - this the page for your form !!"

2 changes: 2 additions & 0 deletions Labs/Lab8-Flask_Simple_Form/lab-flask1/runserver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from app import myapp
myapp.run(debug=True)
Binary file added Labs/Lab9-Flask_Survey/Flask Lab 2.docx
Binary file not shown.
46 changes: 46 additions & 0 deletions Labs/Lab9-Flask_Survey/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-Flask_Survey/flaskLab2.zip
Binary file not shown.
Binary file not shown.
Binary file modified Labs/lab-SQL/Lab-SQLite/app.db
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
14 changes: 13 additions & 1 deletion Labs/lab-SQL/Lab-SQLite/app/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
import sqlite3 as sql

def insert_customer():

def insert_customer(company, email):
# SQL statement to insert into database goes here
with sql.connect("app.db") as con:
cur = con.cursor()
cur.execute(
"INSERT INTO customers (company, email) VALUES (?, ?)", (company, email))
con.commit()


def retrieve_customers():
# SQL statement to query database goes here
with sql.connect("app.db") as con:
con.row_factory = sql.Row
cur = con.cursor()
result = cur.execute("select * from customers").fetchall()
return result
6 changes: 6 additions & 0 deletions Labs/lab-SQL/Lab-SQLite/app/templates/customer.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ <h2>Add Customer to Our Database</h2>
<div class="divider"></div>
<div class="section">
<!--add input fields-->
<p> Company name:<br>
{{ form.company(size=120) }}<br>
</p>
<p> Customer email:<br>
{{ form.email(size=120) }}<br>
</p>
</div>
<div class="divider"></div>
<div class="section">
Expand Down
8 changes: 8 additions & 0 deletions Labs/lab-SQL/Lab-SQLite/app/templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ <h3>These are all of our awesome customers:</h3>
</tr>
</thead>
<!-- code to display all user -->

{% for customer in customers %}
<tr>
<td> {{ customer['company']}} </td>
<td> {{ customer['email']}} </td>
</tr>
{% endfor %}

</table>
</div>
</div>
Expand Down
10 changes: 8 additions & 2 deletions Labs/lab-SQL/Lab-SQLite/app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,23 @@
def index():
return redirect('/create_customer')


@app.route('/create_customer', methods=['GET', 'POST'])
def create_customer():
form = CustomerForm()
if form.validate_on_submit():
# Get data from the form
# Send data from form to Database
company = form.company.data
email = form.email.data
models.insert_customer(company, email)
return redirect('/customers')
return render_template('customer.html', form=form)


@app.route('/customers')
def display_customer():
#Retreive data from database to display
# Retreive data from database to display
customers = models.retrieve_customers()
return render_template('home.html',
customers=customers)
customers=customers)
6 changes: 6 additions & 0 deletions Labs/lab-SQL/Lab-SQLite/schema.sql
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
-- Insert code to create Database Schema
-- This will create your .db database file for use
drop table if exists customers;
create table customers (
customer_id integer primary key,
company text not null,
email text not null
);
Binary file modified Labs/lab-SQL/Pokemon.db
Binary file not shown.