A Cell

A cell is a single input/output block. Worksheets are built out of a list of cells.

class sagenb.notebook.cell.Cell(id, input, out, worksheet)

Bases: sagenb.notebook.cell.Cell_generic

Creates a new compute cell.

INPUT:

  • id - an integer or string; the new cell’s ID
  • input - a string; this cell’s input
  • out - a string; this cell’s output
  • worksheet - a sagenb.notebook.worksheet.Worksheet instance; this cell’s worksheet object

EXAMPLES:

sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', None)
sage: C == loads(dumps(C))
True
cell_output_type()

Returns this compute cell’s output type.

OUTPUT:

  • a string

EXAMPLES:

sage: C = sagenb.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'
changed_input_text()

Returns the changed input text for this compute cell, deleting any previously stored text.

OUTPUT:

  • a string

EXAMPLES:

sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', None)
sage: initial_version=C.version()
sage: C.changed_input_text()
''
sage: C.set_changed_input_text('3+3')
sage: C.input_text()
u'3+3'
sage: C.changed_input_text()
u'3+3'
sage: C.changed_input_text()
''
sage: C.version()-initial_version
0
cleaned_input_text()

Returns this compute cell’s “cleaned” input text, i.e., its input with all of its percent directives removed. If this cell is interacting, it returns the interacting text.

OUTPUT:

  • a string

EXAMPLES:

sage: C = sagenb.notebook.cell.Cell(0, '%hide\n%maxima\n2+3', '5', None)
sage: C.cleaned_input_text()
u'2+3'
computing()

Returns whether this compute cell is queued for evaluation by its worksheet object.

OUTPUT:

  • a boolean

EXAMPLES:

sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir(ext='.sagenb'))
sage: nb.user_manager().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: nb.delete()
delete_files()

Deletes all of the files associated with this compute cell.

EXAMPLES:

sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir(ext='.sagenb'))
sage: nb.user_manager().add_user('sage','sage','sage@sagemath.org',force=True)
sage: W = nb.create_new_worksheet('Test', 'sage')
sage: C = sagenb.notebook.cell.Cell(0, 'plot(sin(x),0,5)', '', W)
sage: C.evaluate()
sage: W.check_comp(wait=9999)     # random output -- depends on computer speed
('d', Cell 0: in=plot(sin(x),0,5), out=
<html><font color='black'><img src='cell://sage0.png'></font></html>

)
sage: C.files()     # random output -- depends on computer speed
['sage0.png']
sage: C.delete_files()
sage: C.files()
[]
sage: W.quit()
sage: nb.delete()
delete_output()

Deletes all output in this compute cell. This also deletes the files, since they appear as output of the cell.

EXAMPLES:

sage: C = sagenb.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=

When output is deleted, any files in the cell directory are deleted as well:

sage: nb = sagenb.notebook.notebook.load_notebook(tmp_dir(ext='.sagenb'))
sage: nb.user_manager().add_user('sage','sage','sage@sagemath.org',force=True)
sage: W = nb.create_new_worksheet('Test', 'sage')
sage: W.edit_save('{{{\nplot(sin(x),(x,0,5))\n///\n20\n}}}')
sage: C = W.cell_list()[0]
sage: C.evaluate()
sage: W.check_comp(wait=9999)     # random output -- depends on computer speed
('d', Cell 0; in=plot(sin(x),(x,0,5)), out=
<html><font color='black'><img src='cell://sage0.png'></font></html>

)
sage: C.files()     # random output -- depends on computer speed
['sage0.png']
sage: C.delete_output()
sage: C.files()
[]
sage: W.quit()
sage: nb.delete()
directory()

Returns the name of this compute cell’s directory, creating it, if it doesn’t already exist.

OUTPUT:

  • a string

EXAMPLES:

sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir(ext='.sagenb'))
sage: nb.user_manager().add_user('sage','sage','sage@sagemath.org',force=True)
sage: W = nb.create_new_worksheet('Test', 'sage')
sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', W)
sage: C.directory()
'.../home/sage/0/cells/0'
sage: nb.delete()
edit_text(ncols=0, prompts=False, max_out=None)

Returns the text displayed for this compute cell in the Edit window.

INPUT:

  • ncols - an integer (default: 0); the number of word wrap columns
  • prompts - a boolean (default: False); whether to strip interpreter prompts from the beginning of each line
  • max_out - an integer (default: None); the maximum number of characters to return

OUTPUT:

  • a string

EXAMPLES:

sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', None)
sage: C.edit_text()
u'{{{id=0|\n2+3\n///\n5\n}}}'
sage: C = sagenb.notebook.cell.Cell(0, 'ěščřžýáíéďĎ', 'ěščřžýáíéďĎ', None)
sage: C.edit_text()
u'{{{id=0|\něščřž\xfd\xe1\xed\xe9ďĎ\n///\něščřž\xfd\xe1\xed\xe9ďĎ\n}}}'
evaluate(introspect=False, time=None, username=None)

Evaluates this compute cell.

INPUT:

  • introspect - a pair [before_cursor, after_cursor] of strings (default: False)
  • time - a boolean (default: None); whether to return the time the computation takes
  • username - a string (default: None); name of user doing the evaluation

EXAMPLES:

We create a notebook, worksheet, and cell and evaluate it in order to compute \(3^5\):

sage: nb = sagenb.notebook.notebook.load_notebook(tmp_dir(ext='.sagenb'))
sage: nb.user_manager().add_user('sage','sage','sage@sagemath.org',force=True)
sage: W = nb.create_new_worksheet('Test', 'sage')
sage: W.edit_save('{{{\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)     # random output -- depends on computer speed
('d', Cell 0: in=3^5, out=
243
)
sage: C     # random output -- depends on computer speed
Cell 0: in=3^5, out=
243
sage: W.quit()
sage: nb.delete()
evaluated()

Returns whether this compute 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:

  • a boolean

EXAMPLES: We create a worksheet with a cell that has wrong output:

sage: nb = sagenb.notebook.notebook.load_notebook(tmp_dir(ext='.sagenb'))
sage: nb.user_manager().add_user('sage','sage','sage@sagemath.org',force=True)
sage: W = nb.create_new_worksheet('Test', 'sage')
sage: W.edit_save('{{{\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)     # random output -- depends on computer speed
('w', Cell 0: in=2+3, out=)

Now the output is right:

sage: C     # random output -- depends on computer speed
Cell 0: in=2+3, out=

And the cell is considered to have been evaluated.

sage: C.evaluated()     # random output -- depends on computer speed
True
sage: W.quit()
sage: nb.delete()
files()

Returns a list of all the files in this compute cell’s directory.

OUTPUT:

  • a list of strings

EXAMPLES:

sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir(ext='.sagenb'))
sage: nb.user_manager().add_user('sage','sage','sage@sagemath.org',force=True)
sage: W = nb.create_new_worksheet('Test', 'sage')
sage: C = sagenb.notebook.cell.Cell(0, 'plot(sin(x),0,5)', '', W)
sage: C.evaluate()
sage: W.check_comp(wait=9999)     # random output -- depends on computer speed
('d', Cell 0: in=plot(sin(x),0,5), out=
<html><font color='black'><img src='cell://sage0.png'></font></html>

)
sage: C.files()     # random output -- depends on computer speed
['sage0.png']
sage: W.quit()
sage: nb.delete()
files_html(out)

Returns HTML to display the files in this compute cell’s directory.

INPUT:

  • out - a string; files to exclude. To exclude bar, foo, ..., use the format 'cell://bar cell://foo ...'

OUTPUT:

  • a string

EXAMPLES:

sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir(ext='.sagenb'))
sage: nb.user_manager().add_user('sage','sage','sage@sagemath.org',force=True)
sage: W = nb.create_new_worksheet('Test', 'sage')
sage: C = sagenb.notebook.cell.Cell(0, 'plot(sin(x),0,5)', '', W)
sage: C.evaluate()
sage: W.check_comp(wait=9999)     # random output -- depends on computer speed
('d', Cell 0: in=plot(sin(x),0,5), out=
<html><font color='black'><img src='cell://sage0.png'></font></html>

)
sage: C.files_html('')     # random output -- depends on computer speed
'<img src="/home/sage/0/cells/0/sage0.png?...">'
sage: W.quit()
sage: nb.delete()
has_output()

Returns whether this compute cell has any output.

OUTPUT:

  • a boolean

EXAMPLES:

sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', None)
sage: C.has_output()
True
sage: C = sagenb.notebook.cell.Cell(0, '2+3', '', None)
sage: C.has_output()
False
html(wrap=None, div_wrap=True, do_print=False, publish=False)

Returns the HTML for this compute cell.

INPUT:

  • wrap - an integer (default: None); the number of word wrap columns
  • div_wrap - a boolean (default: True); whether to wrap the output in outer div elements
  • do_print - a boolean (default: False); whether to return output suitable for printing
  • publish - a boolean (default: False); whether to render a published cell

OUTPUT:

  • a string

EXAMPLES:

