Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions marimo-notebook/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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/).

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.

Your download bundle contains the following files:

hypotenuse_calculator.py - This file contains the original hypotenuse_calculator code
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.
hypotenuse_calculator_duplicate_variable.py - This file shows the effect of re-defining a variable. This file produces an error.
hypotenuse_calculator_after_update.py - This file contains the code after the `adjacent` variable was updated to `10`. This file runs cleanly.
hypotenuse_calculator_after_deletion.py - This file contains the code after the `opposite variable` was deleted. This file produces an error.

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.
break_even_analysis_UI_elements.py - This file contains includes the four UI interface elements to allow the plot to be adjusted.
break_even_analysis_solution.py - This file contains a possible solution to the skills test.

packages.py - This file contains the code used to demonstrate sandboxing.
quadratic.py - This file contains the marimo notebook version of the quadratic formula example.
equation.py - This file contains the Python script version of quadratic.py.

simultaneous_equations.py - This file contains the code used to demonstrate marimo's UI elements.
simultaneous_equations_ui.py - This file contains a possible solution to the challenge skills test.

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.
113 changes: 113 additions & 0 deletions marimo-notebook/break_even_analysis_UI_elements.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# flake8: noqa

import marimo

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


@app.cell
def _():
import matplotlib.pyplot as plt
import marimo as mo

return mo, plt


@app.cell
def _(ui_fixed_cost, ui_quantity, ui_selling_price, ui_unit_cost):
fixed_cost = int(ui_fixed_cost.value)
unit_cost = ui_unit_cost.value
selling_price = float(ui_selling_price.value)
upper_production_quantity = ui_quantity.value
return fixed_cost, selling_price, unit_cost, upper_production_quantity


@app.cell
def _(
fixed_cost,
plt,
selling_price,
ui_color_costs,
unit_cost,
upper_production_quantity,
):
break_even_quantity = fixed_cost / (selling_price - unit_cost)
break_even_income = break_even_quantity * selling_price

units = range(0, upper_production_quantity + 1, 1000)
unit_costs = [(unit * unit_cost) + fixed_cost for unit in units]
sales_income = [unit * selling_price for unit in units]

plt.plot(units, unit_costs, marker="o", color=ui_color_costs.value)
plt.plot(units, sales_income, marker="x")

plt.xlabel("Units Produced")
plt.ylabel("($)")
plt.legend(["Total Costs", "Total Income"])
plt.title("Break-Even Analysis")

plt.vlines(
break_even_quantity,
ymin=0,
ymax=break_even_income,
linestyles="dashed",
)
plt.text(
x=break_even_quantity + 100,
y=int(break_even_income / 2),
s=int(break_even_quantity),
)
plt.grid()
plt.show()
return (
break_even_income,
break_even_quantity,
sales_income,
unit_costs,
units,
)


@app.cell
def _(mo):
ui_fixed_cost = mo.ui.radio(options=["40000", "50000"], value="50000")

ui_unit_cost = mo.ui.slider(start=2, stop=5, step=1)

ui_selling_price = mo.ui.text(value="10")

ui_quantity = mo.ui.dropdown(
options={"10000": 10000, "12000": 12000, "15000": 15000},
value="10000",
)

ui_disply_break_even = mo.ui.switch()

ui_color_costs = mo.ui.dropdown(
options={"Red": "red", "Green": "green", "Blue": "blue"}, value="Red"
)

mo.md(
f"""
Fixed Costs: {ui_fixed_cost}

Unit Cost Price: {ui_unit_cost}

Selling Price: {ui_selling_price}

Maximum Production Quantity: {ui_quantity}
"""
)
return (
ui_color_costs,
ui_disply_break_even,
ui_fixed_cost,
ui_quantity,
ui_selling_price,
ui_unit_cost,
)


if __name__ == "__main__":
app.run()
72 changes: 72 additions & 0 deletions marimo-notebook/break_even_analysis_chart_code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# flake8: noqa

import marimo

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


@app.cell
def _():
import matplotlib.pyplot as plt
import marimo as mo

return mo, plt


@app.cell
def _():
fixed_cost = 50000
unit_cost = 2
selling_price = 10
upper_production_quantity = 10000
return fixed_cost, selling_price, unit_cost, upper_production_quantity


@app.cell
def _(
fixed_cost,
plt,
selling_price,
unit_cost,
upper_production_quantity,
):
break_even_quantity = fixed_cost / (selling_price - unit_cost)
break_even_income = fixed_cost + break_even_quantity * unit_cost

