jordancaraballo commited on
Commit
7bed927
β€’
1 Parent(s): 73a4e01

Migration from solara to streamlit

Browse files
.gitignore CHANGED
@@ -2,6 +2,7 @@
2
  __pycache__/
3
  *.py[cod]
4
  *$py.class
 
5
 
6
  # C extensions
7
  *.so
 
2
  __pycache__/
3
  *.py[cod]
4
  *$py.class
5
+ .DS_Store
6
 
7
  # C extensions
8
  *.so
Dockerfile CHANGED
@@ -18,4 +18,7 @@ USER ${NB_USER}
18
 
19
  EXPOSE 7860
20
 
21
- CMD ["solara", "run", "./pages", "--host=0.0.0.0", "--port=7860"]
 
 
 
 
18
 
19
  EXPOSE 7860
20
 
21
+ CMD ["streamlit", "run", "./pages/Home.py", "--server.port=7860"]
22
+
23
+
24
+ streamlit run pages/Home.py
pages/00_home.py DELETED
@@ -1,19 +0,0 @@
1
- import solara
2
-
3
-
4
- @solara.component
5
- def Page():
6
-
7
- markdown = """
8
- ## Alpha Version - Alaskan Tundra 10-day Lightning Forecast
9
-
10
- Cloud to ground lightning 10-day lightning forecast for the Alaskan tundra.
11
- This is still work in progress and under development.
12
-
13
- ## Demo
14
-
15
- ![](https://drive.google.com/uc?export=view&id=1V9lpVfs13l4PYSlFmlZVCmeLlZXyw-zu)
16
-
17
- """
18
-
19
- solara.Markdown(markdown)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
pages/02_matplotlib.py DELETED
@@ -1,55 +0,0 @@
1
- import reacton
2
- import solara
3
- import numpy as np
4
- import matplotlib.pyplot as plt
5
- from matplotlib.figure import Figure
6
- import matplotlib.animation as animation
7
-
8
-
9
- zoom = solara.reactive(2)
10
- center = solara.reactive((20, 0))
11
-
12
-
13
- class PauseAnimation:
14
- def __init__(self):
15
-
16
- self.fig = Figure()
17
- ax = self.fig.subplots()
18
- ax.set_title('Click to pause/resume the animation')
19
- x = np.linspace(-0.1, 0.1, 1000)
20
-
21
- # Start with a normal distribution
22
- self.n0 = (1.0 / ((4 * np.pi * 2e-4 * 0.1) ** 0.5)
23
- * np.exp(-x ** 2 / (4 * 2e-4 * 0.1)))
24
- self.p, = ax.plot(x, self.n0)
25
-
26
- self.animation = animation.FuncAnimation(
27
- self.fig, self.update, frames=200, interval=50, blit=True)
28
- self.paused = False
29
-
30
- self.fig.canvas.mpl_connect('button_press_event', self.toggle_pause)
31
-
32
- def toggle_pause(self, *args, **kwargs):
33
- if self.paused:
34
- self.animation.resume()
35
- else:
36
- self.animation.pause()
37
- self.paused = not self.paused
38
-
39
- def update(self, i):
40
- self.n0 += i / 100 % 5
41
- self.p.set_ydata(self.n0 % 20)
42
- return (self.p,)
43
-
44
- def get_fig(self):
45
- return self.fig
46
-
47
-
48
- @solara.component
49
- def Page():
50
- # do this instead of plt.figure()
51
- #fig = Figure()
52
- #ax = fig.subplots()
53
- #ax.plot([1, 2, 3], [1, 4, 9])
54
- pa = PauseAnimation()
55
- return solara.FigureMatplotlib(pa)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
pages/Home.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+
3
+ st.set_page_config(
4
+ page_title="Home",
5
+ page_icon="πŸ‘‹",
6
+ )
7
+
8
+ st.write("# Alaskan Tundra Fire Occurrence")
9
+
10
+ st.sidebar.success("Select a page above.")
11
+
12
+ st.markdown(
13
+ """
14
+ This space was created to test several operational products
15
+ developed for Alaskan tundra fire occurrence modeling efforts.
16
+ Select a page from the sidebar to test some of the example
17
+ workflows developed as part of this research.
18
+
19
+ ## Objectives
20
+
21
+ TBD
22
+
23
+ ## Want to learn more?
24
+ - Feel free to contact us for additional details, [email protected]
25
+ """
26
+ )
pages/pages/1_⚑_Alaskan_Tundra_Lightning.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import re
3
+ import datasets
4
+ import numpy as np
5
+ import matplotlib.pyplot as plt
6
+ import matplotlib.animation as animation
7
+ import streamlit as st
8
+ import streamlit.components.v1 as components
9
+ from matplotlib.animation import FuncAnimation
10
+ import cartopy.crs as ccrs
11
+ import rioxarray as rxr
12
+ import xarray as xr
13
+ import pandas as pd
14
+ from glob import glob
15
+ from pathlib import Path
16
+ from datetime import datetime
17
+ from huggingface_hub import snapshot_download
18
+
19
+ # define dataset url
20
+ DATASET_URL = 'jordancaraballo/alaska-wildfire-occurrence'
21
+ EPSG = 3338
22
+
23
+ # Grab the dataset from Hugging Face
24
+ cgl_dataset_dir = snapshot_download(repo_id=DATASET_URL, allow_patterns="*.tif", repo_type='dataset')
25
+ cgl_filename = glob(os.path.join(cgl_dataset_dir, 'alaskan-tundra-lightning-forecast_latest.tif'))[0]
26
+
27
+ print(cgl_dataset_dir)
28
+
29
+ # Open the filename from the forecast
30
+ cgl_ds = rxr.open_rasterio(cgl_filename)
31
+
32
+ # Rename bands with timestamps
33
+ cgl_ds['band'] = cgl_ds.attrs['DATES'].replace("'", "").strip('][').split(', ')
34
+
35
+ # Generate visualization
36
+ fig = plt.figure()
37
+ ax = plt.axes(projection=ccrs.epsg(EPSG))
38
+ ax.coastlines()
39
+
40
+ # Generate merge with cartipy
41
+ mesh = cgl_ds.isel(band=0).plot.pcolormesh(
42
+ ax=ax, transform=ccrs.epsg(EPSG), animated=True,cmap='seismic', vmin=0, vmax=1)
43
+
44
+ # Define update function
45
+ def update_mesh(t):
46
+ ax.set_title("time = %s"%t)
47
+ mesh.set_array(cgl_ds.sel(band=t).values.ravel())
48
+ return mesh,
49
+
50
+ # Generate animation function
51
+ animation = FuncAnimation(
52
+ fig, update_mesh, frames=cgl_ds.band.values, interval=600)
53
+ animation_js = animation.to_jshtml()
54
+
55
+ # Adding autoplay
56
+ click_on_play = """document.querySelector('.anim-buttons button[title="Play"]').click();"""
57
+
58
+ ## Search for the creation of the animation within the jshtml file created by matplotlib
59
+ pattern = re.compile(r"(setTimeout.*?;)(.*?})", re.MULTILINE | re.DOTALL)
60
+
61
+ ## Insert the JS line right below that
62
+ animation_js = pattern.sub(rf"\1 \n {click_on_play} \2", animation_js)
63
+
64
+ # Plot text and animation on streamlit page
65
+ st.title("Alpha Version - Alaskan Tundra 10-day Lightning Forecast")
66
+ st.markdown(
67
+ "Cloud to ground lightning 10-day lightning forecast for the Alaskan tundra. " +
68
+ "This is still work in progress and under development.")
69
+ components.html(animation_js, height=1000)
pages/{01_ipyleaflet.py β†’ pages/2_🌍_Alaska_WRF.py} RENAMED
File without changes
requirements.txt CHANGED
@@ -2,4 +2,8 @@ geemap>=0.21.0
2
  leafmap
3
  mapwidget
4
  solara
5
- geopandas
 
 
 
 
 
2
  leafmap
3
  mapwidget
4
  solara
5
+ geopandas
6
+ datasets
7
+ rioxarray
8
+ streamlit
9
+ cartopy