Re: Preserve notebook's $ContextPath between sessions /
- To: mathgroup at smc.vnet.net
- Subject: [mg104998] Re: [mg104967] Preserve notebook's $ContextPath between sessions /
- From: Leo Alekseyev <dnquark at gmail.com>
- Date: Tue, 17 Nov 2009 05:14:39 -0500 (EST)
- References: <4e3ad400911161117o4867d19bi39f5a63a09ad3f4c@mail.gmail.com>
> Using the "Other..." context mechanism precisely solves the problem you a sked > about in your original email (i.e., close a notebook, re-open it...presum ably in > the same session...and get $ContextPath preserved). Your response adds several Ah, I didn't test this in the same session; you are right. But this is not what I was looking for. If I already have a session running, chances are I can just copy/paste the relevant cells from some notebook that's already open. > 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. Well, that's not necessarily true -- the current behavior is probably optimal for the average user and I don't advocate changing the defaults, just providing an option for customizing them. > 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. That is one solution, and I am already using it in places. That still requires me to copy/paste the initialization cells when I create a new notebook. You are right in that everything I want to do can be done one way or another; I was just hoping to be able to wrap it in a concise utility function (but it doesn't seem possible for reasons described in previous messages). --Leo > 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 wolfram.com> 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 th= at >>> 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 >>> side...you'll have to make sure you pick a unique name. But that's >>> probably not >>> so difficult...you 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 wolfram.com >>> 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 > > > >