reprexpy package

Submodules

reprexpy.reprex module

class reprexpy.reprex.ExecutePreprocessorStoreHist(*args: t.Any, **kwargs: t.Any)[source]

Bases: ExecutePreprocessor

async_execute_cell(cell, cell_index, execution_count, store_history)[source]

Executes a single code cell.

To execute all cells see execute().

Parameters:
  • cell (nbformat.NotebookNode) – The cell which is currently being processed.

  • cell_index (int) – The position of the cell within the notebook object.

  • execution_count (int) – The execution count to be assigned to the cell (default: Use kernel response)

  • store_history (bool) – Determines if history should be stored in the kernel (default: False). Specific to ipython kernels, which can store command histories.

Returns:

output – The execution output payload (or None for no output).

Return type:

dict

Raises:

CellExecutionError – If execution failed and should raise an exception, this will be raised with defaults about the failure.

Returns:

cell – The cell which was just processed.

Return type:

NotebookNode

reprexpy.reprex.reprex(code=None, code_file=None, venue='gh', kernel_name=None, comment='#>', si=False, advertise=False)[source]

Render a reproducible example of Python code (a reprex).

Runs Python code inside a fresh IPython session, captures the results, and marks everything up using the appropriate markdown syntax (determined by venue). The code for your reprex can come from one of three places:

  1. The clipboard (the default). Code for the reprex will be taken from the clipboard if you leave code=None and code_file=None.

  2. A string. Use the code parameter to pass in a string of code.

  3. A file. Use the code_file parameter to specify a path to a file containing reprex code.

Parameters:
  • code (str, optional) – The code that makes up your reprex (e.g., 'x = "hi there"\nprint(x)').

  • code_file (str, optional) – Path to a file that contains your reprex.

  • venue ({'gh', 'so', 'sx'}, optional) – The venue that your reprex is bound for. Choose ‘gh’ if your reprex will be posted to GitHub, ‘so’ if it’s bound for Stack Overflow, or ‘sx’ if you will be inserting it into a docstring.

  • kernel_name (str, optional) – The name of the IPython kernel that you want to use to execute your reprex. Choosing kernel_name=None (the default) means you want to use the default kernel. See the IPython docs kernels for different environments for details on how to create/use a custom kernel.

  • comment (str, optional) – String that should be used to comment out your code’s outputs. This parameter is ignored if venue='sx'.

  • si (bool, optional) – Do you want to display your IPython kernel’s session info at the end of the reprex? See reprexpy.session_info.SessionInfo for details on session info. This parameter is ignored if venue='sx'.

  • advertise (bool, optional) – Do you want to include a note at the bottom of your reprex that says that it was produced by the reprexpy package? This parameter is ignored if venue='sx'.

Returns:

A string containing your rendered reprex. reprex() also tries to copy the rendered reprex to the clipboard.

Return type:

str

Examples

Render a simple reprex for GitHub:

>>> import reprexpy
>>> code = 'x = "hi there"\ny = " old friend"\nprint(x + y)'
>>> print(reprexpy.reprex(code))
```python
x = "hi there"
y = " old friend"
print(x + y)
#> hi there old friend
```

Render same reprex, except pull the code from a file and use Stack Overflow markdown instead of GitHub markdown (hence the leading spaces in the rendered result):

>>> import reprexpy
>>> file_path = reprexpy.reprex_ex('basic-example.py')
>>> print(reprexpy.reprex(code_file=file_path, venue='so'))
# <!-- language-all: lang-py -->
    x = "hi there"
    y = " old friend"
    print(x + y)
    #> hi there old friend
reprexpy.reprex.reprex_ex(file)[source]

Get the path to an example reprex file

Parameters:

file ({'basic-example.py', 'error.py', 'plotting.py'}) – Name of the file whose path you want.

Returns:

A path to an example reprex file.

Return type:

str

reprexpy.session_info module

class reprexpy.session_info.SessionInfo[source]

Bases: object

Class responsible for gathering IPython session information.

A SessionInfo object provides basic information about a user’s environment so that they may easily communicate their environment to others (e.g., when posting a question on Stack Overflow). For example, it provides info on what Python version you are using, as well as the version numbers of packages that you have imported into your IPython session. You must be using the IPython kernel to instantiate this class.

session_info

The Python version IPython is using, the OS it’s running on, and today’s date.

Type:

dict

pkg_info

The packages that the user has imported into their IPython environment, excluding those packages that are part of the Python standard library (e.g., re or os). The names/keys of pkg_info refer to the modules that SessionInfo() has found in your environment (roughly speaking). The values in pkg_info are tuples that provide the name of the distribution that the module is packaged in (i.e., the name found on PyPI), as well as that distribution’s version number. Printing a SessionInfo() object prints these distribution names/versions in the format that pip expects (e.g., in the requirements.txt format).

Type:

dict

Examples

>>> import asttokens
>>> import nbconvert.utils
>>> import reprexpy
>>> reprexpy.SessionInfo()
Session info --------------------------------------------------------------------
Python: 3.6
Platform: Darwin-17.7.0-x86_64-i386-64bit (64-bit)
Date: 2018-08-27
Packages ------------------------------------------------------------------------
asttokens==1.1.11
nbconvert==5.3.1
reprexpy==0.1.0

Module contents