sage: nb = sagenb.notebook.notebook.load_notebook(tmp_dir(ext='.sagenb'))
sage: nb.user_manager().add_user('sage','sage','sage@sagemath.org',force=True)
sage: W = nb.create_new_worksheet('Test', 'sage')
sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', W)
sage: C.html()
u'...cell_outer_0...2+3...5...'
input_text()

Returns this compute cell’s input text.

OUTPUT:

  • a string

EXAMPLES:

sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', None)
sage: C.input_text()
u'2+3'
interrupt()

Sets this compute cell’s evaluation as interrupted.

EXAMPLES:

sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir(ext='.sagenb'))
sage: nb.user_manager().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: nb.delete()
interrupted()

Returns whether this compute cell’s evaluation has been interrupted.

OUTPUT:

  • a boolean

EXAMPLES:

sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir(ext='.sagenb'))
sage: nb.user_manager().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: nb.delete()
introspect()

Returns compute cell’s introspection text.

OUTPUT:

  • a string 2-tuple (“before” and “after” text) or boolean (not introspecting)

EXAMPLES:

sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir(ext='.sagenb'))
sage: nb.user_manager().add_user('sage','sage','sage@sagemath.org',force=True)
sage: W = nb.create_new_worksheet('Test', 'sage')
sage: C = sagenb.notebook.cell.Cell(0, 'sage?', '', W)
sage: C.introspect()
False
sage: C.evaluate(username='sage')
sage: W.check_comp(9999)     # random output -- depends on computer speed
('d', Cell 0: in=sage?, out=)
sage: C.introspect()
[u'sage?', '']
sage: W.quit()
sage: nb.delete()
introspect_html()

Returns this compute cell’s introspection text, setting it to ‘’, if none is available.

OUTPUT:

  • a string

EXAMPLES:

sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir(ext='.sagenb'))
sage: nb.user_manager().add_user('sage','sage','sage@sagemath.org',force=True)
sage: W = nb.create_new_worksheet('Test', 'sage')
sage: C = sagenb.notebook.cell.Cell(0, 'sage?', '', W)
sage: C.introspect()
False
sage: C.evaluate(username='sage')
sage: W.check_comp(9999)     # random output -- depends on computer speed
('d', Cell 0: in=sage?, out=)
sage: C.introspect_html()     # random output -- depends on computer speed
u'...<div class="docstring">...sage...</pre></div>...'
sage: W.quit()
sage: nb.delete()
is_asap()

Returns whether this compute cell is to be evaluated as soon as possible (ASAP).

OUTPUT:

  • a boolean

EXAMPLES:

sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', None)
sage: C.is_asap()
False
sage: C.set_asap(True)
sage: C.is_asap()
True
is_auto_cell()

Returns whether this compute cell is evaluated automatically when its worksheet object starts up.

OUTPUT:

  • a boolean

EXAMPLES:

sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', None)
sage: C.is_auto_cell()
False
sage: C = sagenb.notebook.cell.Cell(0, '#auto\n2+3', '5', None)
sage: C.is_auto_cell()
True
is_html()

Returns whether this is an HTML compute cell, e.g., its system is ‘html’. This is typically specified by the percent directive %html.

OUTPUT:

  • a boolean

EXAMPLES:

sage: C = sagenb.notebook.cell.Cell(0, "%html\nTest HTML", None, None)
sage: C.system()
u'html'
sage: C.is_html()
True
sage: C = sagenb.notebook.cell.Cell(0, "Test HTML", None, None)
sage: C.is_html()
False
is_interacting()

Returns whether this compute cell is currently sagenb.notebook.interact.interact()ing.

OUTPUT:

  • a boolean

EXAMPLES:

sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir(ext='.sagenb'))
sage: nb.user_manager().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_interacting()
False
is_interactive_cell()

Returns whether this compute cell contains sagenb.notebook.interact.interact() either as a function call or decorator.

OUTPUT:

  • a boolean

EXAMPLES:

sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir(ext='.sagenb'))
sage: nb.user_manager().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: nb.delete()
is_no_output()

Returns whether this is a “no output” compute cell, i.e., we don’t care about its output.

OUTPUT:

  • a boolean

EXAMPLES:

sage: C = sagenb.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
next_compute_id()

Returns the ID of the next compute cell in this compute cell’s worksheet object. If this cell is not in the worksheet, it returns the ID of the worksheet’s first compute cell. If this is the last compute cell, it returns its own ID.

OUTPUT:

  • an integer or string

EXAMPLES:

sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir(ext='.sagenb'))
sage: nb.user_manager().add_user('sage','sage','sage@sagemath.org',force=True)
sage: W = nb.create_new_worksheet('Test', 'sage')
sage: W.edit_save('foo\n{{{\n2+3\n///\n5\n}}}bar\n{{{\n2+8\n///\n10\n}}}')
sage: W.new_cell_after(1, "2^2")
Cell 4: in=2^2, out=
sage: [W.get_cell_with_id(i).next_compute_id() for i in [1, 4, 3]]
[4, 3, 3]
output_html()

Returns this compute cell’s HTML output.

OUTPUT:

  • a string

EXAMPLES:

sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', None)
sage: C.output_html()
''
sage: C.set_output_text('5', '<strong>5</strong>')
sage: C.output_html()
u'<strong>5</strong>'
output_text(ncols=0, html=True, raw=False, allow_interact=True)

Returns this compute cell’s output text.

INPUT:

  • ncols - an integer (default: 0); the number of word wrap columns
  • html - a boolean (default: True); whether to output HTML
  • raw - a boolean (default: False); whether to output raw text (takes precedence over HTML)
  • allow_interact - a boolean (default: True); whether to allow sagenb.notebook.interact.interact()ion

OUTPUT:

  • a string

EXAMPLES:

sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir(ext='.sagenb'))
sage: nb.user_manager().add_user('sage','sage','sage@sagemath.org',force=True)
sage: W = nb.create_new_worksheet('Test', 'sage')
sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', W)
sage: C.output_text()
u'<pre class="shrunk">5</pre>'
sage: C.output_text(html=False)
u'<pre class="shrunk">5</pre>'
sage: C.output_text(raw=True)
u'5'
sage: C = sagenb.notebook.cell.Cell(0, 'ěščřžýáíéďĎ', 'ěščřžýáíéďĎ', W)
sage: C.output_text()
u'<pre class="shrunk">ěščřž\xfd\xe1\xed\xe9ďĎ</pre>'
sage: C.output_text(raw=True)
u'ěščřž\xfd\xe1\xed\xe9ďĎ'
parse_html(s, ncols)

Parses HTML for output, escaping and wrapping HTML and removing script elements.

INPUT:

  • s - a string; the HTML to parse
  • ncols - an integer; the number of word wrap columns

OUTPUT:

  • a string

EXAMPLES:

sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir(ext='.sagenb'))
sage: nb.user_manager().add_user('sage','sage','sage@sagemath.org',force=True)
sage: W = nb.create_new_worksheet('Test', 'sage')
sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', W)
sage: C.parse_html('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">\n<html><head></head><body>Test</body></html>', 80)
'&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0...Test</body>'
parse_percent_directives()

Parses this compute cell’s percent directives, determines its system (if any), and returns the “cleaned” input text.

OUTPUT:

  • a string

EXAMPLES:

sage: C = sagenb.notebook.cell.Cell(0, '%hide\n%maxima\n%pi+3', '5', None)
sage: C.parse_percent_directives()
u'%pi+3'
sage: C.percent_directives()
[u'hide', u'maxima']
percent_directives()

Returns a list of this compute cell’s percent directives.

OUTPUT:

  • a list of strings

EXAMPLES:

sage: C = sagenb.notebook.cell.Cell(0, '%hide\n%maxima\n2+3', '5', None)
sage: C.percent_directives()
[u'hide', u'maxima']
plain_text(ncols=0, prompts=True, max_out=None)

Returns the plain text version of this compute cell.

INPUT:

  • ncols - an integer (default: 0); the number of word wrap columns
  • prompts - a boolean (default: False); whether to strip interpreter prompts from the beginning of each line
  • max_out - an integer (default: None); the maximum number of characters to return

OUTPUT:

  • plaintext_output - Plaintext string of the cell

EXAMPLES:

sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', None)
sage: len(C.plain_text())
11
process_cell_urls(urls)

Processes this compute cell’s 'cell://.*?' URLs, replacing the protocol with the cell’s path and appending the version number to prevent cached copies from shadowing the updated copy.

INPUT:

  • urls - a string; the URLs to process

OUTPUT:

  • a string

EXAMPLES:

sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir(ext='.sagenb'))
sage: nb.user_manager().add_user('sage','sage','sage@sagemath.org',force=True)
sage: W = nb.create_new_worksheet('Test', 'sage')
sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', W)
sage: C.process_cell_urls('"cell://foobar"')
'/home/sage/0/cells/0/foobar?...'
sage()

Returns the sage instance for this compute cell(?).

OUTPUT:

  • an instance of sage

EXAMPLES:

sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', None)
sage: C.sage() is None
True
set_asap(asap)

Sets whether to evaluate this compute cell as soon as possible (ASAP).

