Updating initial_view_state dynamically does not work
See original GitHub issueSummary
I’m trying to let my user easily switch to another location with a st.selectbox
or change the zoom or pitch via sliders. But it does not work.
Steps to reproduce
What are the steps we should take to reproduce the bug:
- Run the code below
- Try changing the initial_view_state settings via the app
Code
import pathlib
import pandas as pd
import pydeck as pdk
import streamlit as st
import numpy as np
POWER_PLANT_URL = (
"https://raw.githubusercontent.com/MarcSkovMadsen/awesome-streamlit/master/"
"gallery/global_power_plant_database/global_power_plant_database.csv"
)
LOCATIONS = {
"Orsted Copenhagen HQ": {"latitude": 55.676098, "longitude": 12.568337},
"Orsted Boston": {"latitude": 2.361145, "longitude": -71.057083},
}
ORSTED_CPH_HQ = LOCATIONS["Orsted Copenhagen HQ"]
class ViewStateComponent:
def __init__(self):
self.latitude = ORSTED_CPH_HQ["latitude"]
self.longitude = ORSTED_CPH_HQ["longitude"]
self.zoom = 3
self.pitch = 40.0
def edit_view(self):
location = st.sidebar.selectbox("Location", options=list(LOCATIONS.keys()), index=0)
self.latitude = LOCATIONS[location]["latitude"]
self.longitude = LOCATIONS[location]["longitude"]
self.zoom = st.sidebar.slider("Zoom", min_value=0, max_value=11, value=self.zoom)
self.pitch = st.sidebar.slider(
"Pitch", min_value=0.0, max_value=100.0, value=self.pitch, step=10.0
)
@property
def view_state(self) -> pdk.ViewState:
return pdk.ViewState(
longitude=self.longitude,
latitude=self.latitude,
zoom=self.zoom,
min_zoom=1,
max_zoom=15,
pitch=self.pitch,
# bearing=-27.36,
)
class GlobalPowerPlantDatabaseApp:
def __init__(self):
self.view_state_component = ViewStateComponent()
self.data = self.get_data()
@staticmethod
@st.cache
def get_data():
data = pd.read_csv(POWER_PLANT_URL)
return data[["latitude", "longitude"]].dropna()
def _scatter_plotter_layer(self):
return pdk.Layer(
"ScatterplotLayer",
data=self.data,
get_position=["longitude", "latitude"],
extruded=True,
get_radius=5000,
pickable=True,
opacity=0.8,
stroked=False,
filled=True,
wireframe=True,
)
def view(self):
self.view_state_component.edit_view() # Does not work
st.pydeck_chart(
pdk.Deck(
map_style="mapbox://styles/mapbox/light-v9",
initial_view_state=self.view_state_component.view_state,
layers=[self._scatter_plotter_layer()],
)
)
app = GlobalPowerPlantDatabaseApp()
app.view()
Debug info
- Streamlit version: 0.53
- Python version: 3.7.4
python -m venv .venv
- OS version: Windows 8.1
- Browser version: Chrome
Issue Analytics
- State:
- Created 4 years ago
- Reactions:8
- Comments:6 (1 by maintainers)
Top Results From Across the Web
Updating State Object Does Not Rerender Dynamically ...
Try to refactor the createButtons method to a Component function and provide the used fields as properties. When calling functions inside ...
Read more >react-map-gl | default (Map) - GitHub Pages
The initial view state of the map. If specified, longitude , latitude , zoom etc. in props are ignored when constructing the map....
Read more >What's New - deck.gl
When using Deck as a stateful component, you can now update its initialViewState prop to reset the camera. A new prop onError is...
Read more >Dynamically Update States in React | by Kate Stamas - Medium
You are not going to want to have a separate onChange() or handleChange() ... So how would we use one function to dynamically...
Read more >fetchLayerData - CARTO for deck.gl | CARTO Documentation
Reference The CARTO submodule for deck.gl is open source, so we maintain the ... If not using the CARTO 3 API version, you...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Working a PR on this now! Hoping to have something out by EOD, though I’ll likely need a bit of code review as I’m not at all a typescript person.
I am still unable to update the initial_view_state (showing a map with interactively entered lat/long) with the latest streamlit (0.58) and pydeck (0.3.1) versions. Should we expect this functionality to exist now? Any workarounds known?