diff --git a/app/processing.py b/app/processing.py new file mode 100644 index 0000000..764b84b --- /dev/null +++ b/app/processing.py @@ -0,0 +1,49 @@ +from base64 import b64encode +from io import BytesIO + +from folium import Map + +from app.preprocessing import create_dataframe +from constants import COORDINATES + + +def get_figure(plot): + plot_figure = plot.get_figure() + figure = BytesIO() + plot_figure.savefig(figure) + figure.seek(0) + return figure + + +def render_plot(df): + df.fillna(value=0, inplace=True) + plot = df.plot("year", ["surface", "length", "elevation"], kind="bar") + figure = get_figure(plot) + return figure + + +def encode_plot(figure): + plot = b64encode(figure.getvalue().decode()) + return plot + + +def create_plot(df): + figure = render_plot(df) + plot = encode_plot(figure) + return plot + + +def create_map(df): + map = Map(location=COORDINATES) + for row in df.iterrows(): + map.simple_marker( + location=[row["fields.geo_shape.coordinates"]], clustered_marker=True + ) + return map + + +def process_data(dataset): + df = create_dataframe(dataset) + plot = create_plot(df) + map = create_map(df) + return plot, map diff --git a/app/routes.py b/app/routes.py index 41fb0b6..d3e075e 100644 --- a/app/routes.py +++ b/app/routes.py @@ -2,6 +2,7 @@ from flask import render_template from app import app from app.forms import DatasetForm +from app.processing import process_data @app.route("/") @@ -19,5 +20,8 @@ def data(): @app.route("/visualization") -def visualization(): - return render_template("visualization.html", title="Visualization", form=form) +def visualization(form): + plot, map = process_data(form.dataset) + return render_template( + "visualization.html", title="Visualization", plot=plot, map=map + ) diff --git a/app/templates/visualization.html b/app/templates/visualization.html index bb7c02a..0d3dd28 100644 --- a/app/templates/visualization.html +++ b/app/templates/visualization.html @@ -3,7 +3,7 @@ {% block app_content %}