Skip to main content

%%publish magic

Runs the cell in 1000 Words remote runtime and captures the cell's code, outputs (stdout, stderr and iPython displays), as well as the cell state (all variables necessary to run the cell and any variable created by the cell).

To import it:

from thousandwords import publish

To get help, run %%publish?:

  %cmagic [--public] [--no-variables] [--with-variables] [--not-runnable]

optional arguments:
--public Publish publicly to an unlisted URL. Anyone with the link
can view.
--no-variables Don't include the variables required for execution in the
publication
--with-variables Include the variables required for execution in the
publication
--not-runnable Don't make the publication runnable. If set, the cell is
run locally and only the code and outputs are captured

How it works

%%publish does the following:

  1. A linter detects the cell dependencies, i.e. any variable that's required to execute your cell. Each cell dependency is serialized and uploaded to the web app.
  2. The cell's code is sent to the web app for execution. The default remote runtime comes pre-installed with the key data analytics Python libraries (pandas, matplotlib, numpy and many more).
  3. A URL to the captured cell is printed on stdout.

Reproducibility

The cell is not executed locally, but remotely with in Web app Python runtime. This ensures that anyone with the link can rerun your code and get the same result. It eliminates the risk of having issues due to running different package versions.

tip

For %%publish to work, you need to ensure that:

  1. Your cell dependencies, i.e. the variables required to execute your cell can be serialized. thousandwords uses cloudpickle for serialization.
  2. Your cell only uses packages that are part of the web app runtime. See here for the list of pre-installed packages.

Caveats

caution

Execution of a cell marked with %%publish is stateless. Any state created is captured by the app, but not by the notebook. As an example, consider the following ipython session:

In [1]: from thousandwords import share

In [2]: x = 10

In [3]: %%publish
...: x = 11
Interactive cell available at:
https://1000words-hq.com/c/V-x-vW8D2s2

In [4]: x
Out[4]: 10

x is captured in the URL, but not by the iPython session