MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Preserve notebook's $ContextPath between sessions / store custom

  • To: mathgroup at
  • Subject: [mg104996] Re: [mg104967] Preserve notebook's $ContextPath between sessions / store custom
  • From: John Fultz <jfultz at>
  • Date: Tue, 17 Nov 2009 05:14:17 -0500 (EST)
  • Reply-to: jfultz at

Using the "Other..." context mechanism precisely solves the problem you asked 
about in your original email (i.e., close a notebook, re-open it...presumably in 
the same session...and get $ContextPath preserved).  Your response adds several 
other requests which, if implemented, would leave the system considerably more 
customizable, but considerably less general and easy to use.  It would also tie 
our hands in certain ways.  For example, the help viewer uses contextual 
localization (but at the cell group level, rather than the notebook level), and 
I don't think it's generally desirable to have two help viewer windows which
happen to be pointing to the same page (i.e. having loaded the same notebook
file) share a context.

I'm not sure why you couldn't just solve your problems by adding the explicit 
code you want in initialization cells.  They'll evaluate first every single time 
you open (or close, then re-open) the notebook, and it allows you total 
flexibility in configuring things however you want to configure them.

John Fultz
jfultz at
User Interface Group
Wolfram Research, Inc.

On Mon, 16 Nov 2009 14:17:46 -0500, Leo Alekseyev wrote:
> John, thanks for your response.
> Since there can be many session-dependent volatile contexts, it
> probably wouldn't be a good idea to save them all by default -- which
> is probably why that feature isn't there.  On the other hand, it would
> be nice to offer the user a greater control over this: for instance, I
> am simply trying to avoid doing
> SetOptions[EvaluationNotebook[],CellContext->Notebook];
> Needs/@{"foo`","bar`"} (* which have already been autoloaded by
> init.m, but are now hidden *)
> AppendTo[$ContexPath,"Global`"]; (* in some sessions it's good to have
> a "read-only" access to global context *)
> at the start of every notebook that I want to set up like that.
> Specifying a "Other..." context as you suggested doesn't solve this
> problem because the packages and loaded contexts are still hidden.
> I am just surprised there isn't an easy mechanism to save/retrieve the
> context info.
> By poking around I noticed there's a variable
> Global`Private`SavedContextInfo["$ContextPath"].
> However, it looks like the SavedContextInfo variables cannot be made
> persistent -- correct me if I am wrong.
> I tried writing the following wrapper function:
> test2[] := (SetOptions[EvaluationNotebook[],
> CellContext -> Notebook]; (AppendTo[$ContextPath, #1] &) /@
> Global`Private`SavedContextInfo["$ContextPath"]; $ContextPath 
> DeleteDuplicates[$ContextPath])
> However, it appears that only the SetOptions[EvaluationNotebook[],
> CellContext -> Notebook] command takes effect when written like this
> *unless invoked twice in two separate cells with a fully-qualified
> symbol name.  This seems kind of inelegant (and trying to do this
> voodoo programmatically by manipulating notebooks with kernel
> functions is ugly and doesn't work well).  I am still curious if there
> is a good way around this.
> --Leo
> On Mon, Nov 16, 2009 at 10:44 AM, John Fultz <jfultz at> wrote:
>> Yes, that's right.  Mathematica is not just changing the context, but
>> trying to
>> sandbox a number of evaluation side effects from notebooks which are
>> operating
>> in another context, including...
>> * $Context (of course)
>> * $ContextPath
>> * The state of In[]/Out[] numbering
>> * The Out[] history
>> In a notebook set with a unique notebook-level context, no attempt is
>> made to
>> recover the same context when you close and open the notebook again,
>> and the
>> context you used isn't stored anywhere in the file.  I don't think that
>> occurred
>> to anyone here when designing the feature, and I'm not entirely certain
>> whether
>> it would be generally desirable to do so.
>> But, what you want to do is easy enough.  Instead of specifying a
>> notebook-unique context, specify a named context of your choice (i.e.,
>> the
>> "Other..." option).  Everything will be remembered as long as it's
>> using the
>> same context name, and by hard-coding the name, you can guarantee that.
>>  Down
>>'ll have to make sure you pick a unique name.  But that's
>> probably not
>> so could choose a name based upon the file name, for
>> example.
>> You could write a button which does this, too.  I'll leave that as an
>> exercise
>> for the reader.
>> Sincerely,
>> John Fultz
>> jfultz at
>> User Interface Group
>> Wolfram Research, Inc.
>> On Sun, 15 Nov 2009 05:56:32 -0500 (EST), dnquark wrote:
>>> I have fallen into the habit of making the default context of the
>>> notebooks private by issuing the command
>>> SetOptions[EvaluationNotebook[], CellContext->Notebook]
>>> at the start of every notebook session.  However, I typically add
>>> several other contexts to $ContextPath; I even wrote a utility
>>> function to easily do all this automatically.
>>> It turns out, though, that upon closing and reopening the notebook
>>> Mathematica puts only "Notebook$$xx`" and "System`" contexts in
>>> $ContextPath.  My question is -- is there a way to make _my custom_
>>> $ContextPath persistent between sessions in the same way as
>>> {"Notebook$
>>> $xx`", "System`"} context list is persistent?..  Or at least, can I
>>> store custom metadata with the notebook -- i can always write a
>>> wrapper function to retrieve it.
>>> Thanks,
>>> --Leo

  • Prev by Date: Re: Re: Mathematica skill level snippet(s)
  • Next by Date: Re: Re: Re: Wrong limit?
  • Previous by thread: Re: Preserve notebook's $ContextPath between sessions / store custom
  • Next by thread: Solving Ordinary differential equations by NDSolve