INPUT:

  • asap - a boolean convertible

EXAMPLES:

sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', None)
sage: C.is_asap()
False
sage: C.set_asap(True)
sage: C.is_asap()
True
set_cell_output_type(typ='wrap')

Sets this compute cell’s output type.

INPUT:

  • typ - a string (default: ‘wrap’); the target output type

EXAMPLES:

sage: C = sagenb.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'
set_changed_input_text(new_text)

Updates this compute cell’s changed input text. Note: This does not update the version of the cell. It’s typically used, e.g., for tab completion.

INPUT:

  • new_text - a string; the new changed input text

EXAMPLES:

sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', None)
sage: C.set_changed_input_text('3+3')
sage: C.input_text()
u'3+3'
sage: C.changed_input_text()
u'3+3'
set_input_text(input)

Sets the input text of this compute cell.

INPUT:

  • input - a string; the new input text

TODO: Add doctests for the code dealing with interact.

EXAMPLES:

sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir(ext='.sagenb'))
sage: nb.user_manager().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)     # random output -- depends on computer speed
('d', Cell 1: in=2^2, out=
4
)
sage: initial_version=C.version()
sage: C.set_input_text('3+3')
sage: C.input_text()
u'3+3'
sage: C.evaluated()
False
sage: C.version()-initial_version
1
sage: W.quit()
sage: nb.delete()
set_introspect(before_prompt, after_prompt)

Set this compute cell’s introspection text.

INPUT:

  • before_prompt - a string
  • after_prompt - a string

EXAMPLES:

sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', None)
sage: C.set_introspect("a", "b")
sage: C.introspect()
['a', 'b']
set_introspect_html(html, completing=False, raw=False)

Sets this compute cell’s introspection text.

INPUT:

  • html - a string; the updated text
  • completing - a boolean (default: False); whether the completions menu is open
  • raw - a boolean (default: False)

EXAMPLES:

sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir(ext='.sagenb'))
sage: nb.user_manager().add_user('sage','sage','sage@sagemath.org',force=True)
sage: W = nb.create_new_worksheet('Test', 'sage')
sage: C = sagenb.notebook.cell.Cell(0, 'sage?', '', W)
sage: C.introspect()
False
sage: C.evaluate(username='sage')
sage: W.check_comp(9999)     # random output -- depends on computer speed
('d', Cell 0: in=sage?, out=)
sage: C.set_introspect_html('foobar')
sage: C.introspect_html()
u'foobar'
sage: C.set_introspect_html('`foobar`')
sage: C.introspect_html()
u'`foobar`'
sage: C.set_introspect_html('ěščřžýáíéďĎ')
sage: C.introspect_html()
u'ěščřž\xfd\xe1\xed\xe9ďĎ'
sage: W.quit()
sage: nb.delete()
set_no_output(no_output)

Sets whether this is a “no output” compute cell, i.e., we don’t care about its output.

INPUT:

  • no_output - a boolean convertible

EXAMPLES:

sage: C = sagenb.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
set_output_text(output, html, sage=None)

Sets this compute cell’s output text.

INPUT:

  • output - a string; the updated output text
  • html - a string; updated output HTML
  • sage - a sage instance (default: None); the sage instance to use for this cell(?)

EXAMPLES:

sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', None)
sage: len(C.plain_text())
11
sage: C.set_output_text('10', '10')
sage: len(C.plain_text())
12
stop_interacting()

Stops sagenb.notebook.interact.interact()ion for this compute cell.

TODO: Add doctests.

system()

Returns the system used to evaluate this compute cell. The system is specified by a percent directive like ‘%maxima’ at the top of a cell.

Returns None, if no system is explicitly specified. In this case, the notebook evaluates the cell using the worksheet’s default system.

OUTPUT:

  • a string

EXAMPLES:

sage: C = sagenb.notebook.cell.Cell(0, '%maxima\n2+3', '5', None)
sage: C.system()
u'maxima'
sage: prefixes = ['%hide', '%time', '']
sage: cells = [sagenb.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]
[]
time()

Returns whether to print timing information about the evaluation of this compute cell.

OUTPUT:

  • a boolean

EXAMPLES:

sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', None)
sage: C.time()
False
sage: C = sagenb.notebook.cell.Cell(0, '%time\n2+3', '5', None)
sage: C.time()
True
unset_introspect()

Clears this compute cell’s introspection text.

EXAMPLES:

sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir(ext='.sagenb'))
sage: nb.user_manager().add_user('sage','sage','sage@sagemath.org',force=True)
sage: W = nb.create_new_worksheet('Test', 'sage')
sage: C = sagenb.notebook.cell.Cell(0, 'sage?', '', W)
sage: C.introspect()
False
sage: C.evaluate(username='sage')
sage: W.check_comp(9999)     # random output -- depends on computer speed
('d', Cell 0: in=sage?, out=)
sage: C.introspect()
[u'sage?', '']
sage: C.unset_introspect()
sage: C.introspect()
False
sage: W.quit()
sage: nb.delete()
update_html_output(output='')

Updates this compute cell’s the file list with HTML-style links or embeddings.

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.

INPUT:

  • output - a string (default: ‘’); the new output

EXAMPLES:

sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir(ext='.sagenb'))
sage: nb.user_manager().add_user('sage','sage','sage@sagemath.org',force=True)
sage: W = nb.create_new_worksheet('Test', 'sage')
sage: C = sagenb.notebook.cell.Cell(0, 'plot(sin(x),0,5)', '', W)
sage: C.evaluate()
sage: W.check_comp(wait=9999)     # random output -- depends on computer speed
('d', Cell 0: in=plot(sin(x),0,5), out=
<html><font color='black'><img src='cell://sage0.png'></font></html>

)
sage: C.update_html_output()
sage: C.output_html()     # random output -- depends on computer speed
'<img src="/home/sage/0/cells/0/sage0.png?...">'
sage: W.quit()
sage: nb.delete()
url_to_self()

Returns a notebook URL for this compute cell.

OUTPUT:

  • a string

EXAMPLES:

sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir(ext='.sagenb'))
sage: nb.user_manager().add_user('sage','sage','sage@sagemath.org',force=True)
sage: W = nb.create_new_worksheet('Test', 'sage')
sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', W)
sage: C.url_to_self()
'/home/sage/0/cells/0'
url_to_worksheet()

Returns a URL for the worksheet

OUTPUT:

  • a string

EXAMPLES:

sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir(ext='.sagenb'))
sage: nb.user_manager().add_user('sage','sage','sage@sagemath.org',force=True)
sage: W = nb.create_new_worksheet('Test', 'sage')
sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', W)
sage: C.url_to_worksheet()
'/home/sage/0'
version()

Returns this compute cell’s version number.

OUTPUT:

  • an integer

EXAMPLES:

sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', None)
sage: initial_version=C.version() #random
sage: C.set_input_text('2+3')
sage: C.version()-initial_version
1
word_wrap_cols()

Returns the number of columns for word wrapping this compute cell. This defaults to 70, but the default setting for a notebook is 72.

OUTPUT:

  • an integer

EXAMPLES:

sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', None)
sage: C.word_wrap_cols()
70
sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir(ext='.sagenb'))
sage: nb.user_manager().add_user('sage','sage','sage@sagemath.org',force=True)
sage: W = nb.create_new_worksheet('Test', 'sage')
sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', W)
sage: C.word_wrap_cols()
72
sage: nb.delete()
class sagenb.notebook.cell.Cell_generic(id, worksheet)

Bases: object

Creates a new generic cell.

INPUT:

EXAMPLES:

sage: C = sagenb.notebook.cell.Cell_generic(0, None)
sage: isinstance(C, sagenb.notebook.cell.Cell_generic)
True
sage: isinstance(C, sagenb.notebook.cell.TextCell)
False
sage: isinstance(C, sagenb.notebook.cell.Cell)
False
id()

Returns this generic cell’s ID.

OUTPUT:

  • an integer or string

EXAMPLES:

sage: C = sagenb.notebook.cell.Cell_generic(0, None)
sage: C.id()
0
sage: C = sagenb.notebook.cell.Cell('blue', '2+3', '5', None)
sage: C.id()
'blue'
sage: C = sagenb.notebook.cell.TextCell('yellow', '2+3', None)
sage: C.id()
'yellow'
is_auto_cell()

Returns whether this is an automatically evaluated generic cell. This is always false for Cell_generics and TextCells.

OUTPUT:

  • a boolean

EXAMPLES:

sage: G = sagenb.notebook.cell.Cell_generic(0, None)
sage: T = sagenb.notebook.cell.TextCell(0, 'hello!', None)
sage: [X.is_auto_cell() for X in (G, T)]
[False, False]
is_compute_cell()

Returns whether this generic cell is a compute cell, i.e., an instance of Cell.

OUTPUT:

  • a boolean

EXAMPLES:

sage: G = sagenb.notebook.cell.Cell_generic(0, None)
sage: T = sagenb.notebook.cell.TextCell(0, 'hello!', None)
sage: C = sagenb.notebook.cell.Cell(0, '2+4', '6', None)
sage: [X.is_compute_cell() for X in (G, T, C)]
[False, False, True]
is_interactive_cell()

