Render table and folium map as iframe

This commit is contained in:
coolneng 2020-06-14 00:58:52 +02:00
parent a36cc719ef
commit dd2538f1ea
Signed by: coolneng
GPG Key ID: 9893DA236405AF57
5 changed files with 31 additions and 49 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
**/__pycache__ **/__pycache__
Design.org Design.org
data/*.json data/*.json
app/templates/map.html

View File

@ -1,16 +1,25 @@
from json import load from folium import Map, Marker
from pandas import DataFrame, json_normalize from pandas import DataFrame, json_normalize
from constants import COLUMNS, FILES
from app.data_request import request_dataset from app.data_request import request_dataset
from constants import COLUMNS, COORDINATES
def create_dataframe(dataset) -> DataFrame: def create_dataframe(dataset) -> DataFrame:
""" """
Creates a DataFrame from a JSON file Creates a DataFrame from a JSON response
""" """
json = request_dataset(dataset) json = request_dataset(dataset)
df = json_normalize(data=json, record_path=["records"], errors="ignore",) df = json_normalize(data=json, record_path=["records"], errors="ignore",)
filtered_df = df.filter(items=COLUMNS[dataset]) filtered_df = df.filter(items=COLUMNS[dataset])
return filtered_df return filtered_df
def create_map(df):
"""
Creates a Map with markers from the DataFrame
"""
m = Map(location=COORDINATES, zoom_start=12)
for index, row in df.iterrows():
Marker(location=row["fields.geo_shape.coordinates"]).add_to(m)
m.save("app/templates/map.html")

View File

@ -1,49 +1,17 @@
from base64 import b64encode from base64 import b64encode
from io import BytesIO from io import BytesIO
from folium import Map from app.preprocessing import create_dataframe, create_map
from app.preprocessing import create_dataframe
from constants import COORDINATES
def get_figure(plot): def create_table(df) -> str:
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) df.fillna(value=0, inplace=True)
plot = df.plot("year", ["surface", "length", "elevation"], kind="bar") table = df.to_html(classes=["table-striped", "table-hover"])
figure = get_figure(plot) return table
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): def process_data(dataset):
df = create_dataframe(dataset) df = create_dataframe(dataset)
plot = create_plot(df) table = create_table(df)
map = create_map(df) map = create_map(df)
return plot, map return table, map

View File

@ -11,17 +11,20 @@ def index():
return render_template("index.html", title="Home Page") return render_template("index.html", title="Home Page")
@app.route("/data") @app.route("/data", methods=["GET", "POST"])
def data(): def data():
form = DatasetForm() form = DatasetForm()
if form.validate_on_submit(): if form.validate_on_submit():
return render_template("visualization.html", form=form, title="Visualization") table = process_data(form.dataset.data)
return render_template("visualization.html", title="Visualization", table=table)
return render_template("data.html", title="Data", form=form) return render_template("data.html", title="Data", form=form)
@app.route("/visualization") @app.route("/visualization")
def visualization(form): def visualization():
plot, map = process_data(form.dataset) return render_template("visualization.html", title="Visualization", table=table)
return render_template(
"visualization.html", title="Visualization", plot=plot, map=map
) @app.route("/map")
def map():
return render_template("map.html", title="Map")

View File

@ -3,6 +3,7 @@
{% block app_content %} {% block app_content %}
<h1>Dataset visualization</h1> <h1>Dataset visualization</h1>
<iframe class="map", src="/map" width="500" height="500"></iframe>
{{ table|safe }} {{ table|safe }}
<p><a href="{{ url_for('data') }}">Back</a></p> <p><a href="{{ url_for('data') }}">Back</a></p>
{% endblock %} {% endblock %}