CellContext

CellContext

is an option for Cell which specifies the context to use for the evaluation of the contents of the cell.

Details

  • The following settings can be given:
  • "context`"explicit context
    Notebookunique to this notebook
    CellGroupunique to this cell group
    Cellunique to this cell
  • The default setting is CellContext->"Global`".
  • With CellContext->Cell, a unique default context is used for the current cell. This context is always different from the default contexts of all other cells, but stays the same for all evaluations done in the current cell.
  • With CellContext->CellGroup, a unique default context is used that is shared between all cells in the nearest enclosing group that includes the current cell and any output generated from it.
  • With CellContext->Notebook, a unique default context is used that is shared between all cells in the current notebook.
  • CellContext effectively allows sequences of evaluations to be isolated from each other within a single Wolfram System session.
  • By changing the default context using CellContext, values of session variables such as In, Out, and $Line are effectively localized.
  • CellContext affects both TemplateBox[{shift, return}, Key1, BaseStyle -> {Notes, FontWeight -> Plain, FontFamily -> Source Sans Pro}] evaluations and evaluations triggered by Dynamic and related constructs.
  • The setting for CellContext determines the default initial values for $Context and $ContextPath to be used when a cell is evaluated.
  • The values of $Context and $ContextPath are taken to be local to the context defined by CellContext. Any modifications made to them will affect all cells with the same default context.
  • Packages must typically be loaded separately in each default context generated by CellContext. The packages will not normally be visible across different default contexts.
  • CellContext is often set at the level of stylesheets or complete notebooks rather than individual cells.
  • Typical context names generated by CellContext->Cell are of the form Cell$$n.
  • When the kernel sends low-level boxes to the front end with symbols in the CellContext-specified context, the front end qualifies those symbols with $CellContext` in its representation of those boxes. When those boxes are sent back to the kernel, the front end substitutes the current CellContext setting for $CellContext`.
  • Example groups in documentation pages have CellContextCellGroup by default.

Examples

open allclose all

Basic Examples  (3)

Create a cell with a custom CellContext:

Examples in this documentation use a unique CellContext for each example group:

Assign a value:

This example is in a different group from the above, and has a different context:

The assigned value from the previous group was to a different symbol in a different context:

Scope  (5)

CellContext affects how all unqualified names are resolved:

CellContext does not affect fully qualified names or names already on $ContextPath:

CellContext affects the default values of $Context and $ContextPath:

Dynamic evaluations are affected by CellContext:

Functions that change the context still work as usual:

Set the context specific to the notebook:

Set the context specific to a cell group:

Set the context specific to the individual cell:

Properties & Relations  (2)

DynamicBox and related low-level constructs use $CellContext` to represent CellContext:

Other boxes that represent expressions directly also use $CellContext`:

Pasting identical copies of boxes using $CellContext` uses the locally specified context:

Possible Issues  (1)

CellContext can be set on regular top-level cells:

Settings on inline cells will be ignored in favor of the top-level setting:

Wolfram Research (2007), CellContext, Wolfram Language function, https://reference.wolfram.com/language/ref/CellContext.html (updated 2023).

Text

Wolfram Research (2007), CellContext, Wolfram Language function, https://reference.wolfram.com/language/ref/CellContext.html (updated 2023).

CMS

Wolfram Language. 2007. "CellContext." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2023. https://reference.wolfram.com/language/ref/CellContext.html.

APA

Wolfram Language. (2007). CellContext. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/CellContext.html

BibTeX

@misc{reference.wolfram_2024_cellcontext, author="Wolfram Research", title="{CellContext}", year="2023", howpublished="\url{https://reference.wolfram.com/language/ref/CellContext.html}", note=[Accessed: 14-July-2024 ]}

BibLaTeX

@online{reference.wolfram_2024_cellcontext, organization={Wolfram Research}, title={CellContext}, year={2023}, url={https://reference.wolfram.com/language/ref/CellContext.html}, note=[Accessed: 14-July-2024 ]}