3.2 A Cell

Module: sage.server.notebook.cell

A Cell.

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

Module-level Functions

format_exception( s0, ncols)

Make it so excpetions don't appear expanded by default.

Input:

s0
- string
ncols
- integer
Output: string

If s0 contains "notracebacks" then this function always returns s0

        sage: sage.server.notebook.cell.format_exception(sage.server.notebook.cell.TRACEBACK,80)
        '
Traceback (click to the left for traceback)
...
Traceback (most recent call last):'
        sage: sage.server.notebook.cell.format_exception(sage.server.notebook.cell.TRACEBACK + "notracebacks",80)
        'Traceback (most recent call last):notracebacks'

number_of_rows( txt, ncols)

Returns the number of rows needed to display the string in txt if there are a maximum of ncols columns per row.

        sage: from sage.server.notebook.cell import number_of_rows
        sage: s = "asdfasdf
asdfasdf
"
        sage: number_of_rows(s, 8)
        2
        sage: number_of_rows(s, 5)
        4
        sage: number_of_rows(s, 4)
        4

Class: Cell

class Cell
Cell( self, id, input, out, worksheet)

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

Functions: cell_output_type,$ \,$ changed_input_text,$ \,$ computing,$ \,$ delete_output,$ \,$ directory,$ \,$ do_time,$ \,$ doc_html,$ \,$ edit_text,$ \,$ evaluate,$ \,$ evaluated,$ \,$ files,$ \,$ files_html,$ \,$ has_output,$ \,$ html,$ \,$ html_in,$ \,$ html_new_cell_after,$ \,$ html_new_cell_before,$ \,$ html_out,$ \,$ id,$ \,$ input_text,$ \,$ interrupt,$ \,$ interrupted,$ \,$ introspect,$ \,$ introspect_html,$ \,$ is_asap,$ \,$ is_auto_cell,$ \,$ is_html,$ \,$ is_interacting,$ \,$ is_interactive_cell,$ \,$ is_last,$ \,$ is_no_output,$ \,$ next_id,$ \,$ notebook,$ \,$ output_html,$ \,$ output_text,$ \,$ parse_html,$ \,$ plain_text,$ \,$ process_cell_urls,$ \,$ sage,$ \,$ set_asap,$ \,$ set_cell_output_type,$ \,$ set_changed_input_text,$ \,$ set_id,$ \,$ set_input_text,$ \,$ set_introspect,$ \,$ set_introspect_html,$ \,$ set_is_html,$ \,$ set_no_output,$ \,$ set_output_text,$ \,$ set_worksheet,$ \,$ stop_interacting,$ \,$ time,$ \,$ unset_introspect,$ \,$ update_html_output,$ \,$ url_to_self,$ \,$ version,$ \,$ word_wrap_cols,$ \,$ worksheet,$ \,$ worksheet_filename

cell_output_type( self)

Returns the cell output type.

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'

changed_input_text( self)

Returns the changed input text for the cell. If there was any changed input text, then it is reset to '' before this method returns.

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

computing( self)

Returns True if self is in its worksheet's queue.

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())

delete_output( self)

Delete all output in this cell.

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=

directory( self)

Returns the directory associated to self. If the directory doesn't already exist, then this method creates it.

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())

do_time( self)

Tells the worksheet to print the time it takes to evaluate this cell.

sage: C = sage.server.notebook.cell.Cell(0, '2+3', '5', None)
sage: C.time()
'?'
sage: C.do_time()
sage: C.time()
True

doc_html( self, [wrap=None], [div_wrap=True], [do_print=False])

Modified version of self.html for the doc browser. This is a hack and needs to be improved. The problem is how to get the documentation html to display nicely between the example cells. The type setting (jsMath formating) needs attention too.

edit_text( self, [ncols=0], [prompts=False], [max_out=None])

            sage: C = sage.server.notebook.cell.Cell(0, '2+3', '5', None)
            sage: C.edit_text()
            '{{{id=0|
2+3
///
5
}}}'

