Package: jupyter-tempvars
jupyter-tempvars: Convenient temporary variable management in Jupyter NotebookMost Recent Stable Release:Info:Do you work in Jupyter Notebook?Are your notebooks flaky sometimes, due to leftover/temporary variables?jupyter-tempvarscan help!Namespace pollution with leftover/temporary variables is a
common challenge of using Jupyter notebooks. It's a downside
of the power provided by the shared global notebook namespace,
for things like exploratory data analysis.And, it can be a pretty big downside. It's quite annoying
to work for hours to try to figure out what's wrong with a notebook,
only to have it suddenly work properly when you restart Jupyter.
It's also quite annoying to have a workbook that youthoughtwas working correctly, suddelystopworking once you restart.
It can cause even bigger problems if you pass a notebook
on to someone else, and then it doesn't work right for them even
though it was working fine for you.jupyter-tempvarsis a Jupyter nbextension built on thetempvarsPython packagethat helps minimize these kinds of problems. Simply decorate code
cells with metadata tags matching a defined template, and then variables
that fit the rules you define will be automatically treated
as temporary variables. This means that:Matching variables will be removed from the global namespace before
each tagged cell is executed, ensuring that cell isn't contaminated
by "dragged-in" variables,andMatching variables will be removed from the global namespace after the cell has
finished executing, ensuring that other code cells aren't contaminated by
this cell, either.Asjupyter-tempvarsis currently configured, any variables removed
from the global namespace before execution of the cell arerestoredto the global namespace after execution is finished.PrerequisitesPythonI've been developingjupyter-tempvarsusing Python 3.9, but I believe any
version of Python 3 that works with the underlyingtempvarslibrary
(which should be all actively maintained versions, 3.7+) should work fine.
It should work on any platform supported by Jupyter.
(At some point soon here, I'll be figuring out a test suite to actually
check across platforms and Pythons....)Jupyterjupyter-tempvarsrequires a full instance ofJupyter Notebook,
including the Javascript frontend. So far, I've only tested it with vanilla Jupyter,
but in theory I think it should work with JupyterHub, Jupyter in Anaconda, etc.
I would be grateful for feedback from anyone who tries to use it in
other contexts. I believe it should work withnotebookversions 4.x and above.
Note thatjupyter-tempvarswillNOTwork with JupyterLab!To get a basic Jupyter install justpip install jupyter.jupyter-contrib-nbextensionsThe community-developedjupyter-contrib-nbextensionspackageis not strictly required in order to usejupyter-tempvars, but it's highly recommended.
If nothing else, the configurator plugin that adds aNbextensionstab to the
main Jupyter interface is really handy. See the install instructionshere.InstallationThe first step is to install the overalljupyter-tempvarspackage.
It's on PyPI, so just:$ pip install jupyter-tempvarsFrom there, thejupyter_tempvarsextension needs to be installed into the
Jupyter environment itself. Thejuypter-tempvarsproject ships with a
helper script to simplify the most common case, where the extension is
installed into the user-scope extension registry:$ jupyter-tempvars installNow that the extension is installed, it needs to be activated,
either in theNbextensionsJupyter tab of the configurator:or by running the helper script with theenablecommand:$ jupyter-tempvars enableAgain, note that thejupyter-tempvarshelper script is set up to install the extension
to the user's Jupyter extension repository (--userflag tojupyter nbextension).
If you wish to install to a different location, you'll need to invoke the necessaryjupyter nbextensioncommands manually.UpgradingUpgrading the extension after it's been installed also has two steps:$ pip install --upgrade jupyter-tempvars tempvars
$ jupyter-tempvars installIt's important to also upgradetempvarswhenever you upgradejupyter-tempvars, in case the extension needs features
from a newer version oftempvars.If you pinjupyter-tempvarsto a specific version usingpip freeze,
or with a tool likepoetryorpipenv, you should make sure
that the pins for bothtempvarsandjupyter-tempvarsget updated.UsageAs of v0.1,jupyter-tempvarssupports two filters for identifying
temporary variables, a 'starts with' filter and an 'ends with' filter.
Both methods use the built-in Jupyter capability to attach metadata to
individual notebook cells.The first step to usejupyter-tempvarsis to display theTagsmetadata cell headers, if they're not already visible:Open an.ipynbnotebook.In the toolbar, clickView>Cell Toolbar>TagsThen, for each cell you wantjupyter-tempvarsto handle temporary
variables, add a tag to that cell with one of the following two formats:To treat all variables starting with a given prefix as temporary,
usetempvars-start-{prefix}To treat all variables ending with a given suffix as temporary,
usetempvars-end-{suffix}So, for example, to treat all variables in a cell that start witht_as temporary variables, tag the cell withtempvars-start-t_:Similarly, to treat all variables that end with_tempas temporary,
tag the cell withtempvars-end-_temp:Thetempvars-tags can be used multiple times, and in combination with each other:Temporary variables are also cleared from the namespace before executing the tagged cell:The traceback in the above example also reveals howjupyter-tempvarsworks under the hood: the cell's code is enclosed with atempvars.TempVarscontext manager (imported to the notebook namespace as_TempVars)
before submission to the kernel for execution. One side effect
of this implementation is that the result of the last line of the code is not
echoed to output for tagged cells, and must be explicitlyprint-ed:As noted above,jupyter-tempvarscurrently restores pre-existing temporary variables,
cleared before the start of tagged cell execution,backto the global namespace
after execution has completed. If it would be valuable to allow the option ofnotrestoring the pre-existing variables after execution of a tagged cell,
please commenthere.
More generally, bug reports and feature requests of any kind for bothjupyter-tempvarsand the underlyingtempvarslibrary are always welcome.Advanced Variable Management viatempvarsjupyter-tempvarsonly exposes a subset of the functionality
provided by the underlyingtempvarsPython package. If you need
a more powerful temporary variable
management tool, take a look at thefull capabilitiesoftempvars.jupyter-tempvarsalso requires the full Jupyter notebook frontend to function.
If you want to manage temporary variables when using a tool that
interacts with notebooks outside the context of the Jupyter frontend (e.g.,nbclientornbmake),
you should consider usingtempvarsdirectly in your code,
instead ofjupyter-tempvars.Available onPyPI.Source onGitHub.
Bug reports and feature requests are welcomed at theIssuespage there.Copyright (c) Brian Skinn 2022License: The MIT License. SeeLICENSE.txtfor full license terms.