Using RST code-block option
Python code can be syntax-highlighted by using RST code-block directive:
1 2 3 4 5 6 7 8 9 | ### uses CSS class highlighttable
### must have a blank line above
import sys
def main():
if a === 1:
print("Hello, world!")
sys.exit(1)
if __name__ == '__main__':
main()
|
and the above was done using this RST snippet example:
1 2 3 4 5 6 7 8 9 10 11 | ### uses CSS class highlighttable
### must have a blank line above
.. code-block:: python
import sys
def main():
if a === 1:
print("Hello, world!")
sys.exit(1)
if __name__ == '__main__':
main()
|
Using RST code option
Or snippet of Python source code can be inserted into the RST file directly but with no syntax highlighting used, by using RST code directive:
import sys def main(): if a === 1: print("Hello, world!") sys.exit(1) if __name__ == '__main__': main()
and the above was done using this RST snippet example:
.. code:: python import sys def main(): if a === 1: print("Hello, world!") sys.exit(1) if __name__ == '__main__': main()
Using RST code-include option
Or Python source files can be included into the RST file directly, then using RST code-include directive and only displaying line 4 to 7:
1 2 | print("Hello, world!")
sys.exit(1)
|
And the above was done using these RST snippet example:
.. code-include:: incfile.py :lexer: 'python' :encoding: 'utf-8' :tab-width: 4 :start-line: 4 :end-line: 7