IPython/Jupyter Magic¶
Reprex-rendering is also available in IPython, Jupyter, and VS Code through an IPython cell magic. This functionality requires IPython to be installed at a minimum. (You can install both reprexlite and IPython together with reprexlite[ipython]
.)
To use, first load the extension:
%load_ext reprexlite
and then simply use the %%reprex
magic with a cell containing the code you want a reprex of.
%%reprex
from itertools import product
grid = list(product([1, 2, 3], [8, 16]))
grid
list(zip(*grid))
```python from itertools import product grid = list(product([1, 2, 3], [8, 16])) grid #> [(1, 8), (1, 16), (2, 8), (2, 16), (3, 8), (3, 16)] list(zip(*grid)) #> [(1, 1, 2, 2, 3, 3), (8, 16, 8, 16, 8, 16)] ``` <sup>Created at 2021-02-27 16:08:34 PST by [reprexlite](https://github.com/jayqi/reprexlite) v0.3.1</sup>
That's it! The cell magic shares the same interface and command-line options as the CLI.
%%reprex -v slack
x = 2
x + 2
``` x = 2 x + 2 #> 4 ```
Print Help Documentation¶
You can use the %reprex
line magic (single-%
) to print out documentation.
%reprex
reprexlite v0.3.1 IPython Magic Cell Magic Usage: %%reprex [OPTIONS] Render reproducible examples of Python code for sharing. Your code will be executed and the results will be embedded as comments below their associated lines. Additional markup will be added that is appropriate to the choice of venue option. For example, for the default `gh` venue for GitHub Flavored Markdown, the final reprex will look like: ---------------------------------------- ```python arr = [1, 2, 3, 4, 5] [x + 1 for x in arr] #> [2, 3, 4, 5, 6] max(arr) - min(arr) #> 4 ``` <sup>Created at 2021-02-27 00:13:55 PST by [reprexlite](https://github.com/jayqi/reprexlite) v0.3.1</sup> ---------------------------------------- The supported venue formats are: - gh : GitHub Flavored Markdown - so : StackOverflow, alias for gh - ds : Discourse, alias for gh - html : HTML - py : Python script - rtf : Rich Text Format - slack : Slack Options: -i, --infile PATH Read code from an input file instead via editor. -o, --outfile PATH Write output to file instead of printing to console. -v, --venue [gh|so|ds|html|py|rtf|slack] Output format appropriate to the venue where you plan to share this code. [default: gh] --advertise / --no-advertise Whether to include footer that credits reprexlite. If unspecified, will depend on specified venue's default. --session-info Whether to include details about session and installed packages. --style Autoformat code with black. Requires black to be installed. --comment TEXT Comment prefix to use for results returned by expressions. [default: #>] --old-results Keep old results, i.e., lines that match the prefix specified by the --comment option. If not using this option, then such lines are removed, meaning that an input that is a reprex will be effectively regenerated.
VS Code Interactive Python Windows¶
If you're in VS Code and ipykernel
is installed, you similarly use the %%reprex
cell magic with Python Interactive windows. For a file set to Python language mode, use # %%
to mark an IPython cell that can then be run. Or you can open the Interactive window on its own via "Jupyter: Create Interactive Window" through the Command Palette. See VS Code docs for more info.
Isolated Namespace¶
Note that—just like other ways of rendering a reprex—your code is evaluated in an isolated namespace that is separate from the namespace of your IPython session or your notebook. That means, for example, variables defined in your notebook won't exist in your reprex.
notebook_var = 2
%%reprex --no-advertise
notebook_var
```python notebook_var #> Traceback (most recent call last): #> File "/Users/jqi/repos/reprexlite/reprexlite/code.py", line 69, in evaluate #> result = eval(str(self).strip(), scope, scope) #> File "<string>", line 1, in <module> #> NameError: name 'notebook_var' is not defined ```