Function Repository Resource:

PrintAsCellObject

Source Notebook

Print an expression and get 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.

Examples

Basic Examples (3) 

Print an expression:

In[1]:=
ResourceFunction[
 "PrintAsCellObject", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][x + y]
Out[1]=

Get the printed CellObject for manipulation in the front end:

In[2]:=
cell = ResourceFunction[
  "PrintAsCellObject", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][a + b]
Out[2]=

View the corresponding Cell expression:

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

Print the first five primes:

In[4]:=
printed = Table[ResourceFunction[
   "PrintAsCellObject", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][Prime[n]], {n, 5}]
Out[4]=

Scope (4) 

Print graphics:

In[5]:=
ResourceFunction[
 "PrintAsCellObject", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][Graphics[Circle[]]]
Out[5]=
In[6]:=
ResourceFunction[
 "PrintAsCellObject", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][Graphics3D[Sphere[]]]
Out[6]=

Multiple arguments will be concatenated together:

In[7]:=
ResourceFunction[
 "PrintAsCellObject", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][x^2, "=", 12434]
Out[7]=

Print an unevaluated expression:

In[8]:=
ResourceFunction[
 "PrintAsCellObject", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][Unevaluated[Echo[1 + 1]]]
Out[8]=

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

In[9]:=
ResourceFunction[
 "PrintAsCellObject", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][]
Out[9]=
In[10]:=
NotebookRead[%]
Out[10]=

Properties and Relations (2) 

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

In[11]:=
cell = ResourceFunction[
  "PrintAsCellObject", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][a + b]
Out[11]=
In[12]:=
NotebookRead[cell]
Out[12]=
In[13]:=
Print[a + b]
In[14]:=
NotebookRead[PreviousCell[]]
Out[14]=

However, the output of Print is always Null:

In[15]:=
Print[a + b] // InputForm
Out[15]=
In[16]:=
ResourceFunction[
  "PrintAsCellObject", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][a + b] // InputForm
Out[16]=

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

In[17]:=
PrintTemporary["hello"]
Pause[3]
Out[17]=

Possible Issues (3) 

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

In[18]:=
RemoteEvaluate[
 ResourceFunction[
  "PrintAsCellObject", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"]["test"]]
Out[18]=

Wrapping the evaluation in UsingFrontEnd will give a CellObject:

In[19]:=
RemoteEvaluate[
 UsingFrontEnd[
  ResourceFunction[
   "PrintAsCellObject", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"]["test"]]]
Out[19]=

However, that CellObject is only useful in the front end instance that created it:

In[20]:=
NotebookRead[%]
Out[20]=
In[21]:=
RemoteEvaluate[
 UsingFrontEnd[
  NotebookRead[
   ResourceFunction[
    "PrintAsCellObject", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"]["test"]]]]
Out[21]=

Unlike Print, PrintAsCellObject needs to wait for a response from the front end:

In[22]:=
AbsoluteTiming[
 ResourceFunction[
  "PrintAsCellObject", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"]["test"]]
Out[22]=
In[23]:=
AbsoluteTiming[Print["test"]]
Out[23]=

PrintAsCellObject does not inherit settings for GeneratedCellStyles:

In[24]:=
SetOptions[EvaluationCell[], GeneratedCellStyles -> {"Print" -> {"Print", Background -> LightBlue}}];
Print["This has the correct style."];
ResourceFunction[
  "PrintAsCellObject", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"]["This does not."];

Neat Examples (1) 

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

In[25]:=
PrintForAWhile[expr___, t_?Positive] :=
  With[{cell = ResourceFunction[
      "PrintAsCellObject", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][expr]},
   SessionSubmit[ScheduledTask[NotebookDelete[cell], {t}]]
   ];
In[26]:=
PrintForAWhile["This is only displayed for 5 seconds.", 5]
Out[26]=
In[27]:=
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