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 %}

Dataset visualization

-Image Placeholder -Image Placeholder +Plot Placeholder +Map Placeholder

Back

{% endblock %} diff --git a/constants.py b/constants.py index 05e324f..ffe862f 100644 --- a/constants.py +++ b/constants.py @@ -48,3 +48,4 @@ COLUMNS = { ], } SECRET_KEY = "trolaso" +COORDINATES = [48.864716, 2.349014] diff --git a/shell.nix b/shell.nix index 95ab49b..ec4e926 100644 --- a/shell.nix +++ b/shell.nix @@ -13,6 +13,7 @@ pkgs.mkShell { matplotlib folium pytest + selenium # Development tools black isort