Returns whether this generic cell uses sagenb.notebook.interact.interact() as a function call or decorator.

OUTPUT:

  • a boolean

EXAMPLES:

sage: G = sagenb.notebook.cell.Cell_generic(0, None)
sage: T = sagenb.notebook.cell.TextCell(0, 'hello!', None)
sage: [X.is_auto_cell() for X in (G, T)]
[False, False]
is_last()

Returns whether this generic cell is the last cell in its worksheet object.

OUTPUT:

  • a boolean

EXAMPLES:

sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir(ext='.sagenb'))
sage: nb.user_manager().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 2: in=2^2, out=
sage: C.is_last()
True
sage: C = W.get_cell_with_id(0)
sage: C.is_last()
False
sage: nb.delete()
is_text_cell()

Returns whether this generic cell is a text cell, i.e., an instance of TextCell.

OUTPUT:

  • a boolean

EXAMPLES:

sage: G = sagenb.notebook.cell.Cell_generic(0, None)
sage: T = sagenb.notebook.cell.TextCell(0, 'hello!', None)
sage: C = sagenb.notebook.cell.Cell(0, '2+4', '6', None)
sage: [X.is_text_cell() for X in (G, T, C)]
[False, True, False]
next_id()

Returns the ID of the next cell in this generic cell’s worksheet object. If this cell is not in the worksheet, it returns the ID of the worksheet’s first cell. If this is the last cell, it returns its own ID.

OUTPUT:

  • an integer or string

EXAMPLES:

sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir(ext='.sagenb'))
sage: nb.user_manager().add_user('sage','sage','sage@sagemath.org',force=True)
sage: W = nb.create_new_worksheet('Test', 'sage')
sage: C = W.new_cell_after(1, "2^2")
sage: C = W.get_cell_with_id(1)
sage: C.next_id()
2
sage: C = W.get_cell_with_id(2)
sage: C.next_id()
2
sage: nb.delete()
notebook()

Returns this generic cell’s associated notebook object.

OUTPUT:

EXAMPLES:

sage: nb = sagenb.notebook.notebook.load_notebook(tmp_dir(ext='.sagenb'))
sage: nb.user_manager().add_user('sage','sage','sage@sagemath.org',force=True)
sage: W = nb.create_new_worksheet('Test', 'sage')
sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', W)
sage: C.notebook() is nb
True
sage: nb.delete()
proxied_id()

Returns the ID of the cell for which this generic cell is a proxy. If this cell does not have such an ID, it returns the cell’s own ID.

EXAMPLES:

sage: C = sagenb.notebook.cell.Cell_generic('self_stand_in', None)
sage: [C.id(), C.proxied_id()]
['self_stand_in', 'self_stand_in']
set_id(id)

Sets this generic cell’s ID.

INPUT:

  • id - an integer or string; the new ID

EXAMPLES:

sage: C = sagenb.notebook.cell.Cell_generic(0, None)
sage: C.id()
0
sage: C.set_id('phone')
sage: C.id()
'phone'
set_proxied_id(proxied_id)

Sets, for this generic cell, the ID of the cell that it proxies.

INPUT:

  • proxied_id - an integer or string; the proxied cell’s ID

EXAMPLES:

sage: C = sagenb.notebook.cell.Cell_generic('understudy', None)
sage: [C.id(), C.proxied_id()]
['understudy', 'understudy']
sage: C.set_proxied_id('principal')
sage: [C.id(), C.proxied_id()]
['understudy', 'principal']
set_worksheet(worksheet, id=None)

Sets this generic cell’s worksheet object and, optionally, its ID.

INPUT:

EXAMPLES:

sage: C = sagenb.notebook.cell.Cell_generic(0, None)
sage: W = "worksheet object"
sage: C.set_worksheet(W)
sage: C.worksheet()
'worksheet object'
worksheet()

Returns this generic cell’s worksheet object.

OUTPUT:

EXAMPLES:

sage: C = sagenb.notebook.cell.Cell_generic(0, 'worksheet object')
sage: C.worksheet()
'worksheet object'
sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir(ext='.sagenb'))
sage: nb.user_manager().add_user('sage','sage','sage@sagemath.org',force=True)
sage: W = nb.create_new_worksheet('Test', 'sage')
sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', W)
sage: C.worksheet() is W
True
sage: nb.delete()
worksheet_filename()

Returns the filename of this generic cell’s worksheet object.

  • publish - a boolean (default: False); whether to render a published cell

