This commit is contained in:
louiscklaw
2025-01-31 22:17:25 +08:00
parent cdc3678990
commit 3688f9ee24
100 changed files with 65454 additions and 0 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,5 @@
from flask import Flask
app = Flask(__name__)
from flaskapp import routes

View File

@@ -0,0 +1,26 @@
""" routes.py - Flask route definitions
Flask requires routes to be defined to know what data to provide for a given
URL. The routes provided are relative to the base hostname of the website, and
must begin with a slash."""
from flaskapp import app
from flask import render_template
from wrangling_scripts.Q5 import data_wrangling, username
header, table = data_wrangling()
# The following two lines define two routes for the Flask app, one for just
# '/', which is the default route for a host, and one for '/index', which is
# a common name for the main page of a site.
#
# Both of these routes provide the exact same data - that is, whatever is
# produced by calling `index()` below.
@app.route('/')
@app.route('/index')
def index():
"""Renders the index.html template"""
# Renders the template (see the index.html template file for details). The
# additional defines at the end (table, header, username) are the variables
# handed to Jinja while it is processing the template.
return render_template('index.html', table=table, header=header,
username=username())

View File

@@ -0,0 +1,46 @@
body {
font-family: Helvetica, Arial, sans-serif;
}
table {
border-collapse: collapse;
border: 1px solid black;
}
table > caption {
margin-bottom: 10px;
}
table > caption > h1 {
font-size: 2em;
font-weight: bold;
margin: 0 0 0 0;
}
thead {
border-bottom: 1px solid black;
background-color: #DCDCDC;
}
thead th {
font-weight: bold;
text-align: left;
}
td, th {
padding-left: 10px;
padding-right: 10px;
}
tbody > tr:first-child {
border-top: 1.5px solid black;
}
tbody tr:hover {
background-color: #F5F5F5;
}
tbody tr.star-trek {
font-style: italic;
color: blue;
}

View File

@@ -0,0 +1,53 @@
{#- This is a Jinja template - Flask uses Jinja as its templating engine. Jinja
will process this file looking for directives to define a template based on
the passed-in variables. This template relies on the `table`, `header`, and
`username` variables to be declared.
A Jinja directive will start with a "{%" and Jinja comments will begin with
a "{#". Both are ended with the same symbol and the closing brace "}".
If a dash "-" is provided before the opening or closing symbol for a
directive, Jinja will ignore all whitespace up to that side of the directive
in the template. -#}
<!doctype html>
<html>
<head>
<title>Flask Example</title>
<link rel="stylesheet" href="/static/style.css">
</head>
<body>
<table>
<caption>
<h1>Movies</h1>
{#- Templates can output variables inside {{...}}, like below #}
<small>Data wrangled by {{username}}</small>
</caption>
<thead>
<tr>
{#- Templates can also iterate over variables, producing the contents
of the template (between the "for" and the "endfor" directives) for
each element in the specified collection #}
{%- for col in header %}
<th>{{ col }}</th>
{%- endfor %}
</tr>
</thead>
<tbody>
{%- for row in table %}
{#- It is also possible to add "if" statements, the contents of these
templates (between the "if" and "endif" directives) are inserted only
if the supplied statement evaluates to `True`.
The directive in the line below will only add the class "star-trek"
if the title contains the string "Star Trek" exactly. #}
<tr {%- if 'Star Trek' in row[1] %} class='star-trek' {%- endif %}>
{%- for col in row %}
<td>{{ col }}</td>
{%- endfor %}
</tr>
{%- endfor -%}
</tbody>
</table>
</body>
</html>

View File

@@ -0,0 +1,8 @@
---
tags: [python, flask]
---
# task5
ttps://share.louislabs.com/g/u-iOAl1Q6J_

7
tunmnlu/task_1/Q5/run.py Normal file
View File

@@ -0,0 +1,7 @@
""" run.py - Run the Flask app """
from flaskapp import app
if __name__ == '__main__':
# Tells Flask to run, accessible from the specified host/port pair. Note
# that the routes are loaded because of the import above.
app.run(host='127.0.0.1', port=3001, debug=True)

View File

@@ -0,0 +1,8 @@
#!/usr/bin/env bash
set -ex
export PYTHONDONTWRITEBYTECODE=1
rm -rf **/__pycache__
python ./run.py

View File

@@ -0,0 +1,35 @@
"""
cse6242 s23
Q5.py - utilities to supply data to the templates.
This file contains a pair of functions for retrieving and manipulating data
that will be supplied to the template for generating the table. """
import csv
def username():
return 'tlou31'
def data_wrangling():
with open('data/movies.csv', 'r', encoding='utf-8') as f:
reader = csv.reader(f)
table = list()
noOfRows = 100
# Feel free to add any additional variables
...
# Read in the header
for header in reader:
break
# Read in each row
for i, row in enumerate(reader):
if i <= noOfRows - 1:
table.append(row)
else: break
# Only read first 100 data rows - [2 points] Q5.a
# Order table by the last column - [3 points] Q5.b
table = sorted(table, key=lambda t : float(t[2]), reverse=True)
return header, table