Blocks helps you take the next step from graphical blocks to Python. You can see the Python behind a block or stack, edit and run a Python program, or write a Python block alongside your graphical code.
This feature is currently Beta. Start with a short program, check the generated code, and use the Output panel to help spot mistakes.
You can preview code and convert supported Python to blocks without a connected robot. Running commands that control Marty requires a connection.
| What would you like to do? | Where to go |
|---|---|
| See the Python for one graphical block | Right-click the block and choose Show Python for this block. |
| See the Python for a connected stack | Right-click a block and choose Show Python for this stack. |
| Edit and run a Python program | Click the Marty Python button above the coding area. |
| Write Python within your block program | Right-click an empty area and choose Add Python block. |
| Turn supported Python into graphical blocks | Right-click a Python block and choose Convert Python to blocks. |
The preview lets you compare graphical instructions with their Python equivalents. Opening it does not replace your blocks or run the code. To edit the code, paste it into the Marty Python editor or a Python block.
For example, the Get ready! block becomes:
my_marty.get_ready()
Here, my_marty refers to the Marty you control, and get_ready() tells it what to do. Values inside parentheses customise a command, just as the number fields and menus customise a graphical block.
Try this short example in the editor. It gets Marty ready, then takes three forward steps with a pause between each one:
import time
my_marty.get_ready()
for step in range(3):
my_marty.walk(num_steps=1, turn=0, step_length=25)
time.sleep(0.5)
print("Finished!")
The indented lines belong to the loop. Use four spaces for each indentation level. time.sleep(0.5) waits for half a second, and print() writes a message in Output.
Reset from blocks regenerates the Python from your current project and replaces your Python edits. Editing Python in this window does not automatically change the graphical project. Use Copy to clipboard if you want to keep a copy of your edited code.
You can move the window by dragging its header and resize it using its edges or corners. Maximise gives you more space; Minimise tucks it away so you can see the blocks again.
A Python block runs the code written inside it when the project reaches that block. For example, you can put a graphical Get ready! block before a Python block containing a short movement routine. Use the green flag when your stack has a green-flag event, or click the stack to run it.
While the Python is running, the block shows a running indication. If the code fails, an error indication helps you find the problem. Check names, values and indentation before trying again.
Start with commands copied from a block preview, or type a supported Marty command yourself. For example, enter these two lines into a Python block:
my_marty.get_ready()
my_marty.walk(num_steps=10, turn=0, step_length=25)
Right-click the Python block and choose Convert Python to blocks.
The example becomes a graphical stack containing Get ready! and Walk 10 steps forwards:
Conversion supports the Python patterns that Blocks can represent, including supported Marty commands and loops. It is not a converter for every possible Python program. If a line cannot be represented, the conversion reports the problem and keeps the Python block so you can edit it. Use Undo if you want to return to the Python block after a successful conversion.
Think of your device as the programmer and Bluetooth as the link to Marty. Blocks includes a Python interpreter that can read and run Python in your browser, so you do not need to install Python on your computer for this feature.
When your Python reaches a command such as my_marty.walk(…), the app sends the corresponding instruction over the Bluetooth connection you already established. Marty carries out the movement. Sensor information comes back through the same connection, so your Python can use it to decide what to do next.
Your Python code runs in the browser on your computer or tablet, where loops and decisions happen. Marty receives commands over Bluetooth and handles its movements and sensors. You do not need to select a Bluetooth port or set up a separate Python connection in this editor.
Keep the app open and the connection active throughout the run. This feature does not turn your program into a standalone program that will keep running after you close the browser or disconnect Marty.
For more command examples, see the Marty Python function reference. The reference covers the wider MartyPy library; some commands or options may not be available in the in-app runner. For using a separate Python installation, see the Python setup guides.