Mojo Playpen demo

This page is just for demonstration and experimentation with Playpen features, such as hiding code from readers and creating dependencies between code cells.


Fully visible code

The UI should include a Run button, which will run the main() function and print any output to a code block below this one.

::: {.cell 0=‘p’ 1=‘l’ 2=‘a’ 3=‘y’ 4=‘p’ 5=‘e’ 6=‘n’ execution_count=1}

fn main():
    print("Hello world!")


Partially hidden code

The user sees only the print() function call.

::: {.cell 0=‘p’ 1=‘l’ 2=‘a’ 3=‘y’ 4=‘p’ 5=‘e’ 6=‘n’}

#: from math import sqrt

#: fn main():


If the reader wants to edit the code, they must click an Edit button, which then shows all the hidden code.

As such, Playpen code is not editable at first glance. Users can run the code, but they must click a button to make it editable, because there might be some code that is not visible by default. Only the edit mode shows all the code.

Lots of hidden code

In a real document, the below MyPet struct would also be shown in a code block. To ensure a predictable experience when editing and running the code, we must also include it in any code block that wants to use it.

If the user wants to edit this code, they click the Edit button that expands the code cell and shows the MyPet definition that they can edit.

::: {.cell 0=‘p’ 1=‘l’ 2=‘a’ 3=‘y’ 4=‘p’ 5=‘e’ 6=‘n’}

#: struct MyPet:
#:     var name: String
#:     var age: Int
#:     fn __init__(inout self, name: String, age: Int):
#: = name
#:         self.age = age
#:     fn __copyinit__(inout self, existing: Self):
#: =
#:         self.age = existing.age

fn main():
    let mine = MyPet("Loki", 4)
    let yours = mine