evaluate( self, [introspect=False], [time=False], [username=None])

Input:

username
- name of user doing the evaluation
time
- if True return time computation takes
introspect
- either False or a pair [before_cursor, after_cursor] of strings.

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

            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
{{{
3^5
}}}')
            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())

evaluated( self)

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:

bool
- whether or not this cell has been evaluated in this session

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
{{{
2+3
///
20
}}}')
            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())

files( self)

Returns a list of all the files in self's directory.

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=

)
sage: C.files()
['sage0.png']

sage: import shutil; shutil.rmtree(nb.directory())

has_output( self)

Returns True if there is output for this cell.

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

html_in( self, [do_print=False], [ncols=80])

Returns the HTML code for the input of this cell.

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>

html_new_cell_after( self)

Returns the HTML code for inserting a new cell after self.

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"
                   onmousedown="insert_new_cell_after(0);">
                 </div>

html_new_cell_before( self)

Returns the HTML code for inserting a new cell before self.

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"
                   onmousedown="insert_new_cell_before(0);">
                 </div>

id( self)

Returns the id of self.

sage: C = sage.server.notebook.cell.Cell(0, '2+3', '5', None)
sage: C.id()
0

input_text( self)

Returns self's input text.

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

interrupt( self)

Record that the calculation running in this cell was interrupted.

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())

interrupted( self)

Returns True if the evaluation of this cell has been interrupted.

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())

introspect( self)

TODO: Figure out what the __introspect method is for and write a better doctest.

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']

is_asap( self)

Return True if this is an asap cell, i.e., evaluation of it is done as soon as possible.

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

is_auto_cell( self)

Returns True if self is an auto cell..

An auto cell is a cell that is automatically evaluated when the worksheet starts up.

            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
2+3', '5', None)
            sage: C.is_auto_cell()
            True

is_html( self)

Returns True if this is an HTML cell. An HTML cell whose system is HTML and is typically specified by '

All of the code for determining the system is in worksheet.py in the function check_for_system_switching.

            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, "%html
Test HTML")
            sage: C.is_html()
            False
            sage: W.check_for_system_switching(C.input_text(), C)
            (True,
            "print _support_.syseval(html, ur'''Test HTML''', '...')")
            sage: C.is_html()
            True

sage: import shutil; shutil.rmtree(nb.directory())

is_interacting( self)

Returns True

is_interactive_cell( self)

Return True if this cell contains the use of interact either as a function call or a decorator.

            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
def f(a=slider(0,10,1,5):
    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())

is_last( self)

Returns True if self is the last cell in the worksheet.

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())

is_no_output( self)

Return True if this is an no_output cell, i.e., a cell for which we don't care at all about the output.

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

next_id( self)

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.

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())

notebook( self)

Returns the notebook object associated to self.

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())

plain_text( self, [ncols=0], [prompts=True], [max_out=None])

Returns the plain text version of self.

TODO: Add more comprehensive doctests.

sage( self)

TODO: Figure out what exactly this does.

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

set_asap( self, asap)

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

set_cell_output_type( self, [typ=wrap])

Sets the cell output type.

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'

set_changed_input_text( self, new_text)

Note that this does not update the version of the cell. This is typically used for things like tab completion.

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'

set_id( self, id)

Sets the id of self to id.

sage: C = sage.server.notebook.cell.Cell(0, '2+3', '5', None)
sage: C.set_id(2)
sage: C.id()
2

set_input_text( self, input)

Sets the input text of self to be the string input.

TODO: Add doctests for the code dealing with interact.

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())

set_introspect( self, before_prompt, after_prompt)

TODO: Figure out what the __introspect method is for and write a better doctest.

sage: C = sage.server.notebook.cell.Cell(0, '2+3', '5', None)
sage: C.set_introspect("a", "b")
sage: C.introspect()
['a', 'b']

set_is_html( self, v)

Sets whether or not this cell is an HTML cell.

This is called by check_for_system_switching in worksheet.py.

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

set_no_output( self, no_output)

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.

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

