Display an output along with the code of the current cell. Use `mo.show_code` to show the code of the current cell along with the cell's output. This is useful if you want a cell's code to appear in the app preview or when running the notebook as an app with `marimo run`. In th
(
output: object = None, *, position: Literal["above", "below"] = "below"
)
| 33 | |
| 34 | |
| 35 | def show_code( |
| 36 | output: object = None, *, position: Literal["above", "below"] = "below" |
| 37 | ) -> Html: |
| 38 | """Display an output along with the code of the current cell. |
| 39 | |
| 40 | Use `mo.show_code` to show the code of the current cell along with |
| 41 | the cell's output. This is useful if you want a cell's code to |
| 42 | appear in the app preview or when running the notebook as an app |
| 43 | with `marimo run`. |
| 44 | |
| 45 | In the displayed code, all occurrences of mo.show_code(...) will be |
| 46 | replaced with ... |
| 47 | |
| 48 | Show code that produces the output `factorial(5)`: |
| 49 | |
| 50 | ```python |
| 51 | def factorial(n: int) -> int: |
| 52 | if n == 0: |
| 53 | return 1 |
| 54 | return n * factorial(n - 1) |
| 55 | |
| 56 | |
| 57 | mo.show_code(factorial(5)) |
| 58 | ``` |
| 59 | |
| 60 | Show code of a cell, without an output: |
| 61 | |
| 62 | ```python |
| 63 | def factorial(n: int) -> int: |
| 64 | if n == 0: |
| 65 | return 1 |
| 66 | return n * factorial(n - 1) |
| 67 | |
| 68 | |
| 69 | mo.show_code() |
| 70 | ``` |
| 71 | |
| 72 | Args: |
| 73 | output: the output to display with the cell's code; omit the output |
| 74 | to just show the cell's code. |
| 75 | position: Where to display the code relative to the output. |
| 76 | Use "above" to show code above the output, or "below" (default) to show |
| 77 | code below the output. |
| 78 | |
| 79 | Returns: |
| 80 | HTML of the `output` arg displayed with its code. |
| 81 | """ |
| 82 | assert position in ["above", "below"], ( |
| 83 | "position must be 'above' or 'below'" |
| 84 | ) |
| 85 | |
| 86 | try: |
| 87 | context = get_context() |
| 88 | except ContextNotInitializedError: |
| 89 | return as_html(output) |
| 90 | |
| 91 | cell_id = context.cell_id |
| 92 | if cell_id is None: |
searching dependent graphs…