7
$\begingroup$
SetOptions[EvaluationNotebook[], CellContext -> Notebook]

$Context
"Notebook$$70$398424`"

And from different notebook it will be

"Global`"

I suppose the kernel can match a proper context to a given NotebookObject but how it is done if such information isn't sent to the Kernel?

Here is what the LinkSnooper says:

FE ---> K: EnterExpressionPacket[MakeExpression[BoxData["$Context"], StandardForm]]

FE <--- K: OutputNamePacket["Out[1]= "]

FE <--- K: ReturnExpressionPacket[BoxData["\"Notebook$$70$398424`\"", StandardForm]]

So the Kernel knows what to answer even though it was just asked about the $Context.

Installing LinkSnooper on Windows

$\endgroup$
  • 1
    $\begingroup$ When did you start tracking the packets? Did you capture the traffic when you set the Notebook context to unique? $\endgroup$ – rcollyer Jul 13 '16 at 12:56
  • $\begingroup$ @rcollyer I assumed it does not matter because traffic from all notebooks is going though the same link, and there is no info in those Packets about where they come from. So K and FE are talking behind our backs or LinkSnooper does not show everything. Or I missed something basic. $\endgroup$ – Kuba Jul 13 '16 at 13:02
  • 1
    $\begingroup$ @Kuba there is a utility function that gets called whenever the FrontEnd deems that the $Context needs to be updated. It will not appear in the output as Set[$Context, ...]. $\endgroup$ – ihojnicki Jul 13 '16 at 13:07
  • $\begingroup$ @ihojnicki and rcollyer ah it is there, I missed that because I was playing with Snooper for selected notebooks while I should have set $FrontEndSession's Evaluator. Unless someone is willing to authoritatively answer I will do that later. $\endgroup$ – Kuba Jul 13 '16 at 13:15
  • $\begingroup$ The function @ihojnicki is talking about seems to be: FrontEnd`Private`SwitchCellContext, as you can run FrontEnd`Private`SwitchCellContextEvaluate["asd`", ToExpression@"zzz"] to run stuff in that new context. This seems to be like the case of mathematica.stackexchange.com/questions/108850/… $\endgroup$ – b3m2a1 Dec 27 '17 at 19:42
2
$\begingroup$

You can see what is going on by setting $FrontEndSession Evaluator->"LinkSnooper".

  • Create a notebook (A) with two cells and evaluate them:

    SetOptions[$FrontEndSession, Evaluator -> "LinkSnooper"]
    

    $Context
    
  • Create another (B) one with:

    SetOptions[EvaluationNotebook[], CellContext -> Notebook]
    

    $Context
    

Let's clear LinkSnooper's console and check what happens when we evaluate $Context in (A) and (B). Here is an output (reduced to a relevant part, also FE/Kernel communication expressions names have "*Paclet" pattern (e.g. EvaluatePaclet), I dropped Paclet for a readability)

 $Context
"Global`"

FE -> K: Evaluate[FrontEnd`Private`SwitchCellContext["Global`", "Main"]]

FE <- K: Return["Notebook$$18$756624`"]

FE -> K: Evaluate[ToExpression["FE`SetEvaluationNotebookFileName[None];"]]

FE -> K: EnterExpression[MakeExpression[BoxData["$Context"], StandardForm]]

FE <- K: ReturnExpression[BoxData["\"Global`\"", StandardForm]]

$Context
"Notebook$$18$756624`"

FE -> K: Evaluate[FrontEnd`Private`SwitchCellContext["Notebook$$18$756624`", "Main"]]

FE <- K: Return["Global`"]

FE -> K: Evaluate[ToExpression["FE`SetEvaluationNotebookFileName[None];"]]

FE -> K: EnterExpression[MakeExpression[BoxData["$Context"], StandardForm]]

FE <- K: ReturnExpression[BoxData["\"Notebook$$18$756624`\"", StandardForm]]

Repeating last evaluation shows no sign of SwitchCellContext, which probably means that the FrontEnd knows from where the last input was read and in case of change it asks the Kernel to SwitchCellContext[ to-notebookCellContex, on-kernel].

Which is just a quck guess from a short test. Any insight is appreciated, edit or answer if you want to add someyhing.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.