Skip to content

Commit b19c3d3

Browse files
authored
Merge branch 'master' into python-control-flow
2 parents eb2a167 + 3c2d69f commit b19c3d3

26 files changed

+3590
-2
lines changed

marimo-notebook/README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Marimo: A Reactive, Reproducible Notebook
2+
3+
The materials contained in this download are designed to complement the Real Python tutorial [Marimo: A Reactive, Reproducible Notebook](https://realpython.com/marimo-notebook/).
4+
5+
## Installation
6+
7+
Create a [Python virtual environment](https://realpython.com/python-virtual-environments-a-primer/), activate it, and install marimo along with its dependencies into it:
8+
9+
```sh
10+
$ python -m venv venv/
11+
$ source venv/bin/activate
12+
(venv) $ python -m pip install -r requirements.txt
13+
```
14+
15+
## Contents
16+
17+
Your download bundle contains the following files:
18+
19+
| Filename | Description |
20+
|--------------------------------------------|-------------------------------------------------------------------------------------------------|
21+
| `hypotenuse_calculator.py` | The original `hypotenuse_calculator` code. |
22+
| `hypotenuse_calculator_before_update.py` | The code before any updating is attempted. The code is out of order but runs cleanly. |
23+
| `hypotenuse_calculator_duplicate_variable.py` | Shows the effect of redefining a variable. This file produces an error. |
24+
| `hypotenuse_calculator_after_update.py` | The code after the `adjacent` variable was updated to `10`. Runs cleanly. |
25+
| `hypotenuse_calculator_after_deletion.py` | The code after the `opposite` variable was deleted. This file produces an error. |
26+
| `break_even_analysis_chart_code.py` | The basic chart code for a break-even analysis with fixed input data. |
27+
| `break_even_analysis_ui_elements.py` | Includes four UI interface elements to allow the plot to be adjusted. |
28+
| `break_even_analysis_solution.py` | A possible solution to the skills test. |
29+
| `packages.py` | The code used to demonstrate sandboxing. |
30+
| `quadratic.py` | The marimo notebook version of the quadratic formula example. |
31+
| `equation.py` | The Python script version of `quadratic.py`. |
32+
| `simultaneous_equations.py` | The code used to demonstrate marimo's UI elements. |
33+
| `simultaneous_equations_ui.py` | A possible solution to the challenge skills test. |
34+
| `hidden_state.ipynb` | This Jupyter Notebook is a starting point for investigating its problems, to be adjusted per tutorial. |
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# flake8: noqa
2+
3+
import marimo
4+
5+
__generated_with = "0.13.6"
6+
app = marimo.App(width="medium")
7+
8+
9+
@app.cell
10+
def _():
11+
import marimo as mo
12+
import matplotlib.pyplot as plt
13+
14+
return (plt,)
15+
16+
17+
@app.cell
18+
def _():
19+
fixed_cost = 50000
20+
unit_cost = 2
21+
selling_price = 10
22+
upper_production_quantity = 10000
23+
return fixed_cost, selling_price, unit_cost, upper_production_quantity
24+
25+
26+
@app.cell
27+
def _(fixed_cost, plt, selling_price, unit_cost, upper_production_quantity):
28+
break_even_quantity = fixed_cost / (selling_price - unit_cost)
29+
break_even_income = fixed_cost + break_even_quantity * unit_cost
30+
31+
units = range(0, upper_production_quantity + 1, 1000)
32+
unit_costs = [(x * unit_cost) + fixed_cost for x in units]
33+
sales_income = [unit * selling_price for unit in units]
34+
35+
plt.plot(units, unit_costs, marker="o")
36+
plt.plot(units, sales_income, marker="x")
37+
38+
plt.xlabel("Units Produced")
39+
plt.ylabel("($)")
40+
plt.legend(["Total Costs", "Total Income"])
41+
plt.title("Break-Even Analysis")
42+
43+
plt.vlines(
44+
break_even_quantity,
45+
ymin=0,
46+
ymax=break_even_income,
47+
linestyles="dashed",
48+
)
49+
50+
plt.text(
51+
x=break_even_quantity + 100,
52+
y=int(break_even_income / 2),
53+
s=int(break_even_quantity),
54+
)
55+
56+
plt.grid()
57+
plt.show()
58+
return
59+
60+
61+
if __name__ == "__main__":
62+
app.run()
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# flake8: noqa
2+
3+
import marimo
4+
5+
__generated_with = "0.13.6"
6+
app = marimo.App(width="medium")
7+
8+
9+
@app.cell
10+
def _():
11+
import marimo as mo
12+
import matplotlib.pyplot as plt
13+
14+
return mo, plt
15+
16+
17+
@app.cell
18+
def _(ui_fixed_cost, ui_quantity, ui_selling_price, ui_unit_cost):
19+
fixed_cost = int(ui_fixed_cost.value)
20+
unit_cost = ui_unit_cost.value
21+
selling_price = float(ui_selling_price.value)
22+
upper_production_quantity = ui_quantity.value
23+
return fixed_cost, selling_price, unit_cost, upper_production_quantity
24+
25+
26+
@app.cell
27+
def _(
28+
fixed_cost,
29+
plt,
30+
selling_price,
31+
ui_break_even,
32+
ui_plot_color,
33+
unit_cost,
34+
upper_production_quantity,
35+
):
36+
break_even_quantity = fixed_cost / (selling_price - unit_cost)
37+
break_even_income = break_even_quantity * selling_price
38+
39+
units = range(0, upper_production_quantity + 1, 1000)
40+
total_costs = [(unit * unit_cost) + fixed_cost for unit in units]
41+
sales_income = [unit * selling_price for unit in units]
42+
43+
plt.plot(units, total_costs, marker="o", color=ui_plot_color.value)
44+
plt.plot(units, sales_income, marker="x")
45+
46+
plt.xlabel("Units Produced")
47+
plt.ylabel("($)")
48+
plt.legend(["Total Costs", "Total Income"])
49+
plt.title("Break-Even Analysis")
50+
51+
if ui_break_even.value:
52+
plt.vlines(
53+
break_even_quantity,
54+
ymin=100,
55+
ymax=break_even_income,
56+
linestyles="dashed",
57+
)
58+
59+
plt.text(
60+
x=break_even_quantity + 100,
61+
y=int(break_even_income / 2),
62+
s=int(break_even_quantity),
63+
)
64+
65+
plt.grid()
66+
plt.show()
67+
return
68+
69+
70+
@app.cell
71+
def _(mo):
72+
ui_fixed_cost = mo.ui.radio(options=["40000", "50000"], value="50000")
73+
ui_unit_cost = mo.ui.slider(start=2, stop=5, step=1)
74+
ui_selling_price = mo.ui.text(value="10")
75+
ui_quantity = mo.ui.dropdown(
76+
options={"10000": 10000, "12000": 12000, "15000": 15000}, value="10000"
77+
)
78+
ui_break_even = mo.ui.switch()
79+
ui_plot_color = mo.ui.dropdown(
80+
options={"Red": "red", "Green": "green", "Blue": "blue"}, value="Red"
81+
)
82+
83+
mo.md(
84+
f"""
85+
Fixed Costs: {ui_fixed_cost}
86+
87+
Unit Cost Price: {ui_unit_cost}
88+
89+
Selling Price: {ui_selling_price}
90+
91+
Maximum Quantity: {ui_quantity}
92+
93+
Display Break-Even Data: {ui_break_even}
94+
95+
Total Costs Plot Color: {ui_plot_color}
96+
"""
97+
)
98+
return (
99+
ui_break_even,
100+
ui_fixed_cost,
101+
ui_plot_color,
102+
ui_quantity,
103+
ui_selling_price,
104+
ui_unit_cost,
105+
)
106+
107+
108+
if __name__ == "__main__":
109+
app.run()
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# flake8: noqa
2+
3+
import marimo
4+
5+
__generated_with = "0.13.6"
6+
app = marimo.App(width="medium")
7+
8+
9+
@app.cell
10+
def _():
11+
import marimo as mo
12+
import matplotlib.pyplot as plt
13+
14+
return mo, plt
15+
16+
17+
@app.cell
18+
def _(ui_fixed_cost, ui_quantity, ui_selling_price, ui_unit_cost):
19+
fixed_cost = int(ui_fixed_cost.value)
20+
unit_cost = ui_unit_cost.value
21+
selling_price = float(ui_selling_price.value)
22+
upper_production_quantity = ui_quantity.value
23+
return fixed_cost, selling_price, unit_cost, upper_production_quantity
24+
25+
26+
@app.cell
27+
def _(
28+
fixed_cost,
29+
plt,
30+
selling_price,
31+
ui_color_costs,
32+
unit_cost,
33+
upper_production_quantity,
34+
):
35+
break_even_quantity = fixed_cost / (selling_price - unit_cost)
36+
break_even_income = break_even_quantity * selling_price
37+
38+
units = range(0, upper_production_quantity + 1, 1000)
39+
unit_costs = [(unit * unit_cost) + fixed_cost for unit in units]
40+
sales_income = [unit * selling_price for unit in units]
41+
42+
plt.plot(units, unit_costs, marker="o", color=ui_color_costs.value)
43+
plt.plot(units, sales_income, marker="x")
44+
45+
plt.xlabel("Units Produced")
46+
plt.ylabel("($)")
47+
plt.legend(["Total Costs", "Total Income"])
48+
plt.title("Break-Even Analysis")
49+
50+
plt.vlines(
51+
break_even_quantity,
52+
ymin=0,
53+
ymax=break_even_income,
54+
linestyles="dashed",
55+
)
56+
57+
plt.text(
58+
x=break_even_quantity + 100,
59+
y=int(break_even_income / 2),
60+
s=int(break_even_quantity),
61+
)
62+
63+
plt.grid()
64+
plt.show()
65+
return
66+
67+
68+
@app.cell
69+
def _(mo):
70+
ui_fixed_cost = mo.ui.radio(options=["40000", "50000"], value="50000")
71+
ui_unit_cost = mo.ui.slider(start=2, stop=5, step=1)
72+
ui_selling_price = mo.ui.text(value="10")
73+
ui_quantity = mo.ui.dropdown(
74+
options={"10000": 10000, "12000": 12000, "15000": 15000},
75+
value="10000",
76+
)
77+
ui_disply_break_even = mo.ui.switch()
78+
ui_color_costs = mo.ui.dropdown(
79+
options={"Red": "red", "Green": "green", "Blue": "blue"}, value="Red"
80+
)
81+
82+
mo.md(
83+
f"""
84+
Fixed Costs: {ui_fixed_cost}
85+
86+
Unit Cost Price: {ui_unit_cost}
87+
88+
Selling Price: {ui_selling_price}
89+
90+
Maximum Quantity: {ui_quantity}
91+
92+
Display Break-Even Data: {ui_disply_break_even}
93+
94+
Total Costs Plot Color: {ui_color_costs}
95+
"""
96+
)
97+
return (
98+
ui_color_costs,
99+
ui_fixed_cost,
100+
ui_quantity,
101+
ui_selling_price,
102+
ui_unit_cost,
103+
)
104+
105+
106+
if __name__ == "__main__":
107+
app.run()

marimo-notebook/equation.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# flake8: noqa
2+
3+
__generated_with = "0.13.6"
4+
5+
# %%
6+
import marimo as mo
7+
8+
# %%
9+
mo.md(
10+
r"""
11+
A quadratic equation is one of the form **$ax^2 + bx + c = 0$**, where a, b and c are constants, and a $\neq$ 0.
12+
13+
You can solve it using the *quadratic formula*:
14+
15+
$$x = \frac {-b \pm \sqrt{b^2 -4ac}} {2a}$$
16+
17+
For example, suppose you wanted to solve: **$2x^2 - 3x - 2 = 0$**
18+
"""
19+
)
20+
21+
# %%
22+
a = 2
23+
24+
# %%
25+
import math
26+
27+
# %%
28+
b = -3
29+
30+
# %%
31+
c = -2
32+
33+
# %%
34+
x1 = (-b + math.sqrt(b**2 - 4 * a * c)) / (2 * a)
35+
36+
# %%
37+
x2 = (-b - math.sqrt(b**2 - 4 * a * c)) / (2 * a)
38+
39+
# %%
40+
print(f"x = {x1} and {x2}.")

0 commit comments

Comments
 (0)