A cell is a single input/output block. Worksheets are built out of a list of cells.
EXAMPLES:
sage: C1 = sage.server.notebook.cell.Cell(0, '2+3', '5', None)
sage: C2 = sage.server.notebook.cell.Cell(0, '3+2', '5', None)
sage: C3 = sage.server.notebook.cell.Cell(1, '2+3', '5', None)
sage: C1 == C1
True
sage: C1 == C2
True
sage: C1 == C3
False
EXAMPLES:
sage: C = sage.server.notebook.cell.Cell(0, '2+3', '5', None)
sage: C == loads(dumps(C))
True
EXAMPLES:
sage: C = sage.server.notebook.cell.Cell(0, '2+3', '5', None); C
Cell 0; in=2+3, out=5
Returns a string of the directory associated to self.
EXAMPLES:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: nb.add_user('sage','sage','sage@sagemath.org',force=True)
sage: W = nb.create_new_worksheet('Test', 'sage')
sage: C = sage.server.notebook.cell.Cell(0, '2+3', '5', W)
sage: C._directory_name()
'.../worksheets/sage/0/cells/0'
sage: import shutil; shutil.rmtree(nb.directory())
Returns the cell output type.
EXAMPLES:
sage: C = sage.server.notebook.cell.Cell(0, '2+3', '5', None)
sage: C.cell_output_type()
'wrap'
sage: C.set_cell_output_type('nowrap')
sage: C.cell_output_type()
'nowrap'
Returns the changed input text for the cell. If there was any changed input text, then it is reset to ” before this method returns.
EXAMPLES:
sage: C = sage.server.notebook.cell.Cell(0, '2+3', '5', None)
sage: C.changed_input_text()
''
sage: C.set_changed_input_text('3+3')
sage: C.input_text()
'3+3'
sage: C.changed_input_text()
'3+3'
sage: C.changed_input_text()
''
sage: C.version()
0
Returns the input text with all of the percent directives removed. If the cell is interacting, then the interacting text is returned.
EXAMPLES:
sage: C = sage.server.notebook.cell.Cell(0, '%hide\n%maxima\n2+3', '5', None)
sage: C.cleaned_input_text()
'2+3'
Returns True if self is in its worksheet’s queue.
EXAMPLES:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: nb.add_user('sage','sage','sage@sagemath.org',force=True)
sage: W = nb.create_new_worksheet('Test', 'sage')
sage: C = W.new_cell_after(0, "2^2")
sage: C.computing()
False
sage: import shutil; shutil.rmtree(nb.directory())
Deletes all of the files associated with this cell.
EXAMPLES:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: nb.add_user('sage','sage','sage@sagemath.org',force=True)
sage: W = nb.create_new_worksheet('Test', 'sage')
sage: C = sage.server.notebook.cell.Cell(0, 'plot(sin(x),0,5)', ", W)sage:
C.evaluate()
sage: W.check_comp(wait=9999)
('d', Cell 0; in=plot(sin(x),0,5), out=
<html><font color='black'><img src='cell://sage0.png'></font></html>
<BLANKLINE>
)
sage: C.files()
['sage0.png']
sage: C.delete_files()
sage: C.files()
[]
Delete all output in this cell.
EXAMPLES:
sage: C = sage.server.notebook.cell.Cell(0, '2+3', '5', None); C
Cell 0; in=2+3, out=5
sage: C.delete_output()
sage: C
Cell 0; in=2+3, out=
Returns the directory associated to self. If the directory doesn’t already exist, then this method creates it.
EXAMPLES:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: nb.add_user('sage','sage','sage@sagemath.org',force=True)
sage: W = nb.create_new_worksheet('Test', 'sage')
sage: C = sage.server.notebook.cell.Cell(0, '2+3', '5', W)
sage: C.directory()
'.../worksheets/sage/0/cells/0'
sage: import shutil; shutil.rmtree(nb.directory())
EXAMPLES:
sage: C = sage.server.notebook.cell.Cell(0, '2+3', '5', None)
sage: C.edit_text()
'{{{id=0|\n2+3\n///\n5\n}}}'
INPUT:
EXAMPLES: We create a notebook, worksheet, and cell and evaluate it
in order to compute :
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: nb.add_user('sage','sage','sage@sagemath.org',force=True)
sage: W = nb.create_new_worksheet('Test', 'sage')
sage: W.edit_save('Sage\n{{{\n3^5\n}}}')
sage: C = W.cell_list()[0]; C
Cell 0; in=3^5, out=
sage: C.evaluate(username='sage')
sage: W.check_comp(wait=9999)
('d', Cell 0; in=3^5, out=
243
)
sage: C
Cell 0; in=3^5, out=
243
sage: import shutil; shutil.rmtree(nb.directory())
Return True if this cell has been successfully evaluated in a currently running session.
This is not about whether the output of the cell is valid given the input.
OUTPUT:
EXAMPLES: We create a worksheet with a cell that has wrong output:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: nb.add_user('sage','sage','sage@sagemath.org',force=True)
sage: W = nb.create_new_worksheet('Test', 'sage')
sage: W.edit_save('Sage\n{{{\n2+3\n///\n20\n}}}')
sage: C = W.cell_list()[0]
sage: C
Cell 0; in=2+3, out=
20
We re-evaluate that input cell:
sage: C.evaluate()
sage: W.check_comp(wait=9999)
('d', Cell 0; in=2+3, out=
5
)
Now the output is right:
sage: C
Cell 0; in=2+3, out=
5
And the cell is considered to have been evaluated.
sage: C.evaluated()
True
sage: import shutil; shutil.rmtree(nb.directory())
Returns a list of all the files in self’s directory.
EXAMPLES:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: nb.add_user('sage','sage','sage@sagemath.org',force=True)
sage: W = nb.create_new_worksheet('Test', 'sage')
sage: C = sage.server.notebook.cell.Cell(0, 'plot(sin(x),0,5)', ", W)sage:
C.evaluate()
sage: W.check_comp(wait=9999)
('d', Cell 0; in=plot(sin(x),0,5), out=
<html><font color='black'><img src='cell://sage0.png'></font></html>
<BLANKLINE>
)
sage: C.files()
['sage0.png']
sage: import shutil; shutil.rmtree(nb.directory())
Returns True if there is output for this cell.
EXAMPLES:
sage: C = sage.server.notebook.cell.Cell(0, '2+3', '5', None)
sage: C.has_output()
True
sage: C = sage.server.notebook.cell.Cell(0, '2+3', '', None)
sage: C.has_output()
False
Returns the HTML code for the input of this cell.
EXAMPLES:
sage: C = sage.server.notebook.cell.Cell(0, '2+3', '5', None)
sage: print C.html_in()
<div class="insert_new_cell" id="insert_new_cell_0"...</a>
Returns the HTML code for inserting a new cell after self.
EXAMPLES:
sage: C = sage.server.notebook.cell.Cell(0, '2+3', '5', None)
sage: print C.html_new_cell_after()
<div class="insert_new_cell" id="insert_new_cell_0">...
Returns the HTML code for inserting a new cell before self.
EXAMPLES:
sage: C = sage.server.notebook.cell.Cell(0, '2+3', '5', None)
sage: print C.html_new_cell_before()
<div class="insert_new_cell" id="insert_new_cell_0">...
Returns the id of self.
EXAMPLES:
sage: C = sage.server.notebook.cell.Cell(0, '2+3', '5', None)
sage: C.id()
0
Returns self’s input text.
EXAMPLES:
sage: C = sage.server.notebook.cell.Cell(0, '2+3', '5', None)
sage: C.input_text()
'2+3'
Record that the calculation running in this cell was interrupted.
EXAMPLES:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: nb.add_user('sage','sage','sage@sagemath.org',force=True)
sage: W = nb.create_new_worksheet('Test', 'sage')
sage: C = W.new_cell_after(0, "2^2")
sage: C.interrupt()
sage: C.interrupted()
True
sage: C.evaluated()
False
sage: import shutil; shutil.rmtree(nb.directory())
Returns True if the evaluation of this cell has been interrupted.
EXAMPLES:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: nb.add_user('sage','sage','sage@sagemath.org',force=True)
sage: W = nb.create_new_worksheet('Test', 'sage')
sage: C = W.new_cell_after(0, "2^2")
sage: C.interrupt()
sage: C.interrupted()
True
sage: import shutil; shutil.rmtree(nb.directory())
TODO: Figure out what the __introspect method is for and write a better doctest.
EXAMPLES:
sage: C = sage.server.notebook.cell.Cell(0, '2+3', '5', None)
sage: C.introspect()
False
sage: C.set_introspect("a", "b")
sage: C.introspect()
['a', 'b']
Return True if this is an asap cell, i.e., evaluation of it is done as soon as possible.
EXAMPLES:
sage: C = sage.server.notebook.cell.Cell(0, '2+3', '5', None)
sage: C.is_asap()
False
sage: C.set_asap(True)
sage: C.is_asap()
True
Returns True if self is an auto cell.
An auto cell is a cell that is automatically evaluated when the worksheet starts up.
EXAMPLES:
sage: C = sage.server.notebook.cell.Cell(0, '2+3', '5', None)
sage: C.is_auto_cell()
False
sage: C = sage.server.notebook.cell.Cell(0, '#auto\n2+3', '5', None)
sage: C.is_auto_cell()
True
Returns True if this is an HTML cell. An HTML cell whose system is ‘html’ and is typically specified by %html.
EXAMPLES:
sage: C = sage.server.notebook.cell.Cell(0, "%html\nTest HTML", None, None)
sage: C.system()
'html'
sage: C.is_html()
True
sage: C = sage.server.notebook.cell.Cell(0, "Test HTML", None, None)
sage: C.is_html()
False
Return True if this cell contains the use of interact either as a function call or a decorator.
EXAMPLES:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: nb.add_user('sage','sage','sage@sagemath.org',force=True)
sage: W = nb.create_new_worksheet('Test', 'sage')
sage: C = W.new_cell_after(0, "@interact\ndef f(a=slider(0,10,1,5):\n print a^2")
sage: C.is_interactive_cell()
True
sage: C = W.new_cell_after(C.id(), "2+2")
sage: C.is_interactive_cell()
False
sage: import shutil; shutil.rmtree(nb.directory())
Returns True if self is the last cell in the worksheet.
EXAMPLES:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: nb.add_user('sage','sage','sage@sagemath.org',force=True)
sage: W = nb.create_new_worksheet('Test', 'sage')
sage: C = W.new_cell_after(0, "2^2"); C
Cell 1; in=2^2, out=
sage: C.is_last()
True
sage: C = W.get_cell_with_id(0)
sage: C.is_last()
False
sage: import shutil; shutil.rmtree(nb.directory())
Return True if this is an no_output cell, i.e., a cell for which we don’t care at all about the output.
EXAMPLES:
sage: C = sage.server.notebook.cell.Cell(0, '2+3', '5', None)
sage: C.is_no_output()
False
sage: C.set_no_output(True)
sage: C.is_no_output()
True
Returns the id of the next cell in the worksheet associated to self. If self is not in the worksheet or self is the last cell in the cell_list, then the id of the first cell is returned.
EXAMPLES:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: nb.add_user('sage','sage','sage@sagemath.org',force=True)
sage: W = nb.create_new_worksheet('Test', 'sage')
sage: C = W.new_cell_after(0, "2^2")
sage: C = W.get_cell_with_id(0)
sage: C.next_id()
1
sage: C = W.get_cell_with_id(1)
sage: C.next_id()
0
sage: import shutil; shutil.rmtree(nb.directory())
Returns the notebook object associated to self.
EXAMPLES:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: nb.add_user('sage','sage','sage@sagemath.org',force=True)
sage: W = nb.create_new_worksheet('Test', 'sage')
sage: C = sage.server.notebook.cell.Cell(0, '2+3', '5', W)
sage: C.notebook() is nb
True
sage: import shutil; shutil.rmtree(nb.directory())
Returns a string which consists of the input text of this cell with the percent directives at the top removed. As it’s doing this, it computes a list of all the directives and which system (if any) the cell should be run under.
EXAMPLES:
sage: C = sage.server.notebook.cell.Cell(0, '%hide\n%maxima\n2+3', '5', None)
sage: C.parse_percent_directives()
'2+3'
sage: C.percent_directives()
['hide', 'maxima']
Returns a list of all the percent directives that appear in this cell.
EXAMPLES:
sage: C = sage.server.notebook.cell.Cell(0, '%hide\n%maxima\n2+3', '5', None)
sage: C.percent_directives()
['hide', 'maxima']
Returns the plain text version of self.
TODO: Add more comprehensive doctests.
TODO: Figure out what exactly this does.
EXAMPLES:
sage: C = sage.server.notebook.cell.Cell(0, '2+3', '5', None)
sage: C.sage() is None
True
EXAMPLES:
sage: C = sage.server.notebook.cell.Cell(0, '2+3', '5', None)
sage: C.is_asap()
False
sage: C.set_asap(True)
sage: C.is_asap()
True
Sets the cell output type.
EXAMPLES:
sage: C = sage.server.notebook.cell.Cell(0, '2+3', '5', None)
sage: C.cell_output_type()
'wrap'
sage: C.set_cell_output_type('nowrap')
sage: C.cell_output_type()
'nowrap'
Note that this does not update the version of the cell. This is typically used for things like tab completion.
EXAMPLES:
sage: C = sage.server.notebook.cell.Cell(0, '2+3', '5', None)
sage: C.set_changed_input_text('3+3')
sage: C.input_text()
'3+3'
sage: C.changed_input_text()
'3+3'
Sets the id of self to id.
EXAMPLES:
sage: C = sage.server.notebook.cell.Cell(0, '2+3', '5', None)
sage: C.set_id(2)
sage: C.id()
2
Sets the input text of self to be the string input.
TODO: Add doctests for the code dealing with interact.
EXAMPLES:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: nb.add_user('sage','sage','sage@sagemath.org',force=True)
sage: W = nb.create_new_worksheet('Test', 'sage')
sage: C = W.new_cell_after(0, "2^2")
sage: C.evaluate()
sage: W.check_comp(wait=9999)
('d', Cell 1; in=2^2, out=
4
)
sage: C.version()
0
sage: C.set_input_text('3+3')
sage: C.input_text()
'3+3'
sage: C.evaluated()
False
sage: C.version()
1
sage: import shutil; shutil.rmtree(nb.directory())
TODO: Figure out what the __introspect method is for and write a better doctest.
EXAMPLES:
sage: C = sage.server.notebook.cell.Cell(0, '2+3', '5', None)
sage: C.set_introspect("a", "b")
sage: C.introspect()
['a', 'b']
Sets whether or not this cell is an HTML cell.
This is called by check_for_system_switching in worksheet.py.
EXAMPLES:
sage: C = sage.server.notebook.cell.Cell(0, '2+3', '5', None)
sage: C.is_html()
False
sage: C.set_is_html(True)
sage: C.is_html()
True
Sets whether or not this is an no_output cell, i.e., a cell for which we don’t care at all about the output.
EXAMPLES:
sage: C = sage.server.notebook.cell.Cell(0, '2+3', '5', None)
sage: C.is_no_output()
False
sage: C.set_no_output(True)
sage: C.is_no_output()
True
Sets the worksheet object of self to be worksheet and optionally changes the id of self.
EXAMPLES:
sage: C = sage.server.notebook.cell.Cell(0, '2+3', '5', None)
sage: W = "worksheet object"
sage: C.set_worksheet(W)
sage: C.worksheet()
'worksheet object'
sage: C.set_worksheet(None, id=2)
sage: C.id()
2
Returns the system used to evaluate this cell. The system is specified by a percent directive like ‘%maxima’ at the top of a cell.
If no system is explicitly specified, then None is returned which tells the notebook to evaluate the cell using the worksheet’s default system.
EXAMPLES:
sage: C = sage.server.notebook.cell.Cell(0, '%maxima\n2+3', '5', None)
sage: C.system()
'maxima'
sage: prefixes = ['%hide', '%time', '']
sage: cells = [sage.server.notebook.cell.Cell(0, '%s\n2+3'%prefix, '5', None) for prefix in prefixes]
sage: [(C, C.system()) for C in cells if C.system() is not None]
[]
Returns True if the time it takes to evaluate this cell should be printed.
EXAMPLES:
sage: C = sage.server.notebook.cell.Cell(0, '2+3', '5', None)
sage: C.time()
False
sage: C = sage.server.notebook.cell.Cell(0, '%time\n2+3', '5', None)
sage: C.time()
True
TODO: Figure out what the __introspect method is for and write a better doctest.
EXAMPLES:
sage: C = sage.server.notebook.cell.Cell(0, '2+3', '5', None)
sage: C.set_introspect("a", "b")
sage: C.introspect()
['a', 'b']
sage: C.unset_introspect()
sage: C.introspect()
False
Update the list of files with html-style links or embeddings for this cell.
For interactive cells the html output section is always empty, mainly because there is no good way to distinguish content (e.g., images in the current directory) that goes into the interactive template and content that would go here.
Returns a notebook URL for this cell.
EXAMPLES:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: nb.add_user('sage','sage','sage@sagemath.org',force=True)
sage: W = nb.create_new_worksheet('Test', 'sage')
sage: C = sage.server.notebook.cell.Cell(0, '2+3', '5', W)
sage: C.url_to_self()
'/home/sage/0/cells/0'
Returns the version number of this cell.
EXAMPLES:
sage: C = sage.server.notebook.cell.Cell(0, '2+3', '5', None)
sage: C.version()
0
sage: C.set_input_text('2+3')
sage: C.version()
1
Returns the number of columns for word wrapping. This defaults to 70, but the default setting for a notebook is 72.
EXAMPLES:
sage: C = sage.server.notebook.cell.Cell(0, '2+3', '5', None)
sage: C.word_wrap_cols()
70
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: nb.add_user('sage','sage','sage@sagemath.org',force=True)
sage: W = nb.create_new_worksheet('Test', 'sage')
sage: C = sage.server.notebook.cell.Cell(0, '2+3', '5', W)
sage: C.word_wrap_cols()
72
sage: import shutil; shutil.rmtree(nb.directory())
Returns the workseet associated to self.
EXAMPLES:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: nb.add_user('sage','sage','sage@sagemath.org',force=True)
sage: W = nb.create_new_worksheet('Test', 'sage')
sage: C = sage.server.notebook.cell.Cell(0, '2+3', '5', W)
sage: C.worksheet() is W
True
sage: import shutil; shutil.rmtree(nb.directory())
Returns the filename of the worksheet associated to self.
EXAMPLES:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: nb.add_user('sage','sage','sage@sagemath.org',force=True)
sage: W = nb.create_new_worksheet('Test', 'sage')
sage: C = sage.server.notebook.cell.Cell(0, '2+3', '5', W)
sage: C.worksheet_filename()
'sage/0'
sage: import shutil; shutil.rmtree(nb.directory())
Delete all output in this cell. This is not executed - it is an abstract function that must be overwritten in a derived class.
EXAMPLES: This function just raises a NotImplementedError, since it most be defined in derived class.
sage: C = sage.server.notebook.cell.Cell_generic()
sage: C.delete_output()
...
NotImplementedError
Returns True if this cell contains the use of interact either as a function call or a decorator.
EXAMPLES:
sage: from sage.server.notebook.cell import Cell_generic
sage: C = sage.server.notebook.cell.TextCell(0, '2+3', None)
sage: Cell_generic.is_interactive_cell(C)
False
EXAMPLES:
sage: C1 = sage.server.notebook.cell.TextCell(0, '2+3', None)
sage: C2 = sage.server.notebook.cell.TextCell(0, '3+2', None)
sage: C3 = sage.server.notebook.cell.TextCell(1, '2+3', None)
sage: C1 == C1
True
sage: C1 == C2
True
sage: C1 == C3
False
EXAMPLES:
sage: C = sage.server.notebook.cell.TextCell(0, '2+3', None)
sage: C == loads(dumps(C))
True
String representation of this text cell.
EXAMPLES:
sage: C = sage.server.notebook.cell.TextCell(0, '2+3', None)
sage: C.__repr__()
'TextCell 0: 2+3'
Delete all output in this cell. This does nothing since text cells have no output.
EXAMPLES:
sage: C = sage.server.notebook.cell.TextCell(0, '2+3', None)
sage: C
TextCell 0: 2+3
sage: C.delete_output()
sage: C
TextCell 0: 2+3
EXAMPLES:
sage: C = sage.server.notebook.cell.TextCell(0, '2+3', None)
sage: C.edit_text()
'2+3'
Returns an HTML version of self as a string.
INPUT:
EXAMPLES:
sage: C = sage.server.notebook.cell.TextCell(0, '2+3', None)
sage: C.html()
'<div class="text_cell" id="cell_text_0">2+3...'
sage: C.set_input_text("$2+3$")
sage: C.html(do_math_parse=True)
'<div class="text_cell" id="cell_text_0"><span class="math">2+3</span>...'
Returns an HTML version of the content of self as a string.
INPUT:
EXAMPLES:
sage: C = sage.server.notebook.cell.TextCell(0, '2+3', None)
sage: C.html_inner()
'2+3...'
sage: C.set_input_text("$2+3$")
sage: C.html_inner(do_math_parse=True)
'<span class="math">2+3</span>...'
EXAMPLES:
sage: C = sage.server.notebook.cell.TextCell(0, '2+3', None)
sage: C.id()
0
EXAMPLES:
sage: C = sage.server.notebook.cell.TextCell(0, '2+3', None)
sage: C.is_auto_cell()
False
EXAMPLES:
sage: C = sage.server.notebook.cell.TextCell(0, '2+3', None)
sage: C.plain_text()
'2+3'
This does nothing for TextCells.
EXAMPLES:
sage: C = sage.server.notebook.cell.TextCell(0, '2+3', None)
sage: C.set_cell_output_type("wrap")
Sets the input text of self to be input_text.
EXAMPLES:
sage: C = sage.server.notebook.cell.TextCell(0, '2+3', None)
sage: C
TextCell 0: 2+3
sage: C.set_input_text("3+2")
sage: C
TextCell 0: 3+2
Sets the worksheet object of self to be worksheet and optionally changes the id of self.
EXAMPLES:
sage: C = sage.server.notebook.cell.TextCell(0, '2+3', None)
sage: W = "worksheet object"
sage: C.set_worksheet(W)
sage: C.worksheet()
'worksheet object'
sage: C.set_worksheet(None, id=2)
sage: C.id()
2
Returns the worksheet object associated to self.
EXAMPLES:
sage: C = sage.server.notebook.cell.TextCell(0, '2+3', 'worksheet object')
sage: C.worksheet()
'worksheet object'
Make it so exceptions don’t appear expanded by default.
INPUT:
OUTPUT: string
If s0 contains “notracebacks” then this function always returns s0
EXAMPLES:
sage: sage.server.notebook.cell.format_exception(sage.server.notebook.cell.TRACEBACK,80)
'\nTraceback (click to the left for traceback)\n...\nTraceback (most recent call last):'
sage: sage.server.notebook.cell.format_exception(sage.server.notebook.cell.TRACEBACK + "notracebacks",80)
'Traceback (most recent call last):notracebacks'
Returns the number of rows needed to display the string in txt if there are a maximum of ncols columns per row.
EXAMPLES:
sage: from sage.server.notebook.cell import number_of_rows
sage: s = "asdfasdf\nasdfasdf\n"
sage: number_of_rows(s, 8)
2
sage: number_of_rows(s, 5)
4
sage: number_of_rows(s, 4)
4