Add Glacier name search for table querying
This commit is contained in:
parent
1aac68473d
commit
c6e63cf087
|
@ -21,6 +21,7 @@ class YearForm(FlaskForm):
|
|||
("2017", 2017),
|
||||
("2018", 2018),
|
||||
]
|
||||
name = StringField("Glacier Name")
|
||||
year = SelectField("Year", validators=[DataRequired()], choices=year_list)
|
||||
submit = SubmitField("Search")
|
||||
|
||||
|
|
|
@ -58,13 +58,20 @@ def data():
|
|||
def table_selection():
|
||||
form = YearForm()
|
||||
if form.validate_on_submit():
|
||||
annual_data = (
|
||||
db.session.query(Annual_Data).filter_by(year=form.year.data).statement
|
||||
annual_data = db.session.query(Annual_Data).filter_by(year=form.year.data)
|
||||
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:
|
||||
flash("Invalid query, please try again")
|
||||
if query.scalar() is None:
|
||||
flash("Sorry, no results found")
|
||||
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_selection.html", title="Data", form=form)
|
||||
|
||||
|
|
|
@ -2,6 +2,6 @@
|
|||
{% import 'bootstrap/wtf.html' as wtf %}
|
||||
|
||||
{% block app_content %}
|
||||
<h1>Table</h1>
|
||||
{{ table|safe }}
|
||||
<h1>Table</h1>
|
||||
{{ table|safe }}
|
||||
{% endblock %}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
from app import db
|
||||
from pandas import DataFrame, read_sql, wide_to_long
|
||||
from pandas import DataFrame, read_sql
|
||||
|
||||
|
||||
def create_dataframe(query) -> DataFrame:
|
||||
|
@ -8,7 +8,8 @@ def create_dataframe(query) -> DataFrame:
|
|||
|
||||
|
||||
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
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue