Spaces:
Sleeping
Sleeping
dn commited on
Commit ·
2e518b2
1
Parent(s): 4bf9490
fix lint/format
Browse files- .github/scripts/build.py +27 -14
- apps/charts.py +6 -8
- apps/reactive_plots.py +2 -0
- build.py +0 -1
- notebooks/penguins.py +6 -3
- notebooks/sb79_map.py +19 -10
- pyproject.toml +10 -4
- tests/test_sample.py +2 -4
.github/scripts/build.py
CHANGED
|
@@ -21,14 +21,13 @@ The exported files will be placed in the specified output directory (default: _s
|
|
| 21 |
# ///
|
| 22 |
|
| 23 |
import subprocess
|
| 24 |
-
from typing import List, Union
|
| 25 |
from pathlib import Path
|
| 26 |
|
| 27 |
-
import jinja2
|
| 28 |
import fire
|
| 29 |
-
|
| 30 |
from loguru import logger
|
| 31 |
|
|
|
|
| 32 |
def _export_html_wasm(notebook_path: Path, output_dir: Path, as_app: bool = False) -> bool:
|
| 33 |
"""Export a single marimo notebook to HTML/WebAssembly format.
|
| 34 |
|
|
@@ -49,7 +48,7 @@ def _export_html_wasm(notebook_path: Path, output_dir: Path, as_app: bool = Fals
|
|
| 49 |
output_path: Path = notebook_path.with_suffix(".html")
|
| 50 |
|
| 51 |
# Base command for marimo export
|
| 52 |
-
cmd:
|
| 53 |
|
| 54 |
# Configure export mode based on whether it's an app or a notebook
|
| 55 |
if as_app:
|
|
@@ -83,7 +82,12 @@ def _export_html_wasm(notebook_path: Path, output_dir: Path, as_app: bool = Fals
|
|
| 83 |
return False
|
| 84 |
|
| 85 |
|
| 86 |
-
def _generate_index(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
"""Generate an index.html file that lists all the notebooks.
|
| 88 |
|
| 89 |
This function creates an HTML index page that displays links to all the exported
|
|
@@ -94,7 +98,8 @@ def _generate_index(output_dir: Path, template_file: Path, notebooks_data: List[
|
|
| 94 |
notebooks_data (List[dict]): List of dictionaries with data for notebooks
|
| 95 |
apps_data (List[dict]): List of dictionaries with data for apps
|
| 96 |
output_dir (Path): Directory where the index.html file will be saved
|
| 97 |
-
template_file (Path, optional): Path to the template file. If None,
|
|
|
|
| 98 |
|
| 99 |
Returns:
|
| 100 |
None
|
|
@@ -113,7 +118,7 @@ def _generate_index(output_dir: Path, template_file: Path, notebooks_data: List[
|
|
| 113 |
template_name = template_file.name
|
| 114 |
env = jinja2.Environment(
|
| 115 |
loader=jinja2.FileSystemLoader(template_dir),
|
| 116 |
-
autoescape=jinja2.select_autoescape(["html", "xml"])
|
| 117 |
)
|
| 118 |
template = env.get_template(template_name)
|
| 119 |
|
|
@@ -125,7 +130,7 @@ def _generate_index(output_dir: Path, template_file: Path, notebooks_data: List[
|
|
| 125 |
f.write(rendered_html)
|
| 126 |
logger.info(f"Successfully generated index.html at {index_path}")
|
| 127 |
|
| 128 |
-
except
|
| 129 |
# Handle file I/O errors
|
| 130 |
logger.error(f"Error generating index.html: {e}")
|
| 131 |
except jinja2.exceptions.TemplateError as e:
|
|
@@ -133,7 +138,7 @@ def _generate_index(output_dir: Path, template_file: Path, notebooks_data: List[
|
|
| 133 |
logger.error(f"Error rendering template: {e}")
|
| 134 |
|
| 135 |
|
| 136 |
-
def _export(folder: Path, output_dir: Path, as_app: bool=False) ->
|
| 137 |
"""Export all marimo notebooks in a folder to HTML/WebAssembly format.
|
| 138 |
|
| 139 |
This function finds all Python files in the specified folder and exports them
|
|
@@ -172,12 +177,15 @@ def _export(folder: Path, output_dir: Path, as_app: bool=False) -> List[dict]:
|
|
| 172 |
if _export_html_wasm(nb, output_dir, as_app=as_app)
|
| 173 |
]
|
| 174 |
|
| 175 |
-
logger.info(
|
|
|
|
|
|
|
| 176 |
return notebook_data
|
| 177 |
|
|
|
|
| 178 |
def main(
|
| 179 |
-
output_dir:
|
| 180 |
-
template:
|
| 181 |
) -> None:
|
| 182 |
"""Main function to export marimo notebooks.
|
| 183 |
|
|
@@ -218,10 +226,15 @@ def main(
|
|
| 218 |
return
|
| 219 |
|
| 220 |
# Generate the index.html file that lists all notebooks and apps
|
| 221 |
-
_generate_index(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
|
| 223 |
logger.info(f"Build completed successfully. Output directory: {output_dir}")
|
| 224 |
|
| 225 |
|
| 226 |
-
if __name__ ==
|
| 227 |
fire.Fire(main)
|
|
|
|
| 21 |
# ///
|
| 22 |
|
| 23 |
import subprocess
|
|
|
|
| 24 |
from pathlib import Path
|
| 25 |
|
|
|
|
| 26 |
import fire
|
| 27 |
+
import jinja2
|
| 28 |
from loguru import logger
|
| 29 |
|
| 30 |
+
|
| 31 |
def _export_html_wasm(notebook_path: Path, output_dir: Path, as_app: bool = False) -> bool:
|
| 32 |
"""Export a single marimo notebook to HTML/WebAssembly format.
|
| 33 |
|
|
|
|
| 48 |
output_path: Path = notebook_path.with_suffix(".html")
|
| 49 |
|
| 50 |
# Base command for marimo export
|
| 51 |
+
cmd: list[str] = ["uvx", "marimo", "export", "html-wasm", "--sandbox"]
|
| 52 |
|
| 53 |
# Configure export mode based on whether it's an app or a notebook
|
| 54 |
if as_app:
|
|
|
|
| 82 |
return False
|
| 83 |
|
| 84 |
|
| 85 |
+
def _generate_index(
|
| 86 |
+
output_dir: Path,
|
| 87 |
+
template_file: Path,
|
| 88 |
+
notebooks_data: list[dict] | None = None,
|
| 89 |
+
apps_data: list[dict] | None = None,
|
| 90 |
+
) -> None:
|
| 91 |
"""Generate an index.html file that lists all the notebooks.
|
| 92 |
|
| 93 |
This function creates an HTML index page that displays links to all the exported
|
|
|
|
| 98 |
notebooks_data (List[dict]): List of dictionaries with data for notebooks
|
| 99 |
apps_data (List[dict]): List of dictionaries with data for apps
|
| 100 |
output_dir (Path): Directory where the index.html file will be saved
|
| 101 |
+
template_file (Path, optional): Path to the template file. If None,
|
| 102 |
+
uses the default template.
|
| 103 |
|
| 104 |
Returns:
|
| 105 |
None
|
|
|
|
| 118 |
template_name = template_file.name
|
| 119 |
env = jinja2.Environment(
|
| 120 |
loader=jinja2.FileSystemLoader(template_dir),
|
| 121 |
+
autoescape=jinja2.select_autoescape(["html", "xml"]),
|
| 122 |
)
|
| 123 |
template = env.get_template(template_name)
|
| 124 |
|
|
|
|
| 130 |
f.write(rendered_html)
|
| 131 |
logger.info(f"Successfully generated index.html at {index_path}")
|
| 132 |
|
| 133 |
+
except OSError as e:
|
| 134 |
# Handle file I/O errors
|
| 135 |
logger.error(f"Error generating index.html: {e}")
|
| 136 |
except jinja2.exceptions.TemplateError as e:
|
|
|
|
| 138 |
logger.error(f"Error rendering template: {e}")
|
| 139 |
|
| 140 |
|
| 141 |
+
def _export(folder: Path, output_dir: Path, as_app: bool = False) -> list[dict]:
|
| 142 |
"""Export all marimo notebooks in a folder to HTML/WebAssembly format.
|
| 143 |
|
| 144 |
This function finds all Python files in the specified folder and exports them
|
|
|
|
| 177 |
if _export_html_wasm(nb, output_dir, as_app=as_app)
|
| 178 |
]
|
| 179 |
|
| 180 |
+
logger.info(
|
| 181 |
+
f"Successfully exported {len(notebook_data)} out of {len(notebooks)} files from {folder}"
|
| 182 |
+
)
|
| 183 |
return notebook_data
|
| 184 |
|
| 185 |
+
|
| 186 |
def main(
|
| 187 |
+
output_dir: str | Path = "_site",
|
| 188 |
+
template: str | Path = "templates/tailwind.html.j2",
|
| 189 |
) -> None:
|
| 190 |
"""Main function to export marimo notebooks.
|
| 191 |
|
|
|
|
| 226 |
return
|
| 227 |
|
| 228 |
# Generate the index.html file that lists all notebooks and apps
|
| 229 |
+
_generate_index(
|
| 230 |
+
output_dir=output_dir,
|
| 231 |
+
notebooks_data=notebooks_data,
|
| 232 |
+
apps_data=apps_data,
|
| 233 |
+
template_file=template_file,
|
| 234 |
+
)
|
| 235 |
|
| 236 |
logger.info(f"Build completed successfully. Output directory: {output_dir}")
|
| 237 |
|
| 238 |
|
| 239 |
+
if __name__ == "__main__":
|
| 240 |
fire.Fire(main)
|
apps/charts.py
CHANGED
|
@@ -13,10 +13,10 @@ __generated_with = "0.10.9"
|
|
| 13 |
app = marimo.App(width="medium")
|
| 14 |
|
| 15 |
with app.setup:
|
| 16 |
-
import numpy as np
|
| 17 |
import altair as alt
|
| 18 |
-
import pandas as pd
|
| 19 |
import marimo as mo
|
|
|
|
|
|
|
| 20 |
|
| 21 |
|
| 22 |
@app.cell
|
|
@@ -41,12 +41,10 @@ def _():
|
|
| 41 |
|
| 42 |
# Create interactive chart
|
| 43 |
chart = mo.ui.altair_chart(
|
| 44 |
-
(
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
.properties(height=400, title="Interactive Scatter Plot")
|
| 49 |
-
)
|
| 50 |
)
|
| 51 |
chart
|
| 52 |
return chart
|
|
|
|
| 13 |
app = marimo.App(width="medium")
|
| 14 |
|
| 15 |
with app.setup:
|
|
|
|
| 16 |
import altair as alt
|
|
|
|
| 17 |
import marimo as mo
|
| 18 |
+
import numpy as np
|
| 19 |
+
import pandas as pd
|
| 20 |
|
| 21 |
|
| 22 |
@app.cell
|
|
|
|
| 41 |
|
| 42 |
# Create interactive chart
|
| 43 |
chart = mo.ui.altair_chart(
|
| 44 |
+
alt.Chart(data)
|
| 45 |
+
.mark_circle()
|
| 46 |
+
.encode(x="x", y="y", size=alt.value(100), color=alt.value("steelblue"))
|
| 47 |
+
.properties(height=400, title="Interactive Scatter Plot")
|
|
|
|
|
|
|
| 48 |
)
|
| 49 |
chart
|
| 50 |
return chart
|
apps/reactive_plots.py
CHANGED
|
@@ -76,12 +76,14 @@ def __(alt, data):
|
|
| 76 |
def __():
|
| 77 |
import altair as alt
|
| 78 |
from vega_datasets import data
|
|
|
|
| 79 |
return alt, data
|
| 80 |
|
| 81 |
|
| 82 |
@app.cell
|
| 83 |
def __():
|
| 84 |
import marimo as mo
|
|
|
|
| 85 |
return (mo,)
|
| 86 |
|
| 87 |
|
|
|
|
| 76 |
def __():
|
| 77 |
import altair as alt
|
| 78 |
from vega_datasets import data
|
| 79 |
+
|
| 80 |
return alt, data
|
| 81 |
|
| 82 |
|
| 83 |
@app.cell
|
| 84 |
def __():
|
| 85 |
import marimo as mo
|
| 86 |
+
|
| 87 |
return (mo,)
|
| 88 |
|
| 89 |
|
build.py
DELETED
|
@@ -1 +0,0 @@
|
|
| 1 |
-
.github/scripts/build.py
|
|
|
|
|
|
notebooks/penguins.py
CHANGED
|
@@ -13,13 +13,14 @@ __generated_with = "0.13.5"
|
|
| 13 |
app = marimo.App(width="medium")
|
| 14 |
|
| 15 |
with app.setup:
|
| 16 |
-
import marimo as mo
|
| 17 |
-
import polars as pl
|
| 18 |
import altair as alt
|
|
|
|
| 19 |
import pandas as pd
|
|
|
|
| 20 |
|
| 21 |
file = mo.notebook_location() / "public" / "penguins.csv"
|
| 22 |
|
|
|
|
| 23 |
@app.cell(hide_code=True)
|
| 24 |
def _():
|
| 25 |
mo.md(
|
|
@@ -39,12 +40,14 @@ def _():
|
|
| 39 |
df.head()
|
| 40 |
return (df,)
|
| 41 |
|
|
|
|
| 42 |
@app.cell
|
| 43 |
def _():
|
| 44 |
# Try to avoid reading the file with pandas
|
| 45 |
_df = pd.read_csv(str(file))
|
| 46 |
return
|
| 47 |
|
|
|
|
| 48 |
@app.cell
|
| 49 |
def _(df):
|
| 50 |
# Basic statistics
|
|
@@ -52,7 +55,7 @@ def _(df):
|
|
| 52 |
### Dataset Overview
|
| 53 |
|
| 54 |
- Total records: {df.height}
|
| 55 |
-
- Columns: {
|
| 56 |
|
| 57 |
### Summary Statistics
|
| 58 |
|
|
|
|
| 13 |
app = marimo.App(width="medium")
|
| 14 |
|
| 15 |
with app.setup:
|
|
|
|
|
|
|
| 16 |
import altair as alt
|
| 17 |
+
import marimo as mo
|
| 18 |
import pandas as pd
|
| 19 |
+
import polars as pl
|
| 20 |
|
| 21 |
file = mo.notebook_location() / "public" / "penguins.csv"
|
| 22 |
|
| 23 |
+
|
| 24 |
@app.cell(hide_code=True)
|
| 25 |
def _():
|
| 26 |
mo.md(
|
|
|
|
| 40 |
df.head()
|
| 41 |
return (df,)
|
| 42 |
|
| 43 |
+
|
| 44 |
@app.cell
|
| 45 |
def _():
|
| 46 |
# Try to avoid reading the file with pandas
|
| 47 |
_df = pd.read_csv(str(file))
|
| 48 |
return
|
| 49 |
|
| 50 |
+
|
| 51 |
@app.cell
|
| 52 |
def _(df):
|
| 53 |
# Basic statistics
|
|
|
|
| 55 |
### Dataset Overview
|
| 56 |
|
| 57 |
- Total records: {df.height}
|
| 58 |
+
- Columns: {", ".join(df.columns)}
|
| 59 |
|
| 60 |
### Summary Statistics
|
| 61 |
|
notebooks/sb79_map.py
CHANGED
|
@@ -15,36 +15,45 @@ __generated_with = "0.23.4"
|
|
| 15 |
app = marimo.App()
|
| 16 |
|
| 17 |
with app.setup:
|
| 18 |
-
import marimo as mo
|
| 19 |
-
import geopandas as gpd
|
| 20 |
import folium
|
| 21 |
-
import
|
| 22 |
from folium.plugins import MarkerCluster
|
|
|
|
| 23 |
STOPS_LAYER_ID = "d6fc47eca97e48b7890c8fb7c9b69688"
|
| 24 |
STOPS_URL = r"https://services3.arcgis.com/NaFf4UaPo3IgQXqn/ArcGIS/rest/services/sb79_transit_stops/FeatureServer/0/query?where=0%3D0&objectIds=&geometry=&geometryType=esriGeometryEnvelope&inSR=&spatialRel=esriSpatialRelIntersects&resultType=none&distance=0.0&units=esriSRUnit_Meter&outDistance=&relationParam=&returnGeodetic=false&outFields=*&returnHiddenFields=false&returnGeometry=true&featureEncoding=esriDefault&multipatchOption=xyFootprint&maxAllowableOffset=&geometryPrecision=&outSR=&defaultSR=&datumTransformation=&applyVCSProjection=false&returnIdsOnly=false&returnUniqueIdsOnly=false&returnCountOnly=false&returnExtentOnly=false&returnQueryGeometry=false&returnDistinctValues=false&cacheHint=false&collation=&orderByFields=&groupByFieldsForStatistics=&returnAggIds=false&outStatistics=&having=&resultOffset=&resultRecordCount=&returnZ=false&returnM=false&returnTrueCurves=false&returnExceededLimitFeatures=true&quantizationParameters=&sqlFormat=none&f=pgeojson"
|
| 25 |
|
| 26 |
|
| 27 |
@app.cell
|
| 28 |
def _():
|
| 29 |
-
stops_df = gpd.read_file(STOPS_URL, driver=
|
| 30 |
stops_df.crs
|
| 31 |
return (stops_df,)
|
| 32 |
|
| 33 |
|
| 34 |
@app.cell
|
| 35 |
def _(stops_df):
|
| 36 |
-
m = folium.Map(
|
|
|
|
|
|
|
| 37 |
|
| 38 |
cluster = MarkerCluster(disable_clustering_at_zoom=10).add_to(m)
|
| 39 |
for _, _row in stops_df.iterrows():
|
| 40 |
-
color =
|
| 41 |
cluster.add_child(
|
| 42 |
folium.CircleMarker(
|
| 43 |
-
location=[_row.geometry.y, _row.geometry.x],
|
| 44 |
-
radius=5,
|
| 45 |
-
tooltip=folium.Tooltip(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
color=color,
|
| 47 |
-
fill_color=color
|
| 48 |
).add_to(m)
|
| 49 |
)
|
| 50 |
m
|
|
|
|
| 15 |
app = marimo.App()
|
| 16 |
|
| 17 |
with app.setup:
|
|
|
|
|
|
|
| 18 |
import folium
|
| 19 |
+
import geopandas as gpd
|
| 20 |
from folium.plugins import MarkerCluster
|
| 21 |
+
|
| 22 |
STOPS_LAYER_ID = "d6fc47eca97e48b7890c8fb7c9b69688"
|
| 23 |
STOPS_URL = r"https://services3.arcgis.com/NaFf4UaPo3IgQXqn/ArcGIS/rest/services/sb79_transit_stops/FeatureServer/0/query?where=0%3D0&objectIds=&geometry=&geometryType=esriGeometryEnvelope&inSR=&spatialRel=esriSpatialRelIntersects&resultType=none&distance=0.0&units=esriSRUnit_Meter&outDistance=&relationParam=&returnGeodetic=false&outFields=*&returnHiddenFields=false&returnGeometry=true&featureEncoding=esriDefault&multipatchOption=xyFootprint&maxAllowableOffset=&geometryPrecision=&outSR=&defaultSR=&datumTransformation=&applyVCSProjection=false&returnIdsOnly=false&returnUniqueIdsOnly=false&returnCountOnly=false&returnExtentOnly=false&returnQueryGeometry=false&returnDistinctValues=false&cacheHint=false&collation=&orderByFields=&groupByFieldsForStatistics=&returnAggIds=false&outStatistics=&having=&resultOffset=&resultRecordCount=&returnZ=false&returnM=false&returnTrueCurves=false&returnExceededLimitFeatures=true&quantizationParameters=&sqlFormat=none&f=pgeojson"
|
| 24 |
|
| 25 |
|
| 26 |
@app.cell
|
| 27 |
def _():
|
| 28 |
+
stops_df = gpd.read_file(STOPS_URL, driver="GeoJSON")
|
| 29 |
stops_df.crs
|
| 30 |
return (stops_df,)
|
| 31 |
|
| 32 |
|
| 33 |
@app.cell
|
| 34 |
def _(stops_df):
|
| 35 |
+
m = folium.Map(
|
| 36 |
+
location=[34.0617140033952, -118.314146442073], tiles="CartoDB Positron", zoom_start=10
|
| 37 |
+
)
|
| 38 |
|
| 39 |
cluster = MarkerCluster(disable_clustering_at_zoom=10).add_to(m)
|
| 40 |
for _, _row in stops_df.iterrows():
|
| 41 |
+
color = "blue" if _row["Tier"] == 2 else "red"
|
| 42 |
cluster.add_child(
|
| 43 |
folium.CircleMarker(
|
| 44 |
+
location=[_row.geometry.y, _row.geometry.x],
|
| 45 |
+
radius=5,
|
| 46 |
+
tooltip=folium.Tooltip(
|
| 47 |
+
f"""
|
| 48 |
+
Stop Name: {_row["stop_name"]}<br>
|
| 49 |
+
Tier: {_row["Tier"]}<br>
|
| 50 |
+
Routes: {_row["route_ids_served"]}<br>
|
| 51 |
+
City: {_row["city"]}<br>
|
| 52 |
+
County: {_row["county"]}<br>
|
| 53 |
+
Route type: {_row["routetypes"]}"""
|
| 54 |
+
),
|
| 55 |
color=color,
|
| 56 |
+
fill_color=color,
|
| 57 |
).add_to(m)
|
| 58 |
)
|
| 59 |
m
|
pyproject.toml
CHANGED
|
@@ -28,8 +28,14 @@ target-version = "py312"
|
|
| 28 |
|
| 29 |
[tool.ruff.lint]
|
| 30 |
select = [
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
| 35 |
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
[tool.ruff.lint]
|
| 30 |
select = [
|
| 31 |
+
"E", # pycodestyle
|
| 32 |
+
"F", # pyflakes
|
| 33 |
+
"I", # isort
|
| 34 |
+
"B", # flake8-bugbear
|
| 35 |
+
"W", # Pycodestyle (warnings)
|
| 36 |
+
"UP", # pyupgrade (modern Python syntax)
|
| 37 |
]
|
| 38 |
+
|
| 39 |
+
ignore = [
|
| 40 |
+
"B018", # Found useless expression (sometimes used for output in notebooks)
|
| 41 |
+
]
|
tests/test_sample.py
CHANGED
|
@@ -1,8 +1,6 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
def test_add():
|
| 4 |
-
assert 1+2 == 3
|
| 5 |
|
| 6 |
|
| 7 |
def test_subtract():
|
| 8 |
-
assert 1-2 == -1
|
|
|
|
|
|
|
|
|
|
| 1 |
def test_add():
|
| 2 |
+
assert 1 + 2 == 3
|
| 3 |
|
| 4 |
|
| 5 |
def test_subtract():
|
| 6 |
+
assert 1 - 2 == -1
|