Function Repository Resource:

EvaluateDefinitionNotebookSection

Source Notebook

Evaluate code in a resource function definition notebook

Contributed by: Richard Hennigan and Bob Sandheinrich

ResourceFunction["EvaluateDefinitionNotebookSection"][]

evaluates the definition section in the current InputNotebook[].

ResourceFunction["EvaluateDefinitionNotebookSection"]["section"]

evaluates the named section in the current InputNotebook[].

ResourceFunction["EvaluateDefinitionNotebookSection"][nb]

evaluates the definition section in the given notebook.

ResourceFunction["EvaluateDefinitionNotebookSection"][nb,"section"]

evaluates the named section in the given notebook.

Details and Options

The notebook nb should be a NotebookObject expression, typically given by functions like EvaluationNotebook, InputNotebook, Notebooks or ResourceFunction["ClickedNotebook"].
Some typical sections include "Definition", "Examples" and "Tests".
Definitions are defined as standard symbols in the current $Context, not as ResourceFunction objects.
Re-evaluating definitions from a definition notebook is not guaranteed to give the same definitions as the resource function itself.
Using ResourceFunction["EvaluateDefinitionNotebookSection"] to evaluate a section is equivalent to selecting that section's cell group and pressing .

Examples

Basic Examples (2) 

Evaluate the definition section of a function resource notebook:

In[1]:=
nb = CreateNotebook["FunctionResource"]
Out[1]=
In[2]:=
evaluated = ResourceFunction["EvaluateDefinitionNotebookSection"][nb, "Definition"]
Out[2]=

View the evaluated cells:

In[3]:=
RawBoxes /@ NotebookRead[evaluated]
Out[3]=

Verify that the function is defined:

In[4]:=
MyFunction[]
Out[4]=
In[5]:=
NotebookClose[nb]

Retrieve a resource function definition notebook and evaluate the definitions:

In[6]:=
ResourceFunction["EvaluateDefinitionNotebookSection"][
 ResourceFunction["Wiggled", "DefinitionNotebook"]]
Out[6]=

The function is now defined:

In[7]:=
Wiggled["How Convenient"]
Out[7]=

Scope (4) 

Get a definition notebook:

In[8]:=
nb = ResourceFunction["AbortOnMessage", "DefinitionNotebook"]
Out[8]=

Define the function by evaluating the "Definition" section:

In[9]:=
ResourceFunction[
 "EvaluateDefinitionNotebookSection"][nb, "Definition"]
Out[9]=

Run the tests:

In[10]:=
ResourceFunction["EvaluateDefinitionNotebookSection"][nb, "Tests"]
Out[10]=

Run the examples:

In[11]:=
ResourceFunction["EvaluateDefinitionNotebookSection"][nb, "Examples"]
Out[11]=
In[12]:=
NotebookClose[nb]

Properties and Relations (2) 

Evaluate the "Data Definitions" section of a data resource notebook:

In[13]:=
nb = CreateNotebook["DataResource"]
Out[13]=
In[14]:=
cells = ResourceFunction["EvaluateDefinitionNotebookSection"][nb, "Data Definitions"]
Out[14]=

View the evaluated section:

In[15]:=
RawBoxes /@ NotebookRead[cells] // Column
Out[15]=
In[16]:=
NotebookClose[nb]

EvaluateDefinitionNotebookSection can work on any notebook that has cell groups with CellTags set for the first cell in the group:

In[17]:=
nb = CreateDocument[{
   Cell["First Section", "Section", CellTags -> "ThisOne"],
   ExpressionCell[Defer[1 + 1], "Input"],
   Cell["Second Section", "Section", CellTags -> "ThatOne"],
   ExpressionCell[Defer[2 + 2], "Input"],
   ExpressionCell[Defer[3 + 3], "Input"]
   }]
Out[17]=

Evaluate the first section:

In[18]:=
ResourceFunction["EvaluateDefinitionNotebookSection"][nb, "ThisOne"]
Out[18]=

Evaluate the second section:

In[19]:=
ResourceFunction["EvaluateDefinitionNotebookSection"][nb, "ThatOne"]
Out[19]=
In[20]:=
NotebookClose[nb]

Possible Issues (1) 

If the section does not exist in the notebook, an error message will be printed:

In[21]:=
nb = CreateNotebook["FunctionResource"]
Out[21]=
In[22]:=
ResourceFunction[
 "EvaluateDefinitionNotebookSection"][nb, "ThisDoesNotExist"]
Out[22]=

Version History

  • 1.0.0 – 20 January 2020

Related Resources

License Information