OUTPUT:

  • a string

EXAMPLES:

sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir(ext='.sagenb'))
sage: nb.user_manager().add_user('sage','sage','sage@sagemath.org',force=True)
sage: W = nb.create_new_worksheet('Test', 'sage')
sage: C = sagenb.notebook.cell.Cell(0, '2+3', '5', W)
sage: C.worksheet_filename()
'sage/0'
sage: nb.delete()
sagenb.notebook.cell.ComputeCell

alias of Cell

class sagenb.notebook.cell.TextCell(id, text, worksheet)

Bases: sagenb.notebook.cell.Cell_generic

Creates a new text cell.

INPUT:

  • id - an integer or string; this cell’s ID
  • text - a string; this cell’s contents
  • worksheet - a sagenb.notebook.worksheet.Worksheet instance; this cell’s parent worksheet

EXAMPLES:

sage: C = sagenb.notebook.cell.TextCell(0, '2+3', None)
sage: C == loads(dumps(C))
True
delete_output()

Delete all output in this text cell. This does nothing since text cells have no output.

EXAMPLES:

sage: C = sagenb.notebook.cell.TextCell(0, '2+3', None)
sage: C
TextCell 0: 2+3
sage: C.delete_output()
sage: C
TextCell 0: 2+3
edit_text()

Returns the text to be displayed for this text cell in the Edit window.

OUTPUT:

  • a string

EXAMPLES:

sage: C = sagenb.notebook.cell.TextCell(0, '2+3', None)
sage: C.edit_text()
u'2+3'
html(wrap=None, div_wrap=True, do_print=False, editing=False, publish=False)

Returns HTML code for this text cell, including its contents and associated script elements.

INPUT:

  • wrap – an integer (default: None); number of columns to wrap at (not used)
  • div_wrap – a boolean (default: True); whether to wrap in a div (not used)
  • do_print - a boolean (default: False); whether to render the cell for printing
  • editing - a boolean (default: False); whether to open an editor for this cell

OUTPUT:

  • a string

EXAMPLES:

sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir(ext='.sagenb'))
sage: nb.user_manager().add_user('sage','sage','sage@sagemath.org',force=True)
sage: W = nb.create_new_worksheet('Test', 'sage')
sage: C = sagenb.notebook.cell.TextCell(0, '2+3', W)
sage: C.html()
u'...text_cell...2+3...'
sage: C.set_input_text("$2+3$")
plain_text(prompts=False)

Returns a plain text version of this text cell.

INPUT:

  • prompts - a boolean (default: False); whether to strip interpreter prompts from the beginning of each line

OUTPUT:

  • a string

EXAMPLES:

sage: C = sagenb.notebook.cell.TextCell(0, '2+3', None)
sage: C.plain_text()
u'2+3'
sage: C = sagenb.notebook.cell.TextCell(0, 'ěščřžýáíéďĎ', None)
sage: C.plain_text()
u'ěščřž\xfd\xe1\xed\xe9ďĎ'
set_cell_output_type(typ='wrap')

Sets this text cell’s output type. This does nothing for TextCells.

INPUT:

  • typ - a string (default: ‘wrap’); the target output type

EXAMPLES:

sage: C = sagenb.notebook.cell.TextCell(0, '2+3', None)
sage: C.set_cell_output_type("wrap")
set_input_text(input_text)

Sets the input text of this text cell.

INPUT:

  • input_text - a string; the new input text for this cell

EXAMPLES:

sage: C = sagenb.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
sagenb.notebook.cell.format_exception(s0, ncols)

Formats exceptions so they do not appear expanded by default.

INPUT:

  • s0 - a string
  • ncols - an integer; number of word wrap columns

OUTPUT:

  • a string

If s0 contains “notracebacks,” this function simply returns s0.

EXAMPLES:

sage: sagenb.notebook.cell.format_exception(sagenb.notebook.cell.TRACEBACK,80)
'\nTraceback (click to the left of this block for traceback)\n...\nTraceback (most recent call last):'
sage: sagenb.notebook.cell.format_exception(sagenb.notebook.cell.TRACEBACK + "notracebacks",80)
'Traceback (most recent call last):notracebacks'
sagenb.notebook.cell.number_of_rows(txt, ncols)

Returns the number of rows needed to display a string, given a maximum number of columns per row.

INPUT:

  • txt - a string; the text to “wrap”
  • ncols - an integer; the number of word wrap columns

OUTPUT:

  • an integer

EXAMPLES:

sage: from sagenb.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

Previous topic

Interact Functions in the Notebook

Next topic

A Worksheet

This Page