Capture cell facts

  • The Cell Facts feature in the notebook allows direct capturing of cell outputs, such as print statements, tables, and images, and automatically saves them into HTML snippets. These HTML files can be easily uploaded as attachments, appearing as inline-rendered content in the UI, without the need for additional ‘save to file’ statements

Magic command: %%capture_cell_facts

Important

  • Using capture_cell_facts magic command on top of any cell will capture cell outputs and write to a html file which can be used to set as attachment under any defined custom facts.

  • Captured cell facts are written to temp dir {cur dir}/captured_cell_facts/Captured_Cell_Output.html

  • Each cell run output will overwrite the previous temp html file of captured cell facts. Each command output is captured as a snippet html.

  • Two args are supported with the magic command:

    • -po or --printmsgonly: print statements are only captured from any given cell

    • -cc or --capturecode: capture cell codes as well with output

  • In case of any cell mix of print messages and images or tables etc., it will capture everything except messages using print(). If you want to capture all, please use display() in place of print() method.

  • Once facts are captured, you can use `set_cell_attachment_fact()` api to attach it to any custom fact id.

set_cell_attachment_fact(self, description: str, fact_id: str)

Set attachment fact using captured cell output. Supported for CPD version >=4.6.5.

Parameters:
  • fact_id (str) – Fact id for the attachment

  • description (str) – (Optional) Description about the cell facts attachment file

A way to use me is:

model.set_cell_attachment_fact(description='<file description>', fact_id='<custom fact id>')

Sample

  • Capture print statements

%%capture_cell_facts -po
  # capture print stmt only

  print("Working on Iris data")
  print("Model trained")

  wml_model.set_cell_attachment_fact(description="Print output",fact_id="print_store")
  • Capture confusion metrix

 %%capture_cell_facts

from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay

predictions1=classifier.predict(x_test)

cm1 = confusion_matrix(y_test, predictions1,labels=classifier.classes_)
disp1 = ConfusionMatrixDisplay(confusion_matrix=cm1,display_labels=classifier.classes_)
disp1.plot()
plt.show()

time.sleep(2)
wml_model.set_cell_attachment_fact(description="Confusion metrix",fact_id="con_metrix_store")