units = range(0, upper_production_quantity + 1, 1000)
unit_costs = [(x * unit_cost) + fixed_cost for x in units]
sales_income = [unit * selling_price for unit in units]

plt.plot(units, unit_costs, marker="o")
plt.plot(units, sales_income, marker="x")

plt.xlabel("Units Produced")
plt.ylabel("($)")
plt.legend(["Total Costs", "Total Income"])
plt.title("Break-Even Analysis")

plt.vlines(
break_even_quantity,
ymin=0,
ymax=break_even_income,
linestyles="dashed",
)
plt.text(
x=break_even_quantity + 100,
y=int(break_even_income / 2),
s=int(break_even_quantity),
)
plt.grid()
plt.show()
return (
break_even_income,
break_even_quantity,
sales_income,
unit_costs,
units,
)


if __name__ == "__main__":
app.run()
120 changes: 120 additions & 0 deletions marimo-notebook/break_even_analysis_solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
# flake8: noqa

import marimo

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


@app.cell
def _():
import matplotlib.pyplot as plt
import marimo as mo

return mo, plt


@app.cell
def _(ui_fixed_cost, ui_quantity, ui_selling_price, ui_unit_cost):
fixed_cost = int(ui_fixed_cost.value)
unit_cost = ui_unit_cost.value
selling_price = float(ui_selling_price.value)
upper_production_quantity = ui_quantity.value
return fixed_cost, selling_price, unit_cost, upper_production_quantity


@app.cell
def _(
fixed_cost,
plt,
selling_price,
ui_break_even,
ui_plot_color,
unit_cost,
upper_production_quantity,
):
break_even_quantity = fixed_cost / (selling_price - unit_cost)
break_even_income = break_even_quantity * selling_price

units = range(0, upper_production_quantity + 1, 1000)
total_costs = [(unit * unit_cost) + fixed_cost for unit in units]
sales_income = [unit * selling_price for unit in units]

plt.plot(units, total_costs, marker="o", color=ui_plot_color.value)
plt.plot(units, sales_income, marker="x")

plt.xlabel("Units Produced")
plt.ylabel("($)")
plt.legend(["Total Costs", "Total Income"])
plt.title("Break-Even Analysis")

if ui_break_even.value:
plt.vlines(
break_even_quantity,
ymin=100,
ymax=break_even_income,
linestyles="dashed",
)

plt.text(
x=break_even_quantity + 100,
y=int(break_even_income / 2),
s=int(break_even_quantity),
)

plt.grid()
plt.show()
return (
break_even_income,
break_even_quantity,
sales_income,
total_costs,
units,
)


@app.cell
def _(mo):
ui_fixed_cost = mo.ui.radio(options=["40000", "50000"], value="50000")

ui_unit_cost = mo.ui.slider(start=2, stop=5, step=1)

ui_selling_price = mo.ui.text(value="10")

ui_quantity = mo.ui.dropdown(
options={"10000": 10000, "12000": 12000, "15000": 15000}, value="10000"
)

ui_break_even = mo.ui.switch()

ui_plot_color = mo.ui.dropdown(
options={"Red": "red", "Green": "green", "Blue": "blue"}, value="Red"
)

mo.md(
f"""
Fixed Costs: {ui_fixed_cost}

Unit Cost Price: {ui_unit_cost}

Selling Price: {ui_selling_price}

Maximum Quantity: {ui_quantity}

Display Break-Even Data: {ui_break_even}

Total Costs Plot Color: {ui_plot_color}
"""
)
return (
ui_break_even,
ui_fixed_cost,
ui_plot_color,
ui_quantity,
ui_selling_price,
ui_unit_cost,
)


if __name__ == "__main__":
app.run()
40 changes: 40 additions & 0 deletions marimo-notebook/equation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# flake8: noqa

__generated_with = "0.11.0"

# %%
import marimo as mo

# %%
mo.md(
r"""
A quadratic equation is one of the form **$ax^2 + bx + c = 0$**, where a, b and c are constants, and a $\neq$ 0.

You can solve it using the *quadratic formula*:

$$x = \frac {-b \pm \sqrt{b^2 -4ac}} {2a}$$

For example, suppose you wanted to solve: **$2x^2 - 3x - 2 = 0$**
"""
)

# %%
a = 2

# %%
import math

# %%
b = -3

# %%
c = -2

# %%
x1 = (-b + math.sqrt(b**2 - 4 * a * c)) / (2 * a)

# %%
x2 = (-b - math.sqrt(b**2 - 4 * a * c)) / (2 * a)

# %%
print(f"x = {x1} and {x2}.")
Loading