Hi, I want to create a streamlit app which takes input from user for a particular cargo and for that shows two separate paths from port of origin to destination port.
One path will be on the previous route(in red ) other path will be on the updated route (in green colour) I was hoping to do it on a globe/map using folium by the below code.
I’m a total beginner at Snowflake/Streamlit this so I really would appreciate anyone’s help.
I have figured out how to fetch the data and display it, but the visual map part is still not working for me. For the below code I get error -
Your app is having trouble loading the streamlit_folium.st_folium component.
If this is an installed component that works locally, the app may be having trouble
accessing the component frontend assets due to network latency or proxy settings
in your app deployment.
For ease and poc’s sake I have hardcoded the co-ordinates manually but I know how to fetch it dynamically when needed.
route_coordinates = {
"Route_3 (ports with cold storage)": [34.0522, -118.2437],
"Route_7_Alternate (ports with cold storage)": [36.7783, -119.4179],
}
port_coordinates = {
"Port of Shanghai": [31.2304, 121.4737],
"Los Angeles Port": [33.7291, -118.2637],
}
origin_coords = port_coordinates[row["PORT_OF_ORIGIN"]]
destination_coords = port_coordinates[row["DESTINATION_PORT"]]
prev_coords = route_coordinates[row["PREVIOUS_ROUTE"]]
updated_coords = route_coordinates[row["UPDATED_ROUTE"]]
st.write(f"Previous Coords: {prev_coords}")
st.write(f"Updated Coords: {updated_coords}")
# Initialize Folium map centered at the midpoint
midpoint = [
(origin_coords[0] + destination_coords[0]) / 2,
(origin_coords[1] + destination_coords[1]) / 2,
]
folium_map = folium.Map(location=midpoint, zoom_start=4)
# Add markers for ports
folium.Marker(
location=origin_coords,
popup="Port of Origin: Port of Shanghai",
icon=folium.Icon(color="orange"),
).add_to(folium_map)
folium.Marker(
location=destination_coords,
popup="Destination Port: Los Angeles Port",
icon=folium.Icon(color="blue"),
).add_to(folium_map)
# Add previous route (in red)
folium.PolyLine(
locations=[origin_coords, prev_coords, destination_coords],
color="red",
weight=3,
tooltip="Previous Route",
).add_to(folium_map)
# Add updated route (in green)
folium.PolyLine(
locations=[origin_coords, updated_coords, destination_coords],
color="green",
weight=3,
tooltip="Updated Route",
).add_to(folium_map)
# Display the map
st.write("Map Visualization:")
st_folium(folium_map, width=800, height=500)