Skip to main content

Render reproducible examples of Python code for sharing.

Project description

reprexlite: Python reproducible examples for sharing

PyPI Supported Python versions tests codecov

reprexlite is a tool for rendering reproducible examples of Python code for sharing. With a convenient CLI and lightweight dependencies, you can quickly get it up and running in any virtual environment. It has an optional IPython extension with cell magic for easy use in Jupyter or VS Code. This project is inspired by R's reprex.

  • 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:
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)]

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 to reprexpy and is similarly meant as a port of the R package reprex.

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?"

Installation

reprexlite is available on PyPI:

pip install reprexlite

Optional dependencies can be specified using the "extras" mechanism, e.g., reprexlite[black]. Available extras are:

  • black : for optionally autoformatting your code
  • ipython : to use the %%reprex cell magic in IPython/Jupyter
  • pygments : for syntax highlighting and the RTF venue

Development version

The development version of reprexlite is available on GitHub:

pip install https://github.com/jayqi/reprexlite.git#egg=reprexlite

Quick usage

Command-line interface

The 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:

reprex

This 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 be vim. You can set your default editor using the $EDITOR environment variable—I'm personally a fan of nano.

Once you're done, reprexlite will print out your reprex to console.

To see available options, use the --help flag.

Python library

The same functionality as the CLI is also available from the reprex function with an equivalent API. Simply pass a string with your code, and it will print out the reprex, as well as return a Reprex object that contains all the data and formatting machinery. See the API documentation for more details.

from reprexlite import reprex

code = """
from itertools import product

grid = list(product([1, 2, 3], [8, 16]))
grid
list(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 Magic

reprexlite has an optional IPython extension with a %%reprex cell magic. That means you can easily create a reprex in an IPython shell (requires IPython), in Jupyter (requires Jupyter), or in VS Code's Interactive Python window (requires ipykernel). 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_ext reprexlite

and then put %%reprex at the top of a cell you want to create a reprex for:

%%reprex
from itertools import product

grid = list(product([1, 2, 3], [8, 16]))
grid
list(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 the documentation for more details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

reprexlite-0.5.0.tar.gz (17.8 kB view details)

Uploaded Source

Built Distribution

reprexlite-0.5.0-py3-none-any.whl (16.5 kB view details)

Uploaded Python 3

File details

Details for the file reprexlite-0.5.0.tar.gz.

File metadata

  • Download URL: reprexlite-0.5.0.tar.gz
  • Upload date:
  • Size: 17.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.12

File hashes

Hashes for reprexlite-0.5.0.tar.gz
Algorithm Hash digest
SHA256 3e8968f23619df1289c0d66bc200c0c79e577d199caf6205645cfc9dc4749de2
MD5 6dbb60ed44a13c53f7d85a26a7ff0c32
BLAKE2b-256 a69dfcdbec40e228c2ba6fdf065e12f070e5130b8f0fa7456f5af69260dc0687

See more details on using hashes here.

File details

Details for the file reprexlite-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: reprexlite-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 16.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.12

File hashes

Hashes for reprexlite-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 eaccd0a518f11df50d083a1088ba0dbf05563f7c26111e01e4ff5f3ecea78853
MD5 c9762ec8af141cffaa12a3298a2b5b55
BLAKE2b-256 ca392a9864c6d2005ca4679da6c599cd22cf041153834d8564f89d881034dde3

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page