Function Repository Resource:

MonitorFile

Source Notebook

Create a FrontEnd object that updates when a file changes

Contributed by: Sjoerd Smit

ResourceFunction["MonitorFile"][Dynamic[expr], file]

monitors file and imports the contents with Import into expr when the file changes.

Details and Options

ResourceFunction["MonitorFile"] is a FrontEnd object and will only update when it is in focus. For example, if the notebook it lives in is minimized, the updates will not be performed.
ResourceFunction["MonitorFile"] takes several options:
"FileCheckFunction""LastModificationDate"file property used to check if contents have changed
"ImportFunction"Importfunction applied to file to import the contents
"ContentDisplayFunction"Automaticfunction applied to expr to summarize the file contents
"LayoutFunction"Automaticfunction that creates the final display
"PurgeContentsQ"Falseif True,expr will be set to Missing["FileMissing"] if file is deleted
UpdateInterval2how many seconds between running the file check
"Placeholder"ProgressIndicator[Appearance"Necklace"]object to show while importing file contents
If the "FileCheckFunction" prop option is a String, the changes of file will be tracked with Information[File[file],prop]. If a general function is provided (e.g., FileHash) for "FileCheckFunction", that function will be applied to file to track its changes.
The default "ContentDisplayFunction" is StringForm["`1` bytes imported",ByteCount[#]]&.
You can specify a custom "LayoutFunction" fun function that formats the final display. The function should be of the form fun[file,summary,modificationDate].

Examples

Basic Examples (5) 

Create a test file:

In[1]:=
file = Export["test.txt", ToString@RandomReal[1, 10]]
Out[1]=

Create an overview of the file status:

In[2]:=
ResourceFunction["MonitorFile"][Dynamic[expr], file]
Out[2]=

Check the imported contents:

In[3]:=
expr // FullForm
Out[3]=

Modify the file:

In[4]:=
Export[file, ToString@RandomReal[1, 10000]]
Out[4]=

The imported contents have changed:

In[5]:=
expr // Short
Out[5]=

Scope (1) 

If the file does not exist, a placeholder will be shown:

In[6]:=
ResourceFunction["MonitorFile"][Dynamic[expr], "abcd"]
Out[6]=

Options (11) 

FileCheckFunction (1) 

Use FileHash to test if the contents need to be refreshed:

In[7]:=
ResourceFunction["MonitorFile"][Dynamic[expr], "test.txt", "FileCheckFunction" -> FileHash]
Out[7]=

ImportFunction (2) 

Import the test file as a Wolfram Language expression instead of a string:

In[8]:=
ResourceFunction["MonitorFile"][Dynamic[expr], "test.txt", "ImportFunction" -> Function[Import[#, "WL"]]]
Out[8]=

The expression is a Wolfram Language List now:

In[9]:=
expr // FullForm
Out[9]=

ContentDisplayFunction (2) 

Display the first five elements of the imported list:

In[10]:=
ResourceFunction["MonitorFile"][Dynamic[expr], "test.txt",
 "ImportFunction" -> Function[Import[#, "WL"]],
 "ContentDisplayFunction" -> Function[Take[#, UpTo[5]]]]
Out[10]=

This affects the display, but not the underlying expression:

In[11]:=
expr
Out[11]=

LayoutFunction (1) 

Specify a custom function to typeset the file name, content preview and modification date of the file:

In[12]:=
ResourceFunction["MonitorFile"][Dynamic[expr], "test.txt",
 "ImportFunction" -> Function[Import[#, "WL"]],
 "ContentDisplayFunction" -> Function[Take[#, UpTo[5]]],
 "LayoutFunction" -> Function[{file, content, date},
   AssociationThread[{"File", "Preview", "FileModificationDate"}, {file, content, date}]
   ]
 ]
Out[12]=

PurgeContentsQ (2) 

With "PurgeContentsQ"False, expr will remain unaffected if the file does not exist (or stops existing):

In[13]:=
expr = 1;
ResourceFunction["MonitorFile"][Dynamic[expr], "abcd",
 "PurgeContentsQ" -> False]
Out[13]=

The variable didn't change:

In[14]:=
expr
Out[14]=

With "PurgeContentsQ"True, expr will be set to Missing["FileMissing"] if the file does not exist (or stops existing):

In[15]:=
Clear[expr2];
ResourceFunction["MonitorFile"][Dynamic[expr2], "abcd",
 "PurgeContentsQ" -> True]
Out[15]=
In[16]:=
expr2
Out[16]=

UpdateInterval (2) 

Set how often to check the file for changes:

In[17]:=
ResourceFunction["MonitorFile"][Dynamic[expr], "test.txt", UpdateInterval -> 5]
Out[17]=

Change the file to see how long it takes for the change to be picked up:

In[18]:=
Export["test.txt", ToString@RandomReal[1, 10]]
Out[18]=

Placeholder (1) 

When the import is slow, a placeholder is shown to indicate that it is busy:

In[19]:=
ResourceFunction["MonitorFile"][Dynamic[expr], "test.txt",
 "ImportFunction" -> Function[Pause[3]; Import[#, "WL"]],
 "Placeholder" -> ProgressIndicator[Appearance -> "Indeterminate"]
 ]
Out[19]=

Possible Issues (2) 

The tool will not update when it is hidden in the FrontEnd:

In[20]:=
Clear[expr];
OpenerView[{
  "Hidden",
  ResourceFunction["MonitorFile"][Dynamic[expr], "test.txt"]
  }, False]
Out[20]=

The expression does not update if you do not click the opener:

In[21]:=
expr
Out[21]=

Publisher

Sjoerd Smit

Version History

  • 2.0.0 – 04 October 2021

License Information