Is it possible to dynamically link the content of two cells? For example, can I set up a Notebook with two text cells that display the same value, and if the user types in one the other updates, etc.? I am familiar with Mathematica's dynamic functionality and at least the key concepts of the FrontEnd, and it seems like I should be able to assign a dynamic symbol to certain parts of a cell and just use that in an expression elsewhere, but I can't figure out how to programmatically access a cell's current content. The best I have been able to do is get a CellObject using Cells[]
, e.g. where the first cell is a text cell containing "This is a test!", Cells[][[1]]
gives
Cell[BoxData[
TemplateBox[{6849,"6849","\"Text\""},
"CellObject"]], "Output",
CellChangeTimes->{{3.5653627710636687`*^9, 3.5653627797706504`*^9}, {
3.5653628101959224`*^9, 3.5653628192779245`*^9}, 3.5653630492614017`*^9}]
but this doesn't include the cell's content.
Answer
See if this does it for you. There are still some issues (see below).
nbA = CreateDocument[{Cell["Notebook A", "Title"],
ExpressionCell[
Dynamic[Refresh[
NotebookWrite[dynamicCellObjB, NotebookRead[dynamicCellObjA]];
dynamicCellObjB = First@Cells[nbB, CellTags -> "DynamicCell"];
"cell to be updated:", TrackedSymbols -> {},
UpdateInterval -> 5]]],
Cell["Edit this", "Text", CellTags -> {"DynamicCell"}]}];
dynamicCellObjA = First@Cells[nbA, CellTags -> "DynamicCell"];
nbB = CreateDocument[{Cell["Notebook B", "Title"],
Cell["Not this", "Text", CellTags -> {"DynamicCell"}]}];
dynamicCellObjB = First@Cells[nbB, CellTags -> "DynamicCell"];
You need something to trigger the update. Notebooks live in the front end and NotebookObjects
, while living in the kernel, are just references to the front end object. Dynamic interactivity requires some variable change or event in the kernel to trigger an update. Here I used Dynamic[Refresh[.., UpdateInterval->5]]
to cause an update every five seconds. You can change the update interval as you please. Another key is that the connected cells are identified by a unique cell tag ("DynamicCell"
was my choice of name). There are some timing issues. I occasionally got a message on start-up, if a symbol was evaluated before it was defined.
If it seems on the right track, I hope you can adapt it to your notebooks.
Comments
Post a Comment