Skip to content

Commit

Permalink
Add id to map api (streamlit#7307)
Browse files Browse the repository at this point in the history
  • Loading branch information
willhuang1997 authored and eric-skydio committed Dec 20, 2023
1 parent 148dee0 commit bffee99
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
20 changes: 17 additions & 3 deletions lib/streamlit/elements/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"""A wrapper for simple PyDeck scatter charts."""

import copy
import hashlib
import json
from typing import (
TYPE_CHECKING,
Expand Down Expand Up @@ -238,12 +239,11 @@ def map(
# for information on how to get one and set it up in Streamlit.
#
map_style = None

map_proto = DeckGlJsonChartProto()
map_proto.json = to_deckgl_json(
deck_gl_json = to_deckgl_json(
data, latitude, longitude, size, color, map_style, zoom
)
map_proto.use_container_width = use_container_width
marshall(map_proto, deck_gl_json, use_container_width)
return self.dg._enqueue("deck_gl_json_chart", map_proto)

@property
Expand Down Expand Up @@ -483,3 +483,17 @@ def _get_zoom_level(distance: float) -> int:

# For small number of points the default zoom level will be used.
return _DEFAULT_ZOOM_LEVEL


def marshall(
pydeck_proto: DeckGlJsonChartProto,
pydeck_json: str,
use_container_width: bool,
) -> None:
json_bytes = pydeck_json.encode("utf-8")
id = hashlib.md5(json_bytes).hexdigest()

pydeck_proto.json = pydeck_json
pydeck_proto.use_container_width = use_container_width

pydeck_proto.id = id
11 changes: 11 additions & 0 deletions lib/tests/streamlit/elements/map_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,3 +382,14 @@ def test_pyspark_dataframe(self):

"""Check if map data has 5 rows"""
self.assertEqual(len(c["layers"][0]["data"]), 5)

def test_id_changes_when_data_changes(self):
st.map()

orig_id = self.get_delta_from_queue().new_element.deck_gl_json_chart.id
np.random.seed(0)

df = pd.DataFrame({"lat": [1, 2, 3, 4], "lon": [10, 20, 30, 40]})
st.map(df)
new_id = self.get_delta_from_queue().new_element.deck_gl_json_chart.id
self.assertNotEquals(orig_id, new_id)

0 comments on commit bffee99

Please sign in to comment.