Skip to content

Commit

Permalink
improvement: showcase loading external data (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
mscolnick authored Jan 10, 2025
1 parent 1f5b918 commit 522c133
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
- name: 📤 Upload artifact
uses: actions/upload-pages-artifact@v3
with:
path: public
path: _site

deploy:
needs: build
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
_site/

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand Down
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ This template repository demonstrates how to export [marimo](https://marimo.io)

- `apps/charts.py`: Interactive data visualization with Altair
- `notebooks/fibonacci.py`: Interactive Fibonacci sequence calculator
- `notebooks/penguins.py`: Interactive data analysis with Polars and marimo

## 🚀 Usage

Expand All @@ -16,3 +17,36 @@ This template repository demonstrates how to export [marimo](https://marimo.io)
3. Push to main branch
4. Go to repository **Settings > Pages** and change the "Source" dropdown to "GitHub Actions"
5. GitHub Actions will automatically build and deploy to Pages

## Including data or assets

To include data or assets in your notebooks, add them to the `public/` directory.

For example, the `apps/charts.py` notebook loads an image asset from the `public/` directory.

```markdown
<img src="public/logo.png" width="200" />
```

And the `notebooks/penguins.py` notebook loads a CSV dataset from the `public/` directory.

```python
import polars as pl
df = pl.read_csv(mo.notebook_location() / "public" / "penguins.csv")
```

## 🧪 Testing

To test the export process, run `scripts/build.py` from the root directory.

```bash
python scripts/build.py
```

This will export all notebooks in a folder called `_site/` in the root directory. Then to serve the site, run:

```bash
python -m http.server -d _site
```

This will serve the site at `http://localhost:8000`.
4 changes: 3 additions & 1 deletion apps/charts.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import marimo

__generated_with = "0.10.6"
__generated_with = "0.10.9"
app = marimo.App(width="medium")


Expand All @@ -19,6 +19,8 @@ def _(mo):
"""
# Interactive Data Visualization
<img src="public/logo.png" width="200" />
This notebook demonstrates a simple interactive visualization using Altair.
Try selecting the points!
"""
Expand Down
99 changes: 99 additions & 0 deletions notebooks/penguins.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import marimo

__generated_with = "0.10.9"
app = marimo.App(width="medium")


@app.cell
def _():
import polars as pl
import marimo as mo
import altair as alt
return alt, mo, pl


@app.cell(hide_code=True)
def _(mo):
mo.md(
"""
# Palmer Penguins Analysis
Analyzing the Palmer Penguins dataset using Polars and marimo.
"""
)
return


@app.cell
def _(mo, pl):
# Read the penguins dataset
df = pl.read_csv(mo.notebook_location() / "public" / "penguins.csv")
df.head()
return (df,)


@app.cell
def _(df, mo):
# Basic statistics
mo.md(f"""
### Dataset Overview
- Total records: {df.height}
- Columns: {', '.join(df.columns)}
### Summary Statistics
{mo.as_html(df.describe())}
""")
return


@app.cell(hide_code=True)
def _(mo):
mo.md(r"""### Species Distribution""")
return


@app.cell
def _(alt, df, mo):
# Create species distribution chart
species_chart = mo.ui.altair_chart(
alt.Chart(df)
.mark_bar()
.encode(x="species", y="count()", color="species")
.properties(title="Distribution of Penguin Species"),
chart_selection=None,
)

species_chart
return (species_chart,)


@app.cell(hide_code=True)
def _(mo):
mo.md(r"""### Bill Dimensions Analysis""")
return


@app.cell
def _(alt, df, mo):
# Scatter plot of bill dimensions
scatter = mo.ui.altair_chart(
alt.Chart(df)
.mark_point()
.encode(
x="bill_length_mm",
y="bill_depth_mm",
color="species",
tooltip=["species", "bill_length_mm", "bill_depth_mm"],
)
.properties(title="Bill Length vs Depth by Species"),
chart_selection=None,
)

scatter
return (scatter,)


if __name__ == "__main__":
app.run()
2 changes: 1 addition & 1 deletion scripts/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def generate_index(all_notebooks: List[str], output_dir: str) -> None:
def main() -> None:
parser = argparse.ArgumentParser(description="Build marimo notebooks")
parser.add_argument(
"--output-dir", default="public", help="Output directory for built files"
"--output-dir", default="_site", help="Output directory for built files"
)
args = parser.parse_args()

Expand Down

0 comments on commit 522c133

Please sign in to comment.