Add Glacier name search for table querying

This commit is contained in:
coolneng 2020-01-09 20:56:17 +01:00
parent 1aac68473d
commit c6e63cf087
Signed by: coolneng
GPG Key ID: 9893DA236405AF57
4 changed files with 20 additions and 11 deletions

View File

@ -21,6 +21,7 @@ class YearForm(FlaskForm):
("2017", 2017), ("2017", 2017),
("2018", 2018), ("2018", 2018),
] ]
name = StringField("Glacier Name")
year = SelectField("Year", validators=[DataRequired()], choices=year_list) year = SelectField("Year", validators=[DataRequired()], choices=year_list)
submit = SubmitField("Search") submit = SubmitField("Search")

View File

@ -58,13 +58,20 @@ def data():
def table_selection(): def table_selection():
form = YearForm() form = YearForm()
if form.validate_on_submit(): if form.validate_on_submit():
annual_data = ( annual_data = db.session.query(Annual_Data).filter_by(year=form.year.data)
db.session.query(Annual_Data).filter_by(year=form.year.data).statement query = annual_data
if form.name.data:
query = (
db.session.query(Annual_Data)
.filter_by(year=form.year.data)
.join(Glacier, Glacier.id == Annual_Data.id)
.filter_by(name=form.name.data)
.group_by(Glacier.id)
) )
if annual_data is None: if query.scalar() is None:
flash("Invalid query, please try again") flash("Sorry, no results found")
return redirect(url_for("table_selection")) return redirect(url_for("table_selection"))
table = create_table(annual_data) table = create_table(query.statement)
return render_template("table.html", table=table, title="Table") return render_template("table.html", table=table, title="Table")
return render_template("table_selection.html", title="Data", form=form) return render_template("table_selection.html", title="Data", form=form)

View File

@ -1,5 +1,5 @@
from app import db from app import db
from pandas import DataFrame, read_sql, wide_to_long from pandas import DataFrame, read_sql
def create_dataframe(query) -> DataFrame: def create_dataframe(query) -> DataFrame:
@ -8,7 +8,8 @@ def create_dataframe(query) -> DataFrame:
def render_table(df) -> str: def render_table(df) -> str:
table = df.to_html(classes=["table-bordered", "table-striped", "table-hover"]) df.fillna(value=0, inplace=True)
table = df.to_html(classes=["table-striped", "table-hover"])
return table return table