Package: reprexlite
reprexlite: Python reproducible examples for sharingreprexliteis a tool for renderingreproducibleexamples of Python code for sharing. With aconvenient CLIand lightweight dependencies, you can quickly get it up and running in any virtual environment. It has an optionalIPython extension with cell magicfor easy use in Jupyter or VS Code. This project is inspired by R'sreprex.Paste or type some Python code that you're interested in sharing.reprexlite will execute that code in an isolated namespace. Any returned values or standard output will be captured and displayed as comments below their associated code.The rendered reprex will be printed for you to share. Its format can be easily copied, pasted, and run as-is by someone else. Here's an example of an outputted reprex:fromitertoolsimportproductgrid=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)]Writing a good reprex takes thought and effort (see"Reprex Do's and Don'ts"for tips). The goal of reprexlite is to be a tool that seamlessly handles the mechanical stuff, so you can devote your full attention to the important, creative work of writing the content.Reprex-style code formatting—namely, with outputs as comments—is also great for documentation. Users can copy and run with no modification. Consider using reprexlite when writing your documentation instead of copying code with>>>prompts from an interactive Python shell. In fact, reprexlite can parse code with>>>prompts and convert it into a reprex for you instead.reprexlite is a lightweight alternative toreprexpyand is similarly meant as a port of the R packagereprex.Why reproducible examples?If you're asking for help or reporting a bug, you are more likely to succeed in getting others to help you if you include a good reprex. If you're writing documentation, your readers will appreciate examples that they can easily run. See"Design Philosophy"for more on both "Why reproducible examples?" and "Why reprexlite?"Installationreprexlite is available on PyPI:pipinstallreprexliteOptional dependencies can be specified using the"extras" mechanism, e.g.,reprexlite[black]. Available extras are:black: for optionally autoformatting your codeipython: to use the%%reprexcell magic in IPython/Jupyterpygments: for syntax highlighting and the RTF venueDevelopment versionThe development version of reprexlite is available on GitHub:pipinstallhttps://github.com/jayqi/reprexlite.git#egg=reprexliteQuick usageCommand-line interfaceThe primary way to use reprexlite is through the CLI. It allows you to create a reprex without entering a Python session. Simply invoke the command:reprexThis will take you into your system's default command-line text editor where you can type or paste your Python code. On macOS, for example, this will bevim. You can set your default editor using the$EDITORenvironment variable—I'm personally a fan ofnano.Once you're done, reprexlite will print out your reprex to console.To see available options, use the--helpflag.Python libraryThe same functionality as the CLI is also available from thereprexfunction with an equivalent API. Simply pass a string with your code, and it will print out the reprex, as well as return aReprexobject that contains all the data and formatting machinery. See theAPI documentationfor more details.fromreprexliteimportreprexcode="""from itertools import productgrid = list(product([1, 2, 3], [8, 16]))gridlist(zip(*grid))"""reprex(code)#> ```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-26 00:32:00 PST by [reprexlite](https://github.com/jayqi/reprexlite) v0.3.0</sup>#> <reprexlite.formatting.GitHubReprex object at 0x109059f10>IPython/Jupyter Cell Magicreprexlite has an optional IPython extension with a%%reprexcell magic. That means you can easily create a reprex in anIPython shell(requires IPython), inJupyter(requires Jupyter), or inVS Code's Interactive Python window(requiresipykernel). This can be handy if you're already working in a Jupyter notebook and want to share some code and output, which otherwise doesn't neatly copy and paste in a nice format.To use, simply load the extension with%%load_extreprexliteand then put%%reprexat the top of a cell you want to create a reprex for:%%reprexfromitertoolsimportproductgrid=list(product([1,2,3],[8,16]))gridlist(zip(*grid))The magic accepts the same inline option flags as the CLI. Use the line magic%reprex(note single%) to print out help. See thedocumentationfor more details.