For your examples to work you have to make them valid python code.
This is the base of a python script of module. The structure follows a leading docstring that must follow reStructuredText syntax and a header. Then the executable code of your script.
For a quick reference have a look at the example Sphinx-Gallery introduction
In this 2 block structure, the website is rendered ordered with the docstring, then output images and output code. Finally the generating code script.
Notebook Styled examples syntax¶
It is also possible to have more complicated examples, for which you want to embed more documenting strings within your code and separating it by blocks. This is also possible, you follow standard python coding syntax but new documenting strings that will be rendered as reStructuredText have to be leaded by a line of hashes.
In this multiblock structure, the website is rendered ordered by the leading docstring. Then executable python code is presented and after its output in images and test, following the intuitive notebook structure.