Streamlit returns error if get_elevation added to ColumnLayer

283 Views Asked by At

I am new to streamlit, and I am trying to display a pydeck layer with columns. I want the column height to be the rain (mm) column I am providing in a dataframe, but whenever I use get_evelation="rain (mm)", I get the following error:

Error: Function calls not allowed in JSON expressions
at http://localhost:8501/static/js/7.f6560317.chunk.js:2:512302
at u (http://localhost:8501/static/js/7.f6560317.chunk.js:2:512666)
at http://localhost:8501/static/js/7.f6560317.chunk.js:2:512596
at Array.forEach (<anonymous>)
at u (http://localhost:8501/static/js/7.f6560317.chunk.js:2:512568)
at u (http://localhost:8501/static/js/7.f6560317.chunk.js:2:512673)
at t.default (http://localhost:8501/static/js/7.f6560317.chunk.js:2:512250)
at e.h [as convertFunction] (http://localhost:8501/static/js/7.f6560317.chunk.js:2:1049691)
at http://localhost:8501/static/js/7.f6560317.chunk.js:2:2778265
at y (http://localhost:8501/static/js/7.f6560317.chunk.js:2:2778495)

The function I am using to render my map is the following:

def make_map2(data, lat, lon, zoom):
    
    column_layer = pdk.Layer(
                             "ColumnLayer",
                             data=data,
                             get_position=["lon", "lat"],
                             get_elevation="rain (mm)",
                             elevation_scale=20,
                             radius=2000,
                             get_fill_color=[180, 0, 200, 140],
                             pickable=True,
                             auto_highlight=True,
                             extruded=True
    )

    tooltip={'html': 'Location: {location}</br> Date: {date} </br> Rainfall (mm): {rain_mm}</br> Type: {type}'}

    r = pdk.Deck(column_layer,
                 initial_view_state={
                                    "latitude": lat,
                                    "longitude": lon,
                                    "zoom": zoom,
                                    "pitch": 60
                                },
                 tooltip=tooltip,
                 map_provider="mapbox",
                 map_style='mapbox://styles/mapbox/light-v9',
                )

    map2 = st.write(r)

    return map2

The input data have this structure:

        date        location    lat     lon     rain (mm)   type
0       2021-09-15  lowestoft   52.483  1.727   54.115513   predicted
1       2021-09-15  heathrow    51.479  -0.449  30.008739   predicted
2       2021-09-15  eastbourne  50.762  0.285   90.584396   predicted
3       2021-09-15  cambridge   52.245  0.102   51.445862   predicted
4       2021-09-15  manston     51.346  1.337   81.089737   predicted
5       2021-09-15  oxford      51.761  -1.262  39.420902   predicted
1

There are 1 best solutions below

1
On BEST ANSWER

This could be as simple as a misspelled argument:

get_evelation="rain (mm)" should be get_elevation="rain (mm)"


Edit: Irrespective of the misspelled argument name, this error occurs in how pydeck parses string literals. pydeck assumes that rain (mm) is a function call, and throws an error. Naming the get_elevation column without the spaces or parenthesis allows the example to work:

import pandas as pd
import numpy as np
import streamlit as st
import pydeck as pdk

df = pd.read_csv("~/Desktop/data.csv")
#to prove hypothesis, make column name one continuous string with no extra characters
df["rain_noparens"] = df["rain (mm)"] 

view = pdk.data_utils.compute_view(df[["lon", "lat"]])
view.pitch = 75
view.bearing = 60

column_layer = pdk.Layer(
    "ColumnLayer",
    data=df,
    get_position=["lon", "lat"],
    get_elevation="rain_noparens",
    elevation_scale=20,
    radius=2000,
    get_fill_color=[180, 0, 200, 140],
    pickable=True,
    auto_highlight=True,
)

tooltip = {
    "html": "Location: {location}</br> Date: {date} </br> Rainfall (mm): {rain (mm)}</br> Type: {type}"
}

r = pdk.Deck(
    column_layer,
    initial_view_state=view,
    tooltip=tooltip,
    map_provider="mapbox",
    map_style="mapbox://styles/mapbox/light-v9",
)

st.pydeck_chart(r)

See also: https://github.com/streamlit/streamlit/issues/3992