# How does the Kernel know the $Context in a localized notebook SetOptions[EvaluationNotebook[], CellContext -> Notebook]$Context

"Notebook$$70398424"  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.

• When did you start tracking the packets? Did you capture the traffic when you set the Notebook context to unique? Jul 13 '16 at 12:56
• @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.
– Kuba
Jul 13 '16 at 13:02
• @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, ...]. Jul 13 '16 at 13:07
• @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. – Kuba Jul 13 '16 at 13:15 • The function @ihojnicki is talking about seems to be: FrontEndPrivateSwitchCellContext, as you can run FrontEndPrivateSwitchCellContextEvaluate["asd", ToExpression@"zzz"] to run stuff in that new context. This seems to be like the case of mathematica.stackexchange.com/questions/108850/… Dec 27 '17 at 19:42 ## 1 Answer 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[FrontEndPrivateSwitchCellContext["Global", "Main"]] FE <- K: Return["Notebook$$18756624"] FE -> K: Evaluate[ToExpression["FESetEvaluationNotebookFileName[None];"]] FE -> K: EnterExpression[MakeExpression[BoxData["Context"], StandardForm]] FE <- K: ReturnExpression[BoxData["\"Global\"", StandardForm]] Context  "Notebook$$18$756624"


FE -> K: Evaluate[FrontEndPrivateSwitchCellContext["Notebook$$18756624", "Main"]] FE <- K: Return["Global"] FE -> K: Evaluate[ToExpression["FESetEvaluationNotebookFileName[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.