Show More
@@ -7750,7 +7750,21 b' This is obviously a brute force way of avoiding race conditions with the' | |||||
7750 | Interactive demos with IPython |
|
7750 | Interactive demos with IPython | |
7751 | \layout Standard |
|
7751 | \layout Standard | |
7752 |
|
7752 | |||
7753 | IPython ships with XXX |
|
7753 | IPython ships with a basic system for running scripts interactively in sections, | |
|
7754 | useful when presenting code to audiences. | |||
|
7755 | A few tags embedded in comments (so that the script remains valid Python | |||
|
7756 | code) divide a file into separate blocks, and the demo can be run one block | |||
|
7757 | at a time, with IPython printing (with syntax highlighting) the block before | |||
|
7758 | executing it, and returning to the interactive prompt after each block. | |||
|
7759 | The interactive namespace is updated after each block is run with the contents | |||
|
7760 | of the demo's namespace. | |||
|
7761 | \layout Standard | |||
|
7762 | ||||
|
7763 | This allows you to show a piece of code, run it and then execute interactively | |||
|
7764 | commands based on the variables just created. | |||
|
7765 | Once you want to continue, you simply execute the next block of the demo. | |||
|
7766 | The following listing shows the markup necessary for dividing a script | |||
|
7767 | into sections for execution as a demo. | |||
7754 | \layout Standard |
|
7768 | \layout Standard | |
7755 |
|
7769 | |||
7756 |
|
7770 | |||
@@ -7764,6 +7778,84 b' codelist{examples/example-demo.py}' | |||||
7764 | \end_inset |
|
7778 | \end_inset | |
7765 |
|
7779 | |||
7766 |
|
7780 | |||
|
7781 | \layout Standard | |||
|
7782 | ||||
|
7783 | In order to run a file as a demo, you must first make a | |||
|
7784 | \family typewriter | |||
|
7785 | Demo | |||
|
7786 | \family default | |||
|
7787 | object out of it. | |||
|
7788 | If the file is named | |||
|
7789 | \family typewriter | |||
|
7790 | myscript.py | |||
|
7791 | \family default | |||
|
7792 | , the following code will make a demo: | |||
|
7793 | \layout LyX-Code | |||
|
7794 | ||||
|
7795 | from IPython.demo import Demo | |||
|
7796 | \layout LyX-Code | |||
|
7797 | ||||
|
7798 | mydemo = Demo('myscript.py') | |||
|
7799 | \layout Standard | |||
|
7800 | ||||
|
7801 | This creates the | |||
|
7802 | \family typewriter | |||
|
7803 | mydemo | |||
|
7804 | \family default | |||
|
7805 | object, whose blocks you run one at a time by simply calling the object | |||
|
7806 | with no arguments. | |||
|
7807 | If you have autocall active in IPython (the default), all you need to do | |||
|
7808 | is type | |||
|
7809 | \layout LyX-Code | |||
|
7810 | ||||
|
7811 | mydemo | |||
|
7812 | \layout Standard | |||
|
7813 | ||||
|
7814 | and IPython will call it, executing each block. | |||
|
7815 | Demo objects can be restarted, you can move forward or back skipping blocks, | |||
|
7816 | re-execute the last block, etc. | |||
|
7817 | Simply use the Tab key on a demo object to see its methods, and call | |||
|
7818 | \family typewriter | |||
|
7819 | `?' | |||
|
7820 | \family default | |||
|
7821 | on them to see their docstrings for more usage details. | |||
|
7822 | In addition, the | |||
|
7823 | \family typewriter | |||
|
7824 | demo | |||
|
7825 | \family default | |||
|
7826 | module itself contains a comprehensive docstring, which you can access | |||
|
7827 | via | |||
|
7828 | \layout LyX-Code | |||
|
7829 | ||||
|
7830 | from IPython import demo | |||
|
7831 | \layout LyX-Code | |||
|
7832 | ||||
|
7833 | demo? | |||
|
7834 | \layout Standard | |||
|
7835 | ||||
|
7836 | ||||
|
7837 | \series bold | |||
|
7838 | Limitations: | |||
|
7839 | \series default | |||
|
7840 | It is important to note that these demos are limited to fairly simple uses. | |||
|
7841 | In particular, you can | |||
|
7842 | \emph on | |||
|
7843 | not | |||
|
7844 | \emph default | |||
|
7845 | put division marks in indented code (loops, if statements, function definitions | |||
|
7846 | , etc.) Supporting something like this would basically require tracking the | |||
|
7847 | internal execution state of the Python interpreter, so only top-level divisions | |||
|
7848 | are allowed. | |||
|
7849 | If you want to be able to open an IPython instance at an arbitrary point | |||
|
7850 | in a program, you can use IPython's embedding facilities, described in | |||
|
7851 | detail in Sec\SpecialChar \@. | |||
|
7852 | \SpecialChar ~ | |||
|
7853 | ||||
|
7854 | \begin_inset LatexCommand \ref{sec:embed} | |||
|
7855 | ||||
|
7856 | \end_inset | |||
|
7857 | ||||
|
7858 | . | |||
7767 | \layout Section |
|
7859 | \layout Section | |
7768 |
|
7860 | |||
7769 |
|
7861 |
General Comments 0
You need to be logged in to leave comments.
Login now