set_worksheet( self, worksheet, [id=None])

Sets the worksheet object of self to be worksheet and optionally changes the id of self.

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

time( self)

Returns True if the time it takes to evaluate this cell should be printed.

sage: C = sage.server.notebook.cell.Cell(0, '2+3', '5', None)
sage: C.time()
'?'
sage: C.do_time()
sage: C.time()
True

unset_introspect( self)

TODO: Figure out what the __introspect method is for and write a better doctest.

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_html_output( self, [output=])

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.

url_to_self( self)

TODO: Figure out why the url starts with /home/.

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'

sage: import shutil; shutil.rmtree(nb.directory())

version( self)

Returns the version number of this cell.

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

word_wrap_cols( self)

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

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())

worksheet( self)

Returns the workseet associated to self.

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())

worksheet_filename( self)

Returns the filename of the worksheet associated to self.

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())

Special Functions: __cmp__,$ \,$ __init__,$ \,$ __repr__,$ \,$ _directory_name

__cmp__( self, right)

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

__repr__( self)

sage: C = sage.server.notebook.cell.Cell(0, '2+3', '5', None); C
Cell 0; in=2+3, out=5

_directory_name( self)

Returns a string of the directory associated to self.

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())

Class: Cell_generic

class Cell_generic

Functions: delete_output,$ \,$ is_interactive_cell

delete_output( self)

Delete all output in this cell. This is not executed - it is an abstract function that must be overwritten in a derived class.

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()
Traceback (most recent call last):
...
NotImplementedError

is_interactive_cell( self)

Returns True if this cell contains the use of interact either as a function call or a decorator.

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

Class: ComputeCell

class ComputeCell
ComputeCell( self, id, input, out, worksheet)

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

Functions: cell_output_type,$ \,$ changed_input_text,$ \,$ computing,$ \,$ delete_output,$ \,$ directory,$ \,$ do_time,$ \,$ doc_html,$ \,$ edit_text,$ \,$ evaluate,$ \,$ evaluated,$ \,$ files,$ \,$ files_html,$ \,$ has_output,$ \,$ html,$ \,$ html_in,$ \,$ html_new_cell_after,$ \,$ html_new_cell_before,$ \,$ html_out,$ \,$ id,$ \,$ input_text,$ \,$ interrupt,$ \,$ interrupted,$ \,$ introspect,$ \,$ introspect_html,$ \,$ is_asap,$ \,$ is_auto_cell,$ \,$ is_html,$ \,$ is_interacting,$ \,$ is_interactive_cell,$ \,$ is_last,$ \,$ is_no_output,$ \,$ next_id,$ \,$ notebook,$ \,$ output_html,$ \,$ output_text,$ \,$ parse_html,$ \,$ plain_text,$ \,$ process_cell_urls,$ \,$ sage,$ \,$ set_asap,$ \,$ set_cell_output_type,$ \,$ set_changed_input_text,$ \,$ set_id,$ \,$ set_input_text,$ \,$ set_introspect,$ \,$ set_introspect_html,$ \,$ set_is_html,$ \,$ set_no_output,$ \,$ set_output_text,$ \,$ set_worksheet,$ \,$ stop_interacting,$ \,$ time,$ \,$ unset_introspect,$ \,$ update_html_output,$ \,$ url_to_self,$ \,$ version,$ \,$ word_wrap_cols,$ \,$ worksheet,$ \,$ worksheet_filename

cell_output_type( self)

Returns the cell output type.

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'

changed_input_text( self)

Returns the changed input text for the cell. If there was any changed input text, then it is reset to '' before this method returns.

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

computing( self)

Returns True if self is in its worksheet's queue.

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())

delete_output( self)

Delete all output in this cell.

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=

directory( self)

Returns the directory associated to self. If the directory doesn't already exist, then this method creates it.

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())

do_time( self)

Tells the worksheet to print the time it takes to evaluate this cell.

sage: C = sage.server.notebook.cell.Cell(0, '2+3', '5', None)
sage: C.time()
'?'
sage: C.do_time()
sage: C.time()
True

