Skip to content

Instantly share code, notes, and snippets.

@hansthen
Created June 18, 2025 21:09
Show Gist options
  • Save hansthen/ef932f8a729e3c6e053af5cd1414e146 to your computer and use it in GitHub Desktop.
Save hansthen/ef932f8a729e3c6e053af5cd1414e146 to your computer and use it in GitHub Desktop.
example streamlit-argparse
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
def initialize(key, value):
if key not in st.session_state:
st.session_state["key"] = value
return st.session_state[key]
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"
}
},
42,0-1 Top
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment