I developed a dash WebApp locally on my computer and it works beautifuly. I got a map (dash-leaflet) in the App which displays different icons. These icons are sourced from url using the assign function from dash_extensions.
Thanks. In DSS 9 it can be a bit challenging to get assets to work, but it can be done. Here is a small example that should work out of the box,
# Setup per-app assets folder.
import os
from dash_extensions.dataiku import bind_assets_folder
app_id = "my_app"
os.environ["ASSETS_FOLDER"] = f"assets/{app_id}"
assets_folder = os.path.join(os.getcwd(), "assets")
os.makedirs(assets_folder, exist_ok=True)
# region Normal Dash app
import dash_html_components as html
import dash_leaflet as dl
import dash_leaflet.express as dlx
from dash_extensions.enrich import DashProxy
from dash_extensions.javascript import assign
# A few countries.
countries = [dict(name="Denmark", iso2="dk", lat=56.26392, lon=9.501785),
dict(name="Sweden", iso2="se", lat=59.334591, lon=18.063240),
dict(name="Norway", iso2="no", lat=59.911491, lon=9.501785)]
# Generate geojson with a marker for each country and name as tooltip.
geojson = dlx.dicts_to_geojson([{**c, **dict(tooltip=c['name'])} for c in countries])
# Create javascript function that draws a marker with a custom icon, in this case a flag hosted by flagcdn.
draw_flag = assign("""function(feature, latlng){
const flag = L.icon({iconUrl: `https://flagcdn.com/64x48/${feature.properties.iso2}.png`, iconSize: [64, 48]});
return L.marker(latlng, {icon: flag});
}""")
# Create example app.
my_app = DashProxy()
my_app.layout = html.Div([
dl.Map(children=[
dl.TileLayer(), dl.GeoJSON(data=geojson, options=dict(pointToLayer=draw_flag), zoomToBounds=True)
], style={'width': '100%', 'height': '50vh', 'margin': "auto", "display": "block"}, id="map"),
])
# endregion
# Inject state into the app object from dataiku.
bind_assets_folder(app, app_id , assets_folder)
my_app.hijack(app)
Thanks Emil!
Yes, adding the parameter assets_folder='my_path_to_assets' solved the issue.
Unfortunately, a new one appeared: Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'map')
It seems to me, to be related to Dash_leaflet library and the asynchronous feature of React:
For some reason this error only appears when I embed Dash in a Flask app.
My mistake was that I didn’t indicate the format of the geoJson object at: geojson = dl.GeoJSON(data=geobuf,id="geojson", format="geobuf", options=dict(pointToLayer=point_to_layer))
Hey Emil, what is app_id referring to here? I am using the assets folder and created it in the Code Library; however, I could not find a way to reference it in the Dash Webapp
Thanks for the response @Emil. Do you know where can I get that string? Also, where did you define the assets folder? Is it on the Code Library section?