16
loading...
This website collects cookies to deliver better user experience
Basic-amenities-in-SFO
.pip install greppo
.greppo serve app.py
.mkdir greppo-quickstart
cd greppo-quickstart
greppo
inside a Python virtual environment. For this tutorial we are going to use virtualenv (see here for other environment managers)virtualenv ENV
source ENV/bin/activate
pip install greppo
touch app.py
from greppo import app
import geopandas as gpd
app
is the entry point for adding interactive and visual components to your application. With app
we can instruct greppo to add a map layer, or a bar chart, or set up APIs to variables. Geopandas is imported as gpd
by convention to read vector data.sfo_amenities = gpd.read_file("./SFO_Amenities.geojson")
amenities = list(sfo_amenities['amenity'].unique())
.unique()
.app.base_layer(
name="CartoDB Light",
visible=True,
url="https://cartodb-basemaps-a.global.ssl.fastly.net/light_all/{z}/{x}/{y}@2x.png",
subdomains=None,
attribution='© <a target="_blank" href="http://osm.org/copyright">OpenStreetMap</a> contributors | Carto Tile',
)
app.overlay_layer(
sfo_amenities,
title="SFO Amenities",
description="Location of some basic amenities in San Francisco",
style={"fillColor": "#F87979"},
visible=True,
)
overlay_layer
API, and the first argument is the geojson file that we read in step 1.default_area_selection = gpd.read_file("./SFO_Selection.geojson")
area_selection = app.draw_feature(
name="Draw area selection", features=default_area_selection, geometry=["Polygon"]
)
default_area_selection
).area_selection
is a list of polygons. For interactivity Greppo binds the state of the user interaction to this variable area_selection
. That is, whenever app.py runs or is updated, area_selection
will contain all the polygons as seen on the screen. To be clear, app.py can always assume area_selection will have the data it cares about and can perform computations on that (see next step).selected_amenities = sfo_amenities.loc[sfo_amenities.within(
area_selection.at[0, 'geometry'])]
selected_amenities_count_df = selected_amenities['amenity'].value_counts()
area_selection
and filter out the counts of amenities within the selected_amenities
data frame, and create a new data frame selected_amenities_count_df
.selected_amenities_count = []
for amenity in amenities:
if(amenity in selected_amenities_count_df.index):
selected_amenities_count.append(
selected_amenities_count_df[amenity].item())
else:
selected_amenities_count.append(0)
app.bar_chart(
name="amenities-sfo-select",
title="Amenities in SFO",
description="The count of the basic amenities within the selected area in SFO.",
x=amenities,
y=selected_amenities_count,
backgroundColor="rgb(200, 50, 150)",
)
app.py
ready, let’s serve the app with Greppo and look at our output!greppo serve app.py
.pyc
for fast execution)INFO: Started server process [76886]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Uvicorn running on http://0.0.0.0:8080 (Press CTRL+C to quit)
http://0.0.0.0:8080
in your favorite browser and viola!Save
.