Step 5: Add data to the map
Concept: Tethys provides a map component, and OpenLayers components for layers and data sources. We'll show urban-area polygons loaded from a remote GeoJSON file.
lib.tethys.Map(
style=lib.Style(height="70vh", marginBottom="1em")
)(
lib.ol.layer.Vector(
lib.ol.source.Vector(
options=lib.Props(
url="https://d2ad6b4ur7yvpq.cloudfront.net/naturalearth-3.3.0/ne_50m_urban_areas.geojson",
format="GeoJSON",
)
)
)
)
Reading the component syntax
This is the core pattern you'll use everywhere, so it's worth slowing down:
- Props are keyword arguments.
lib.tethys.Map(style=...)configures the component with specific styles - Children are standard positional arguments to the Component if there are no kwargs, or they are positional arguments to a subsequent, chained call of the Component if there the Component requires both Props and Children
- Children are from a chained call on the result. Because
ol.tethys.Maphas astyleconfigured, the childlib.ol.layer.Vectormust be passed to a chained call. However,lib.ol.layer.Vectorhas children and no props, and so thelib.ol.source.Vectorcomponent can be passed directly tolib.ol.layer.Vectorrather than as a nested call.
Key ideas
lib.tethys.Maprenders an interactive map and takes layers as children.lib.olis the namespace for the OpenLayers package. Alayer.Vectorcontains asource.Vectorthat defines the underlying features/data that in this case comes from a GeoJSON URL.lib.Props, likelib.Styleis a helper function that allows you to continue to write pythonic, kwarg-style syntax for the key-value pairs rather than dictionary syntax (e.g.options=lib.Props(url="...", format="GeoJSON")rather thanoptions={"url": "...", "format": "GeoJSON"}). You may use whichever you prefer.
Full home() so far
@App.page
def home(lib):
return lib.tethys.Display(
lib.tethys.Map(
style=lib.Style(height="70vh", marginBottom="1em")
)(
lib.ol.layer.Vector(
lib.ol.source.Vector(
options=lib.Props(
url="https://d2ad6b4ur7yvpq.cloudfront.net/naturalearth-3.3.0/ne_50m_urban_areas.geojson",
format="GeoJSON",
)
)
)
),
lib.m.Text("Steps go here")
)
What you should see
Reload the app and you should now see urban-area polygons rendering on the map.