|
| 1 | +import marimo |
| 2 | + |
| 3 | +__generated_with = "0.12.8" |
| 4 | +app = marimo.App(width="medium") |
| 5 | + |
| 6 | + |
| 7 | +@app.cell |
| 8 | +def _(): |
| 9 | + import marimo as mo |
| 10 | + import pandas as pd |
| 11 | + import random |
| 12 | + |
| 13 | + # Create a simple dataset |
| 14 | + data = { |
| 15 | + 'product': ['Widget A', 'Widget B', 'Widget C', 'Widget D', 'Widget E'], |
| 16 | + 'category': ['Electronics', 'Office', 'Electronics', 'Kitchen', 'Office'], |
| 17 | + 'price': [random.randint(10, 100) for _ in range(5)], |
| 18 | + 'quantity': [random.randint(1, 50) for _ in range(5)] |
| 19 | + } |
| 20 | + |
| 21 | + # Create a pandas DataFrame |
| 22 | + products_df = pd.DataFrame(data) |
| 23 | + |
| 24 | + # Display the dataframe |
| 25 | + products_df |
| 26 | + return data, mo, pd, products_df, random |
| 27 | + |
| 28 | + |
| 29 | +@app.cell |
| 30 | +def _(): |
| 31 | + select_price = 30 |
| 32 | + return (select_price,) |
| 33 | + |
| 34 | + |
| 35 | +@app.cell |
| 36 | +def _(mo, products_df, select_price): |
| 37 | + # Using SQL to query our products dataframe |
| 38 | + result = mo.sql(f"SELECT * FROM products_df WHERE price > {select_price}") |
| 39 | + # Display the result |
| 40 | + result |
| 41 | + return (result,) |
| 42 | + |
| 43 | + |
| 44 | +@app.cell |
| 45 | +def _(mo, products_df): |
| 46 | + # Create a dropdown for category selection |
| 47 | + category_filter = mo.ui.dropdown( |
| 48 | + products_df['category'].unique().tolist(), |
| 49 | + label="Filter by category", |
| 50 | + value=products_df['category'].unique().tolist()[0] |
| 51 | + ) |
| 52 | + |
| 53 | + # Display the dropdown |
| 54 | + category_filter |
| 55 | + return (category_filter,) |
| 56 | + |
| 57 | + |
| 58 | +@app.cell |
| 59 | +def _(category_filter, mo, products_df): |
| 60 | + # Query that reacts to the dropdown selection |
| 61 | + filtered_result = mo.sql(f""" |
| 62 | + -- This query will update automatically when the dropdown value changes |
| 63 | + SELECT * FROM products_df |
| 64 | + WHERE category = '{category_filter.value}' |
| 65 | + """) |
| 66 | + |
| 67 | + # Display the filtered results |
| 68 | + filtered_result |
| 69 | + return (filtered_result,) |
| 70 | + |
| 71 | + |
| 72 | +@app.cell(hide_code=True) |
| 73 | +def _(mo): |
| 74 | + import matplotlib.pyplot as plt |
| 75 | + |
| 76 | + # Create a price range slider |
| 77 | + price_range = mo.ui.slider( |
| 78 | + 0, |
| 79 | + 100, |
| 80 | + value=50, |
| 81 | + label="Maximum price" |
| 82 | + ) |
| 83 | + |
| 84 | + # Display the slider |
| 85 | + price_range |
| 86 | + return plt, price_range |
| 87 | + |
| 88 | + |
| 89 | +@app.cell(hide_code=True) |
| 90 | +def _(mo, plt, price_range, products_df): |
| 91 | + # Query with price filter |
| 92 | + price_filtered = mo.sql(f""" |
| 93 | + SELECT * FROM products_df |
| 94 | + WHERE price <= {price_range.value} |
| 95 | + """) |
| 96 | + |
| 97 | + # Create a visualization |
| 98 | + plt.figure(figsize=(10, 5)) |
| 99 | + plt.bar(price_filtered['product'], price_filtered['price']) |
| 100 | + plt.xlabel('Product') |
| 101 | + plt.ylabel('Price ($)') |
| 102 | + plt.title(f'Products with price <= ${price_range.value}') |
| 103 | + plt.xticks(rotation=45) |
| 104 | + plt.gcf() |
| 105 | + |
| 106 | + return (price_filtered,) |
| 107 | + |
| 108 | + |
| 109 | +@app.cell |
| 110 | +def _(cars, mo): |
| 111 | + # Query external CSV file directly |
| 112 | + cars = mo.sql(""" |
| 113 | + -- Download a CSV and create an in-memory table |
| 114 | + CREATE OR REPLACE TABLE cars AS |
| 115 | + FROM 'https://datasets.marimo.app/cars.csv'; |
| 116 | + |
| 117 | + -- Query the table |
| 118 | + SELECT Origin, AVG(MPG_City) as avg_city_mpg, AVG(MPG_Highway) as avg_highway_mpg |
| 119 | + FROM cars |
| 120 | + GROUP BY Origin |
| 121 | + ORDER BY avg_city_mpg DESC; |
| 122 | + """) |
| 123 | + |
| 124 | + # Display the results |
| 125 | + cars |
| 126 | + return (cars,) |
| 127 | + |
| 128 | + |
| 129 | +@app.cell |
| 130 | +def _(cars, mo): |
| 131 | + # Create a dropdown for selecting car origin |
| 132 | + origin_dropdown = mo.ui.dropdown.from_series( |
| 133 | + mo.sql("SELECT DISTINCT Origin FROM cars")["Origin"], |
| 134 | + value="Asia" |
| 135 | + ) |
| 136 | + |
| 137 | + # Display the dropdown |
| 138 | + origin_dropdown |
| 139 | + return (origin_dropdown,) |
| 140 | + |
| 141 | + |
| 142 | +@app.cell |
| 143 | +def _(cars, mo, origin_dropdown): |
| 144 | + # Query that filters based on selection |
| 145 | + filtered_cars = mo.sql(f""" |
| 146 | + SELECT * FROM cars |
| 147 | + WHERE Origin = '{origin_dropdown.value}' |
| 148 | + ORDER BY MPG_Highway DESC |
| 149 | + LIMIT 10 |
| 150 | + """) |
| 151 | + |
| 152 | + # Create statistics summary |
| 153 | + mo.hstack([ |
| 154 | + mo.stat(label="Total cars", value=str(len(filtered_cars))), |
| 155 | + mo.stat(label="Avg MPG Highway", value=f"{filtered_cars['MPG_Highway'].mean():.1f}"), |
| 156 | + mo.stat(label="Avg MPG City", value=f"{filtered_cars['MPG_City'].mean():.1f}") |
| 157 | + ]) |
| 158 | + return (filtered_cars,) |
| 159 | + |
| 160 | + |
| 161 | +if __name__ == "__main__": |
| 162 | + app.run() |
0 commit comments