doc_html( self, [wrap=None], [div_wrap=True], [do_print=False])

Modified version of self.html for the doc browser. This is a hack and needs to be improved. The problem is how to get the documentation html to display nicely between the example cells. The type setting (jsMath formating) needs attention too.

edit_text( self, [ncols=0], [prompts=False], [max_out=None])

            sage: C = sage.server.notebook.cell.Cell(0, '2+3', '5', None)
            sage: C.edit_text()
            '{{{id=0|
2+3
///
5
}}}'

evaluate( self, [introspect=False], [time=False], [username=None])

Input:

username
- name of user doing the evaluation
time
- if True return time computation takes
introspect
- either False or a pair [before_cursor, after_cursor] of strings.

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

            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
{{{
3^5
}}}')
            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())

evaluated( self)

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:

bool
- whether or not this cell has been evaluated in this session

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
{{{
2+3
///
20
}}}')
            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())

files( self)

Returns a list of all the files in self's directory.

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=

)
sage: C.files()
['sage0.png']

sage: import shutil; shutil.rmtree(nb.directory())

has_output( self)

Returns True if there is output for this cell.

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

html_in( self, [do_print=False], [ncols=80])

Returns the HTML code for the input of this cell.

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>

html_new_cell_after( self)

Returns the HTML code for inserting a new cell after self.

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"
                   onmousedown="insert_new_cell_after(0);">
                 </div>

html_new_cell_before( self)

Returns the HTML code for inserting a new cell before self.

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"
                   onmousedown="insert_new_cell_before(0);">
                 </div>

id( self)

Returns the id of self.

sage: C = sage.server.notebook.cell.Cell(0, '2+3', '5', None)
sage: C.id()
0

input_text( self)

Returns self's input text.

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

interrupt( self)

Record that the calculation running in this cell was interrupted.

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())

interrupted( self)

Returns True if the evaluation of this cell has been interrupted.

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())

introspect( self)

TODO: Figure out what the __introspect method is for and write a better doctest.

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']

is_asap( self)

Return True if this is an asap cell, i.e., evaluation of it is done as soon as possible.

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

is_auto_cell( self)

Returns True if self is an auto cell..

An auto cell is a cell that is automatically evaluated when the worksheet starts up.

            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
2+3', '5', None)
            sage: C.is_auto_cell()
            True

is_html( self)

Returns True if this is an HTML cell. An HTML cell whose system is HTML and is typically specified by '

All of the code for determining the system is in worksheet.py in the function check_for_system_switching.

            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, "%html
Test HTML")
            sage: C.is_html()
            False
            sage: W.check_for_system_switching(C.input_text(), C)
            (True,
            "print _support_.syseval(html, ur'''Test HTML''', '...')")
            sage: C.is_html()
            True

sage: import shutil; shutil.rmtree(nb.directory())

is_interacting( self)

Returns True

is_interactive_cell( self)

Return True if this cell contains the use of interact either as a function call or a decorator.

            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
def f(a=slider(0,10,1,5):
    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())

is_last( self)

Returns True if self is the last cell in the worksheet.

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())

is_no_output( self)

Return True if this is an no_output cell, i.e., a cell for which we don't care at all about the output.

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

next_id( self)

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.

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())

notebook( self)

Returns the notebook object associated to self.

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())

plain_text( self, [ncols=0], [prompts=True], [max_out=None])

Returns the plain text version of self.

TODO: Add more comprehensive doctests.

sage( self)

TODO: Figure out what exactly this does.

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

set_asap( self, asap)

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

set_cell_output_type( self, [typ=wrap])

Sets the cell output type.

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'

set_changed_input_text( self, new_text)

Note that this does not update the version of the cell. This is typically used for things like tab completion.

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'

set_id( self, id)

Sets the id of self to id.

sage: C = sage.server.notebook.cell.Cell(0, '2+3', '5', None)
sage: C.set_id(2)
sage: C.id()
2

set_input_text( self, input)

Sets the input text of self to be the string input.

TODO: Add doctests for the code dealing with interact.

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())

set_introspect( self, before_prompt, after_prompt)

TODO: Figure out what the __introspect method is for and write a better doctest.

sage: C = sage.server.notebook.cell.Cell(0, '2+3', '5', None)
sage: C.set_introspect("a", "b")
sage: C.introspect()
['a', 'b']

set_is_html( self, v)

Sets whether or not this cell is an HTML cell.

This is called by check_for_system_switching in worksheet.py.

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

set_no_output( self, no_output)

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.

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

set_worksheet( self, worksheet, [id=None])

Sets the worksheet object of self to be worksheet and optionally changes the id of self.

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

time( self)

Returns True if the time it takes to evaluate this cell should be printed.

sage: C = sage.server.notebook.cell.Cell(0, '2+3', '5', None)
sage: C.time()
'?'
sage: C.do_time()
sage: C.time()
True

unset_introspect( self)

TODO: Figure out what the __introspect method is for and write a better doctest.

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_html_output( self, [output=])

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.

url_to_self( self)

TODO: Figure out why the url starts with /home/.

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'

sage: import shutil; shutil.rmtree(nb.directory())

version( self)

Returns the version number of this cell.

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

word_wrap_cols( self)

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

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())

worksheet( self)

Returns the workseet associated to self.

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())

worksheet_filename( self)

Returns the filename of the worksheet associated to self.

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())

Special Functions: __cmp__,$ \,$ __init__,$ \,$ __repr__,$ \,$ _directory_name

__cmp__( self, right)

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

__repr__( self)

sage: C = sage.server.notebook.cell.Cell(0, '2+3', '5', None); C
Cell 0; in=2+3, out=5

_directory_name( self)

Returns a string of the directory associated to self.

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())

Class: TextCell

class TextCell
TextCell( self, id, text, worksheet)

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

Functions: delete_output,$ \,$ edit_text,$ \,$ html,$ \,$ id,$ \,$ is_auto_cell,$ \,$ plain_text,$ \,$ set_cell_output_type,$ \,$ set_input_text,$ \,$ set_worksheet,$ \,$ worksheet

delete_output( self)

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

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

edit_text( self)

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

html( self, ncols, [do_print=False], [do_math_parse=True])

Returns an HTML version of self as a string.

Input:

do_math_parse
- bool (default: True) If True, call math_parse (defined in cell.py) on the html.

sage: C = sage.server.notebook.cell.TextCell(0, '2+3', None)
sage: C.html(80)
'<div><font size=+1>2+3</font></div>'
sage: C.set_input_text("$2+3$")
sage: C.html(80, do_math_parse=True)
'<div><font size=+1><span class="math">2+3</span></font></div>'

id( self)

sage: C = sage.server.notebook.cell.TextCell(0, '2+3', None)
sage: C.id()
0

is_auto_cell( self)

sage: C = sage.server.notebook.cell.TextCell(0, '2+3', None)
sage: C.is_auto_cell()
False

plain_text( self, [prompts=False])

sage: C = sage.server.notebook.cell.TextCell(0, '2+3', None)
sage: C.plain_text()
'2+3'

set_cell_output_type( self, [typ=wrap])

This does nothing for TextCells.

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

set_input_text( self, input_text)

Sets the input text of self to be input_text.

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

set_worksheet( self, worksheet, [id=None])

Sets the worksheet object of self to be worksheet and optionally changes the id of self.

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

worksheet( self)

Returns the worksheet object associated to self.

sage: C = sage.server.notebook.cell.TextCell(0, '2+3', 'worksheet object')
sage: C.worksheet()
'worksheet object'

Special Functions: __cmp__,$ \,$ __init__,$ \,$ __repr__

__cmp__( self, right)

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

__repr__( self)

String representation of this text cell.

sage: C = sage.server.notebook.cell.TextCell(0, '2+3', None)
sage: C.__repr__()
'TextCell 0: 2+3'

See About this document... for information on suggesting changes.