Jonathan Marokhovsky commited on
Commit
8775fbe
1 Parent(s): 516561e

Sending all changes and seeing what sticks

Browse files
Files changed (1) hide show
  1. app.py +37 -18
app.py CHANGED
@@ -1,21 +1,32 @@
 
1
  import leafmap.foliumap as leafmap
2
  import rioxarray
3
  import geopandas as gpd
4
  import streamlit as st
5
-
6
- # import altair as alt
7
- # import ibis
8
- # from ibis import _
9
- # import ibis.selectors as s
10
  from streamlit_folium import st_folium
11
  import json
12
  from leafmap import pmtiles_style
13
 
 
14
  # Common Functions
 
 
 
 
 
 
 
 
15
 
16
 
17
  # Actual Page content below
18
- st.set_page_config(layout="wide", page_title="Huggingface Toy", page_icon="❦")
 
 
19
  st.title("Huggingface Streamlit Toy")
20
 
21
  """
@@ -70,9 +81,10 @@ m.add_pmtiles(
70
  # TODO: Dynamically check the center of the map and change what's displayed depending on that.
71
 
72
  polygon_ex = '{"type":"Feature","properties":{},"geometry":{"type":"Polygon","coordinates":[[[-6.108398,40.780541],[-6.108398,54.342149],[15.908203,54.342149],[15.908203,40.780541],[-6.108398,40.780541]]]}}'
73
- code_ex = """
74
- m.add_gdf(geo, layer_name="selection")
75
- """
 
76
 
77
 
78
  ## Map controls sidebar
@@ -96,28 +108,35 @@ with st.sidebar:
96
  # if bounds is not None:
97
  # bounds.set_crs("epsg:4326")
98
 
99
- geo = gpd.read_file(polygon, driver="GeoJSON")
100
- geo.set_crs("epsg:4326")
101
- x = (
102
- rioxarray.open_rasterio("/vsicurl/" + deforest, masked=True)
103
- .rio.clip(geo.geometry.values, geo.crs, from_disk=True)
104
- .mean()
105
- )
106
- value = x
107
 
108
 
109
  "### Average of tons of carbon lost:"
110
- st.write(f"{value:,}")
111
  st.divider()
112
 
113
  # run whatever python code is in the python box, just for fun
114
  eval(compile(code, "<string>", "exec"))
 
115
  st_data = m.to_streamlit(height=700, bidirectional=True)
116
  polygon = st_data["last_active_drawing"]
 
 
 
117
  if polygon is not None:
118
  "### The Polygon is: "
 
119
  st.write(f"{polygon}")
120
 
 
 
121
  "## Explore Further"
122
  st.write("""
123
  To modify the map, add code to the `code` section in the sidebar to the left. Some examples of code to add are below.
 
1
+ from boettiger_app import read_polygon
2
  import leafmap.foliumap as leafmap
3
  import rioxarray
4
  import geopandas as gpd
5
  import streamlit as st
6
+ import altair as alt
7
+ import ibis
8
+ from ibis import _
9
+ import ibis.selectors as s
 
10
  from streamlit_folium import st_folium
11
  import json
12
  from leafmap import pmtiles_style
13
 
14
+
15
  # Common Functions
16
+ def read_polygon(polygon):
17
+ """
18
+ Take in a polygon from the app and convert it to GeoJSON
19
+ """
20
+ geojson_str = json.dumps(polygon)
21
+ gdf = gpd.read_file(geojson_str, driver="GeoJSON")
22
+ gdf.set_crs("epsg:4326")
23
+ return gdf
24
 
25
 
26
  # Actual Page content below
27
+ started = None
28
+ if not started:
29
+ st.set_page_config(layout="wide", page_title="Huggingface Toy", page_icon="❦")
30
  st.title("Huggingface Streamlit Toy")
31
 
32
  """
 
81
  # TODO: Dynamically check the center of the map and change what's displayed depending on that.
82
 
83
  polygon_ex = '{"type":"Feature","properties":{},"geometry":{"type":"Polygon","coordinates":[[[-6.108398,40.780541],[-6.108398,54.342149],[15.908203,54.342149],[15.908203,40.780541],[-6.108398,40.780541]]]}}'
84
+ # code_ex = """
85
+ # m.add_gdf(polygon, layer_name="selection")
86
+ # """
87
+ code_ex = ""
88
 
89
 
90
  ## Map controls sidebar
 
108
  # if bounds is not None:
109
  # bounds.set_crs("epsg:4326")
110
 
111
+ # geo = gpd.read_file(polygon, driver="GeoJSON")
112
+ # geo.set_crs("epsg:4326")
113
+ # x = (
114
+ # rioxarray.open_rasterio("/vsicurl/" + deforest, masked=True)
115
+ # .rio.clip(geo.geometry.values, geo.crs, from_disk=True)
116
+ # .mean()
117
+ # )
118
+ # value = x
119
 
120
 
121
  "### Average of tons of carbon lost:"
122
+ # st.write(f"{value:,}")
123
  st.divider()
124
 
125
  # run whatever python code is in the python box, just for fun
126
  eval(compile(code, "<string>", "exec"))
127
+
128
  st_data = m.to_streamlit(height=700, bidirectional=True)
129
  polygon = st_data["last_active_drawing"]
130
+ "### Could there be a polygon here?"
131
+ st.write(f"{polygon is not None}")
132
+
133
  if polygon is not None:
134
  "### The Polygon is: "
135
+ gdf = read_polygon(polygon)
136
  st.write(f"{polygon}")
137
 
138
+ started = True
139
+
140
  "## Explore Further"
141
  st.write("""
142
  To modify the map, add code to the `code` section in the sidebar to the left. Some examples of code to add are below.