Correctly using st_map and pandas dataframe

Summary

Hello streamlit community, I would like some guidance on how to properly use the st.map widget. I am trying to show coordinates stored in a simple dictionary. The widget does not correctly show the location of the coordinates.

Code snippet:

some_location = {"lon":-6.175226341354826,"lat":106.82701971424365}
st.map(some_location)

Expected behavior:

As per the parameter description of the st.map function, the function is compatible with the ‘dict’ structure.

Error message:

2022-11-07 09:56:44.293 Uncaught app exception
Traceback (most recent call last):
  File "C:\Users\MKurniawan\.conda\envs\MSDGI\lib\site-packages\streamlit\scriptrunner\script_runner.py", line 557, in _run_script
    exec(code, module.__dict__)
  File "Home.py", line 211, in <module>
    mid_row()
  File "Home.py", line 153, in mid_row
    st.map(map_df)
  File "C:\Users\MKurniawan\.conda\envs\MSDGI\lib\site-packages\streamlit\elements\map.py", line 74, in map
    map_proto.json = to_deckgl_json(data, zoom)
  File "C:\Users\MKurniawan\.conda\envs\MSDGI\lib\site-packages\streamlit\elements\map.py", line 144, in to_deckgl_json
    if data is None or data.empty:
AttributeError: 'dict' object has no attribute 'empty'

My work-around

In order to mitigate this, I converted the dictionary to the pandas data frame. This works, but does not display the points at the correct coordinates.

# Default locations
    loc_data = {
        "longitude": [
            52.29274500131053,
            52.48445527569644,
            50.42902450689658,
            24.977514892279544,
            50.974819434969845,
            3.0933771882453205,
        ],
        "latitude": [
            6.90964804526128,
            7.332599016521705,
            30.51765681207883,
            55.08787764229468,
            -113.99429073143872,
            101.56100788451147,
        ],
    }
    map_df = pd.DataFrame.from_dict(loc_data)

    # Create a map
    st.map(map_df)

I’m simply trying to show some plots correctly on the map.
Any guidance is appreciated.

Thanks in advance!
Arief.

Hi @Arief-AK,

Welcome to the Streamlit community! :wave: :partying_face:

st.map does accept a dict as input. However, the dictionary needs to conform to the following format:

{
    "lat": {
        "idx": val,
        "idx+1": val,
        "idx+2": val,
        ...,
        ...,
    },
    "lon": {
        "idx": val,
        "idx+1": val,
        "idx+2": val,
        ...,
        ...,
    },
}

where idx ranges from 0 to the number of coordinates - 1. The first key of the dict should be "lat", and its value is a dictionary containing index:latitude pairs, and the second key should be "lon", and its value is a dictionary containing index:longitude pairs.

Here’s an example demonstrating the format the dictionary should conform to:

import streamlit as st

some_location = {
    "lat": {
        "0": 37.75399198995102,
    },
    "lon": {
        "0": -122.37992172574494,
    },
}

st.map(some_location)

some_location = {
    "lat": {
        "0": 37.75399198995102,
        "1": 37.76870674681875,
        "2": 37.74880050179763,
        "3": 37.77024493777782,
        "4": 37.733002101413284,
    },
    "lon": {
        "0": -122.37992172574494,
        "1": -122.401237588094,
        "2": -122.37760526265474,
        "3": -122.41019671643923,
        "4": -122.37945589954018,
    },
}

st.map(some_location)

Happy Streamlit-ing!
Snehan :balloon:

Hi @snehankekre, thanks for your response. I have tried your solution, I am still experiencing the same error.

code

import streamlit as st

some_location = {
        "lat": {
            "0": 37.75399198995102,
        },
        "lon": {
            "0": -122.37992172574494,
        },
    }

    st.map(some_location)

error message

2022-11-07 11:20:31.468 Uncaught app exception
Traceback (most recent call last):
  File "C:\Users\MKurniawan\.conda\envs\MSDGI\lib\site-packages\streamlit\scriptrunner\script_runner.py", line 557, in _run_script
    exec(code, module.__dict__)
  File "C:\Users\MKurniawan\source\ROSEN-MSDGI-UI\pages\About.py", line 32, in <module>
    initialise()
  File "C:\Users\MKurniawan\source\ROSEN-MSDGI-UI\pages\About.py", line 28, in initialise
    st.map(some_location)
  File "C:\Users\MKurniawan\.conda\envs\MSDGI\lib\site-packages\streamlit\elements\map.py", line 74, in map
    map_proto.json = to_deckgl_json(data, zoom)
  File "C:\Users\MKurniawan\.conda\envs\MSDGI\lib\site-packages\streamlit\elements\map.py", line 144, in to_deckgl_json
    if data is None or data.empty:
AttributeError: 'dict' object has no attribute 'empty'

This is a bug in Streamlit, but it is already solved in the develop branch and will be released with the upcoming version.

3 Likes

Sorry about that :sweat_smile: As @lukasmasuch pointed out, it looks like there was a bug in the st.map code that was inadvertently fixed recently in #5590.

Until the fix makes it into the next Streamlit release, it is available in the latest streamlit-nightly. To use it, you’d have to uninstall streamlit and install streamlit-nightly. I didn’t realize earlier that I was using the nightly release to run the above snippets.

2 Likes

Thanks @lukasmasuch @snehankekre for the responses, using the streamlit-nightly release fixes the issue.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.