Skip to content

Commit 3811142

Browse files
authored
Merge pull request #640 from realpython/marimo-notebook
marimo
2 parents 20a870a + d13da64 commit 3811142

15 files changed

+991
-0
lines changed

marimo-notebook/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
The materials contained in this download are designed to complement the Real Python tutorial [Marimo: A Reactive, Reproducible Notebook](https://realpython.com/marimo-notebook-reactive-reproducible/).
2+
3+
You should create a new folder named marimo on your computer and place each of these files inside it. You might also consider creating a [Python virtual environment](https://realpython.com/python-virtual-environments-a-primer/) within this folder.
4+
5+
Your download bundle contains the following files:
6+
7+
hypotenuse_calculator.py - This file contains the original hypotenuse_calculator code
8+
hypotenuse_calculator_before_update.py - This file contains the code before any updating is attempted. The code has been deliberately placed out of order, however it runs cleanly.
9+
hypotenuse_calculator_duplicate_variable.py - This file shows the effect of re-defining a variable. This file produces an error.
10+
hypotenuse_calculator_after_update.py - This file contains the code after the `adjacent` variable was updated to `10`. This file runs cleanly.
11+
hypotenuse_calculator_after_deletion.py - This file contains the code after the `opposite variable` was deleted. This file produces an error.
12+
13+
break_even_analysis_chart_code.py - This file contains the basic chart code. It will produce a break-even analysis for a fixed set of input data.
14+
break_even_analysis_UI_elements.py - This file contains includes the four UI interface elements to allow the plot to be adjusted.
15+
break_even_analysis_solution.py - This file contains a possible solution to the skills test.
16+
17+
packages.py - This file contains the code used to demonstrate sandboxing.
18+
quadratic.py - This file contains the marimo notebook version of the quadratic formula example.
19+
equation.py - This file contains the Python script version of quadratic.py.
20+
21+
simultaneous_equations.py - This file contains the code used to demonstrate marimo's UI elements.
22+
simultaneous_equations_ui.py - This file contains a possible solution to the challenge skills test.
23+
24+
hidden_state.ipynb - This file contains a Jupyter Notebook that can be used as a starting point for the investigation of its problems. You should adjust it as instructed in the tutorial.
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# flake8: noqa
2+
3+
import marimo
4+
5+
__generated_with = "0.11.0"
6+
app = marimo.App(width="medium")
7+
8+
9+
@app.cell
10+
def _():
11+
import matplotlib.pyplot as plt
12+
import marimo as mo
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+
plt.text(
57+
x=break_even_quantity + 100,
58+
y=int(break_even_income / 2),
59+
s=int(break_even_quantity),
60+
)
61+
plt.grid()
62+
plt.show()
63+
return (
64+
break_even_income,
65+
break_even_quantity,
66+
sales_income,
67+
unit_costs,
68+
units,
69+
)
70+
71+
72+
@app.cell
73+
def _(mo):
74+
ui_fixed_cost = mo.ui.radio(options=["40000", "50000"], value="50000")
75+
76+
ui_unit_cost = mo.ui.slider(start=2, stop=5, step=1)
77+
78+
ui_selling_price = mo.ui.text(value="10")
79+
80+
ui_quantity = mo.ui.dropdown(
81+
options={"10000": 10000, "12000": 12000, "15000": 15000},
82+
value="10000",
83+
)
84+
85+
ui_disply_break_even = mo.ui.switch()
86+
87+
ui_color_costs = mo.ui.dropdown(
88+
options={"Red": "red", "Green": "green", "Blue": "blue"}, value="Red"
89+
)
90+
91+
mo.md(
92+
f"""
93+
Fixed Costs: {ui_fixed_cost}
94+
95+
Unit Cost Price: {ui_unit_cost}
96+
97+
Selling Price: {ui_selling_price}
98+
99+
Maximum Production Quantity: {ui_quantity}
100+
"""
101+
)
102+
return (
103+
ui_color_costs,
104+
ui_disply_break_even,
105+
ui_fixed_cost,
106+
ui_quantity,
107+
ui_selling_price,
108+
ui_unit_cost,
109+
)
110+
111+
112+
if __name__ == "__main__":
113+
app.run()
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# flake8: noqa
2+
3+
import marimo
4+
5+
__generated_with = "0.11.0"
6+
app = marimo.App(width="medium")
7+
8+
9+
@app.cell
10+
def _():
11+
import matplotlib.pyplot as plt
12+
import marimo as mo
13+
14+
return mo, 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 _(
28+
fixed_cost,
29+
plt,
30+
selling_price,
31+
unit_cost,
32+
upper_production_quantity,
33+
):
34+
break_even_quantity = fixed_cost / (selling_price - unit_cost)
35+
break_even_income = fixed_cost + break_even_quantity * unit_cost
36+
37+
units = range(0, upper_production_quantity + 1, 1000)
38+
unit_costs = [(x * unit_cost) + fixed_cost for x in units]
39+
sales_income = [unit * selling_price for unit in units]
40+
41+
plt.plot(units, unit_costs, marker="o")
42+
plt.plot(units, sales_income, marker="x")
43+
44+
plt.xlabel("Units Produced")
45+
plt.ylabel("($)")
46+
plt.legend(["Total Costs", "Total Income"])
47+
plt.title("Break-Even Analysis")
48+
49+
plt.vlines(
50+
break_even_quantity,
51+
ymin=0,
52+
ymax=break_even_income,
53+
linestyles="dashed",
54+
)
55+
plt.text(
56+
x=break_even_quantity + 100,
57+
y=int(break_even_income / 2),
58+
s=int(break_even_quantity),
59+
)
60+
plt.grid()
61+
plt.show()
62+
return (
63+
break_even_income,
64+
break_even_quantity,
65+
sales_income,
66+
unit_costs,
67+
units,
68+
)
69+
70+
71+
if __name__ == "__main__":
72+
app.run()
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# flake8: noqa
2+
3+
import marimo
4+
5+
__generated_with = "0.11.0"
6+
app = marimo.App(width="medium")
7+
8+
9+
@app.cell
10+
def _():
11+
import matplotlib.pyplot as plt
12+
import marimo as mo
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+
break_even_income,
69+
break_even_quantity,
70+
sales_income,
71+
total_costs,
72+
units,
73+
)
74+
75+
76+
@app.cell
77+
def _(mo):
78+
ui_fixed_cost = mo.ui.radio(options=["40000", "50000"], value="50000")
79+
80+
ui_unit_cost = mo.ui.slider(start=2, stop=5, step=1)
81+
82+
ui_selling_price = mo.ui.text(value="10")
83+
84+
ui_quantity = mo.ui.dropdown(
85+
options={"10000": 10000, "12000": 12000, "15000": 15000}, value="10000"
86+
)
87+
88+
ui_break_even = mo.ui.switch()
89+
90+
ui_plot_color = mo.ui.dropdown(
91+
options={"Red": "red", "Green": "green", "Blue": "blue"}, value="Red"
92+
)
93+
94+
mo.md(
95+
f"""
96+
Fixed Costs: {ui_fixed_cost}
97+
98+
Unit Cost Price: {ui_unit_cost}
99+
100+
Selling Price: {ui_selling_price}
101+
102+
Maximum Quantity: {ui_quantity}
103+
104+
Display Break-Even Data: {ui_break_even}
105+
106+
Total Costs Plot Color: {ui_plot_color}
107+
"""
108+
)
109+
return (
110+
ui_break_even,
111+
ui_fixed_cost,
112+
ui_plot_color,
113+
ui_quantity,
114+
ui_selling_price,
115+
ui_unit_cost,
116+
)
117+
118+
119+
if __name__ == "__main__":
120+
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.11.0"
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)