MathGroup Archive 2011

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

Search the Archive

Re: Context Woes (questions!) in application design...

  • To: mathgroup at smc.vnet.net
  • Subject: [mg115749] Re: Context Woes (questions!) in application design...
  • From: Albert Retey <awnl at gmx-topmail.de>
  • Date: Thu, 20 Jan 2011 06:26:25 -0500 (EST)
  • References: <ih6ec5$3ek$1@smc.vnet.net>

Hi,
> 
> Questions:
> 
> A) Is there a simpler way?
> B) Is there a solution to programmatically specifying $ContextPath on a
> created notebook (InitializationCell doesn't seem to do it because as best I
> can tell it doesn't autorun)?
> C) How can what is apparently a global variable ($ContextPath) have two
> different values? (As best I can tell, I'm only running 1 instance of M on
> my machine).

I don't know what the recommended or most simple way to handle this
would be. I'm also not sure whether I completely understand what your
problem is, after all I don't see whether or why it is necessary to
create or change Global` symbols from within your package code. Still, I
have been facing the same problem I think you do describe: You want to
use CellContext -> Notebook for the generated notebooks but still need
access to other packages within them.

What has worked for me is to set CellProlog so that calls Needs for the
packages you need (or changes $ContextPath accordingly, if you prefer).
E.g.:

CreateDocument[
  {ExpressionCell[Defer[$ContextPath], "Input"]},
  CellContext -> Notebook,
  CellProlog :> (Needs /@ {"MyPackage1`","MyPackage2`"})
]

if you evaluate the input cell of the generated notebook, you will find
that $ContextPath is set as desired. On the other hand, that will only
affect shift-return evaluations within the newly created notebook, not
anything that is run by buttons or dynamics. But for these actions, you
might want to do the Needs explicitly. Remember that a call to Needs is
cheap once the package is loaded: then only $ContextPath will be
changed, but the package file is not read again.

The following is ugly, but it shows some of the subtleties, e.g. that
the dynamics see different settings of $ContextPath, depending on
whether something has been evaluated or pressed in he new notebook. It
is probably something you want to play with:

CreateDocument[{
  ExpressionCell[Defer[$ContextPath], "Input"],
  Dynamic[{DateString[], $ContextPath}, UpdateInterval -> 1]
  }, CellContext -> Notebook, CellProlog :> Needs["Developer`"],
 DockedCells -> {Cell[
    BoxData@ToBoxes@
      Dynamic[{DateString[], $ContextPath}, UpdateInterval -> 1],
    "DockedCell"],
   Cell[BoxData@ToBoxes@Row[{
        Button["test1",
         MessageDialog[$ContextPath]],
        Button["test2",
         Needs["Developer`"];
         MessageDialog[$ContextPath], Method -> "Queued"]
        }], "DockedCell"]
   }]


hth,

albert


  • Prev by Date: Re: SetOptions does not work with Grid
  • Next by Date: Re: Array of arrays of various sizes and compile
  • Previous by thread: Context Woes (questions!) in application design...
  • Next by thread: How to edit/change Wolfram examples?