Wolfram Research

Function Repository Resource:

PrintAsCellObject (1.0.0) current version: 2.0.0 »

Source Notebook

Print an expression and return the corresponding CellObject

Contributed by: Richard Hennigan (Wolfram Research)

ResourceFunction["PrintAsCellObject"][expr]

prints expr as output and yields a CellObject corresponding to the printed cell.

Details and Options

In a notebook, ResourceFunction["PrintAsCellObject"] generates a cell with style "Print".
ResourceFunction["PrintAsCellObject"] can print any expression, including graphics and dynamic objects.
ResourceFunction["PrintAsCellObject"][expr1,expr2,] prints expr1 concatenated together, effectively using Row.
With a text-based interface, ResourceFunction["PrintAsCellObject"] ends its output with a single newline (line feed).
If no front end is available, the output of ResourceFunction["PrintAsCellObject"] is a Missing object.
You can arrange to have expressions on several lines by using Column.
ResourceFunction["PrintAsCellObject"] sends its output to the channel $Output.
ResourceFunction["PrintAsCellObject"] uses the format type of $Output as its default format type.
ResourceFunction["PrintAsCellObject"] requires dynamic interactivity to be enabled in order to return the CellObject. To toggle dynamic interactivity, use the Evaluation Dynamic Updating Enabled menu item or the "ToggleDynamicUpdating" front end token.
ResourceFunction["PrintAsCellObject"] has the option TimeConstraint, which sets a limit on how long it waits to get the CellObject from the front end. If the cell cannot be acquired before the time limit, $TimedOut is returned instead.
Setting the TimeConstraint option for ResourceFunction["PrintAsCellObject"] must be done with SetOptions, since it would otherwise be interpreted as an argument to print.

Examples

Basic Examples (3) 

Print an expression:

In[1]:=
ResourceFunction["PrintAsCellObject"][x + y]
Out[1]=

Get the printed CellObject for manipulation in the front end:

In[2]:=
cell = ResourceFunction["PrintAsCellObject"][a + b]
Out[2]=

View the corresponding Cell expression:

In[3]:=
NotebookRead[cell]
Out[3]=

Select the cell contents:

In[4]:=
SelectionMove[cell, All, CellContents]

Delete the cell:

In[5]:=
NotebookDelete[cell]

Print the first five primes:

In[6]:=
printed = Table[ResourceFunction["PrintAsCellObject"][Prime[n]], {n, 5}]
Out[6]=

Clean up the printed cells:

In[7]:=
NotebookDelete[printed]

Scope (4) 

Print graphics:

In[8]:=
ResourceFunction["PrintAsCellObject"][Graphics[Circle[]]]
Out[8]=
In[9]:=
ResourceFunction["PrintAsCellObject"][Graphics3D[Sphere[]]]
Out[9]=

Multiple arguments will be concatenated together:

In[10]:=
ResourceFunction["PrintAsCellObject"][x^2, "=", 12434]
Out[10]=

Print an unevaluated expression:

In[11]:=
ResourceFunction["PrintAsCellObject"][Unevaluated[Echo[1 + 1]]]
Out[11]=

If no arguments are given, an empty cell will be printed:

In[12]:=
ResourceFunction["PrintAsCellObject"][]
Out[12]=
In[13]:=
NotebookRead[%]
Out[13]=

Properties and Relations (2) 

The cells generated by PrintAsCellObject are equivalent to those generated by Print:

In[14]:=
cell = ResourceFunction["PrintAsCellObject"][a + b]
Out[14]=
In[15]:=
NotebookRead[cell]
Out[15]=
In[16]:=
Print[a + b]
In[17]:=
NotebookRead[PreviousCell[]]
Out[17]=

However, the output of Print is always Null:

In[18]:=
Print[a + b] // InputForm
Out[18]=
In[19]:=
ResourceFunction["PrintAsCellObject"][a + b] // InputForm
Out[19]=

PrintAsCellObject returns a CellObject similar to PrintTemporary, but does not automatically remove the cell:

In[20]:=
cell = PrintTemporary["hello"]
Pause[3]
Out[20]=

Possible Issues (5) 

In a kernel session that has no front end, the output is a Missing object:

In[21]:=
ResourceFunction["LocalEvaluate"][
 ResourceFunction["PrintAsCellObject"]["test"]]
Out[21]=

In a session where a front end, but no dynamic interactivity, is available, PrintAsCellObject will time out while waiting for a cell:

In[22]:=
ResourceFunction["LocalEvaluate"][
 UsingFrontEnd[ResourceFunction["PrintAsCellObject"]["test"]]]
Out[22]=

Using options must be done with SetOptions, since they would otherwise be interpreted as arguments:

In[23]:=
ResourceFunction["PrintAsCellObject"]["test", TimeConstraint -> 0.0001]
Out[23]=
In[24]:=
SetOptions[ResourceFunction["PrintAsCellObject"], TimeConstraint -> 0.0001]
Out[24]=
In[25]:=
ResourceFunction["PrintAsCellObject"]["test"]
Out[25]=
In[26]:=
SetOptions[ResourceFunction["PrintAsCellObject"], TimeConstraint -> Automatic]
Out[26]=

Unlike Print, PrintAsCellObject needs to wait for communication between the kernel and front end:

In[27]:=
AbsoluteTiming[ResourceFunction["PrintAsCellObject"]["test"]]
Out[27]=
In[28]:=
AbsoluteTiming[Print["test"]]
Out[28]=

If the value for TimeConstraint is too small, results will return faster, but there will not be enough time to get the cell:

In[29]:=
SetOptions[ResourceFunction["PrintAsCellObject"], TimeConstraint -> 0.0001]
Out[29]=
In[30]:=
AbsoluteTiming[ResourceFunction["PrintAsCellObject"]["test"]]
Out[30]=
In[31]:=
SetOptions[ResourceFunction["PrintAsCellObject"], TimeConstraint -> Automatic]
Out[31]=
In[32]:=
AbsoluteTiming[ResourceFunction["PrintAsCellObject"]["test"]]
Out[32]=

Neat Examples (1) 

Create a version of Print that is automatically cleared after a specified amount of time:

In[33]:=
PrintForAWhile[expr___, t_?Positive] := With[{cell = ResourceFunction["PrintAsCellObject"][expr]},
   SessionSubmit[ScheduledTask[NotebookDelete[cell], {t}]]
   ];
In[34]:=
PrintForAWhile["This is only displayed for 5 seconds.", 5]
Out[34]=
In[35]:=
Do[With[{t = RandomReal[10]}, PrintForAWhile[
   ResourceFunction["BirdSay"][
    Row[{"This is only displayed for ", t, " seconds."}]], t]], 5]

Version History

  • 2.0.0 – 13 September 2022
  • 1.0.0 – 23 August 2019

Related Resources

License Information