Created
April 3, 2025 17:31
-
-
Save hansthen/8f6c77a6dabfc596364d4112080f5686 to your computer and use it in GitHub Desktop.
argparse form v2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mport argparse | |
from streamlit_argparse import Form | |
from streamlit_folium import st_folium | |
import folium | |
from folium.plugins import Draw | |
import streamlit as st | |
import shlex | |
import jq | |
import json | |
st.set_page_config(layout="wide") | |
parser = argparse.ArgumentParser( | |
description='test simple parser', | |
) | |
parser.add_argument( | |
'--area', | |
nargs='*', | |
required=True, | |
type=json.loads, | |
help='selected area' | |
) | |
parser.add_argument( | |
'--spots', | |
nargs='*', | |
required=False, | |
type=json.loads, | |
help='selected spots' | |
) | |
parser.add_argument( | |
'--description', | |
required=False, | |
help='description' | |
) | |
st.write(parser.format_usage()) | |
def area_widget(form, action, value): | |
area = st.session_state.intermediate["area"] \ | |
if "intermediate" in st.session_state \ | |
else st.session_state.area | |
st.text_area("area", area, disabled=True) | |
return [json.dumps(f) for f in area] | |
def spots_widget(form, action, value): | |
spots = st.session_state.intermediate["points"] \ | |
if "intermediate" in st.session_state \ | |
else st.session_state.spots | |
st.text_area("spots", spots, disabled=True) | |
if spots is None: | |
return None | |
else: | |
return [json.dumps(f) for f in spots] | |
args = ["--area", """ | |
{"type": "Feature", "properties": {}, "geometry": {"type":"Polygon", | |
"coordinates": [[[-72.70752, 47.144836], [-72.70752, 48.008215],[-72.136231, | |
48.008215], [-72.136231, 47.144836], [-72.70752,47.144836]]]}}""", | |
"--spots", """{"type": "Feature", "properties": {}, "geometry": {"type": "Point", | |
"coordinates": [-80.06836, 39.740987]}}""" | |
] | |
try: | |
parser.exit = lambda *args, **kwargs: None | |
initial = parser.parse_args(args) | |
except Exception: | |
st.write("invalid results") | |
left, right = st.columns(2) | |
with right: | |
if initial.area and "area" not in st.session_state: | |
st.session_state.area = initial.area | |
if initial.spots and "spots" not in st.session_state: | |
st.session_state.spots = initial.spots | |
spots = st.session_state.get("spots", []) | |
area = st.session_state.get("area", []) | |
m = folium.Map(location=[39.949610, -75.150282], zoom_start=5) | |
fg = folium.FeatureGroup().add_to(m) | |
if spots: | |
for spot in spots: | |
folium.CircleMarker(spot["geometry"]["coordinates"][::-1]).add_to(fg) | |
for a in area: | |
if a["geometry"]["type"] == "Point": | |
radius = a["properties"]["radius"] | |
folium.Circle( | |
a["geometry"]["coordinates"][::-1], | |
radius=radius, | |
color="red" | |
).add_to(fg) | |
elif a["geometry"]["type"] == "Polygon": | |
area_coordinates = a["geometry"]["coordinates"] | |
rev = [c[::-1] for ac in area_coordinates for c in ac] | |
folium.Polygon( | |
rev, | |
color="red" | |
).add_to(fg) | |
Draw( | |
export=False, | |
feature_group=fg, | |
draw_options={ | |
"polyline": False, | |
"marker": False, | |
"polygon": { | |
"shapeOptions": { | |
"color": "red" | |
} | |
}, | |
"circlemarker": { | |
"shapeOptions": { | |
"color": "red" | |
} | |
}, | |
"circle": { | |
"shapeOptions": { | |
"color": "red" | |
} | |
}, | |
"rectangle": { | |
"shapeOptions": { | |
"color": "red" | |
} | |
} | |
} | |
} | |
).add_to(m) | |
# split between different types of selected items | |
# this is if we use the callback handlers of the draw plugin | |
parsed = jq.compile(""" | |
# for each item in the array, note we do not unpack the array due to |= | |
.[] |= | |
# add a flag | |
. + {point: (.geometry.type == "Point" and (.properties | has("radius") == false))} | |
# split all items based on the flag (and remove the flag itself) | |
| { | |
points: [.[] | select(.point) | del(.point)], | |
area: [.[] | select(.point == false) | del(.point)] | |
} | |
""") | |
def store_in_session(): | |
data = st.session_state.folium | |
if data["all_drawings"] is not None: | |
features = data["all_drawings"] | |
st.session_state.intermediate = parsed.input_value(features).first() | |
data = st_folium( | |
m, | |
returned_objects=["all_drawings"], | |
on_change=store_in_session, | |
key="folium" | |
) | |
st.write(data) | |
with left: | |
form = Form( | |
parser, | |
area=area_widget, | |
spots=spots_widget | |
) | |
output = form.render(args) | |
41,9 Top | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment