A worksheet is embedded in a web page that is served by the Sage server. It is a linearly-ordered collections of numbered cells, where a cell is a single input/output block.
The worksheet module is responsible for running calculations in a worksheet, spawning Sage processes that do all of the actual work and are controlled via pexpect, and reporting on results of calculations. The state of the cells in a worksheet is stored on the filesystem (not in the notebook pickle sobj).
AUTHORS:
Return the absolute path to the directory that contains the Sage Notebook directory for the notebook that contains this worksheet.
OUTPUT: string
EXAMPLES:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: W = nb.create_new_worksheet('A Test Worksheet', 'admin')
sage: W.DIR() # random output
'/Users/was/.sage/temp/teragon_2.local/19129'
We compare two worksheets.
INPUT:
OUTPUT:
EXAMPLES:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: W2 = nb.create_new_worksheet('test2', 'admin')
sage: W1 = nb.create_new_worksheet('test1', 'admin')
sage: cmp(W1, W2)
1
sage: cmp(W2, W1)
-1
The getstate method makes sure that the self.__cells dictionary is not saved in the pickle since it could be huge.
OUTPUT: a dictionary; same as self.__dict__ but with some fields deleted.
EXAMPLES:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: W = nb.create_new_worksheet('Test Edit Save', 'admin')
sage: v = W.__getstate__().keys(); v.sort(); v
['_Worksheet__autopublish', '_Worksheet__collaborators', '_Worksheet__comp_is_running', '_Worksheet__dir', '_Worksheet__docbrowser', '_Worksheet__filename', '_Worksheet__name', '_Worksheet__next_id', '_Worksheet__owner', '_Worksheet__pretty_print', '_Worksheet__queue', '_Worksheet__saved_by_info', '_Worksheet__system', '_Worksheet__viewers']
Create and initialize a new worksheet.
INPUT:
EXAMPLES: We test the constructor via an indirect doctest:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: W = nb.create_new_worksheet('Test', 'admin')
sage: W
[Cell 0; in=, out=]
Return the number of cells in this worksheet.
OUTPUT: int
EXAMPLES:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: W = nb.create_new_worksheet('test1', 'admin')
sage: len(W)
1
sage: W.edit_save('Sage\n{{{\n2+3\n///\n5\n}}}\n{{{id=10|\n2+8\n///\n10\n}}}')
sage: len(W)
2
Return string representation of this worksheet, which is simply the string representation of the underlying list of cells.
OUTPUT: string
EXAMPLES:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: W = nb.create_new_worksheet('test1', 'admin')
sage: W.__repr__()
'[Cell 0; in=, out=]'
sage: W.edit_save('Sage\n{{{\n2+3\n///\n5\n}}}\n{{{id=10|\n2+8\n///\n10\n}}}')
sage: W.__repr__()
'[Cell 0; in=2+3, out=\n5, Cell 10; in=2+8, out=\n10]'
Add the given user as a collaborator on this worksheet.
INPUT:
EXAMPLES:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: nb.add_user('diophantus','sage','sage@sagemath.org',force=True)
sage: W = nb.create_new_worksheet('Collaborator test', 'admin')
sage: W.collaborators()
[]
sage: W.add_collaborator('diophantus')
sage: W.collaborators()
['diophantus']
Add the given user as an allowed viewer of this worksheet.
INPUT:
EXAMPLES:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: nb.add_user('diophantus','sage','sage@sagemath.org',force=True)
sage: W = nb.create_new_worksheet('Viewer test', 'admin')
sage: W.add_viewer('diophantus')
sage: W.viewers()
['diophantus']
Create and append a new cell to the list of cells.
OUTPUT: a new empty cell
EXAMPLES:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: W = nb.create_new_worksheet('Test Edit Save', 'admin')
sage: W
[Cell 0; in=, out=]
sage: W.append_new_cell()
Cell 1; in=, out=
sage: W
[Cell 0; in=, out=, Cell 1; in=, out=]
Return a list of the filenames of files in the worksheet data directory.
OUTPUT: list of strings
EXAMPLES:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: W = nb.create_new_worksheet('A Test Worksheet', 'admin')
sage: W.attached_data_files()
[]
sage: open('%s/foo.data'%W.data_directory(),'w').close()
sage: W.attached_data_files()
['foo.data']
Return a new list of the id’s of cells in this worksheet.
OUTPUT: a new list
EXAMPLES:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: W = nb.create_new_worksheet('Test Edit Save', 'admin')
Now we set the worksheet to have two cells with the default id of 0 and another with id 10.
sage: W.edit_save('Sage\n{{{\n2+3\n///\n5\n}}}\n{{{id=10|\n2+8\n///\n10\n}}}')
sage: W.cell_id_list()
[0, 10]
Return a reference to the list of the all the cells in this worksheet.
OUTPUT:
Note
This function loads the cell list from disk (the file worksheet.txt) if it isn’t available in memory.
EXAMPLES:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: W = nb.create_new_worksheet('Test Edit Save', 'admin')
sage: W.edit_save('Sage\n{{{\n2+3\n///\n5\n}}}\n{{{\n2+8\n///\n10\n}}}')
sage: v = W.cell_list(); v
[Cell 0; in=2+3, out=
5, Cell 1; in=2+8, out=
10]
sage: v[0]
Cell 0; in=2+3, out=
5
Return the directory in which the cells of this worksheet are evaluated.
OUTPUT: string
EXAMPLES:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: W = nb.create_new_worksheet('A Test Worksheet', 'admin')
sage: W.cells_directory()
'.../worksheets/admin/0/cells/'
Check the status on computation of the cell with given id.
INPUT:
OUTPUT:
Check on currently computing cells in the queue.
INPUT:
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: W.edit_save('Sage\n{{{\n3^20\n}}}')
sage: W.cell_list()[0].evaluate()
sage: W.check_comp() # random output -- depends on computer speed
('d', Cell 0; in=3^20, out=
3486784401
)
sage: nb.delete()
Check for input cells that start with %foo, where foo is an object with an eval method.
INPUT:
EXAMPLES: First, we set up a new notebook and 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')
We first test running a native command in ‘sage’ mode and then a GAP cell within Sage mode.
sage: W.edit_save('Sage\nsystem:sage\n{{{\n2+3\n}}}\n\n{{{\n%gap\nSymmetricGroup(5)\n}}}')
sage: c0, c1 = W.cell_list()
sage: W.check_for_system_switching(c0.cleaned_input_text(), c0)
(False, '2+3')
sage: W.check_for_system_switching(c1.cleaned_input_text(), c1)
(True, "print _support_.syseval(gap, ur'''SymmetricGroup(5)''', '...')")
sage: c0.evaluate()
sage: W.check_comp() #random output -- depends on the computer's speed
('d', Cell 0; in=2+3, out=
5
)
sage: c1.evaluate()
sage: W.check_comp() #random output -- depends on the computer's speed
('d', Cell 1; in=%gap
SymmetricGroup(5), out=
Sym( [ 1 .. 5 ] )
)
Next, we run the same commands but from ‘gap’ mode.
sage: W.edit_save('Sage\nsystem:gap\n{{{\n%sage\n2+3\n}}}\n\n{{{\nSymmetricGroup(5)\n}}}')
sage: c0, c1 = W.cell_list()
sage: W.check_for_system_switching(c0.cleaned_input_text(), c0)
(False, '2+3')
sage: W.check_for_system_switching(c1.cleaned_input_text(), c1)
(True,
"print _support_.syseval(gap, ur'''SymmetricGroup(5)''', '...')")
sage: c0.evaluate()
sage: W.check_comp() #random output -- depends on the computer's speed
('d', Cell 0; in=%sage
2+3, out=
5
)
sage: c1.evaluate()
sage: W.check_comp() #random output -- depends on the computer's speed
('d', Cell 1; in=SymmetricGroup(5), out=
Sym( [ 1 .. 5 ] )
)
Returns a string of the non-owner collaborators on this worksheet.
INPUT:
EXAMPLES:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: W = nb.create_new_worksheet('test1', 'admin')
sage: C = W.collaborators(); C
[]
sage: C.append('sage')
sage: C.append('wstein')
sage: W.collaborator_names()
'sage, wstein'
sage: W.collaborator_names(max=1)
'sage, ...'
Return a (reference to the) list of the collaborators who can also view and modify this worksheet.
OUTPUT: list
EXAMPLES:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: W = nb.create_new_worksheet('test1', 'admin')
sage: C = W.collaborators(); C
[]
sage: C.append('sage')
sage: W.collaborators()
['sage']
Return the configuration object for this worksheet, which is stored in an sobj in the worksheet directory.
OUTPUT: worksheet configuration object.
EXAMPLES:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: W = nb.create_new_worksheet('test1', 'admin')
sage: W.conf()
Configuration: {}
Return path to directory where worksheet data is stored.
OUTPUT: string
EXAMPLES:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: W = nb.create_new_worksheet('A Test Worksheet', 'admin')
sage: W.data_directory()
'.../worksheets/admin/0/data/'
Delete all the output in all the worksheet cells.
INPUT:
EXAMPLES: We create a new notebook, user, and a worksheet with one cell.
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\nsystem:sage\n{{{\n2+3\n///\n5\n}}}')
Notice that there is 1 cell with 5 in its output.
sage: W.cell_list()
[Cell 0; in=2+3, out=
5]
We now delete the output, observe that it is gone.
sage: W.delete_all_output('sage')
sage: W.cell_list()
[Cell 0; in=2+3, out=]
If an invalid user tries to delete all, a ValueError is raised.
sage: W.delete_all_output('hacker')
...
ValueError: user 'hacker' not allowed to edit this worksheet
Clean up.
sage: nb.delete()
Delete the directory in which all the cell computations occur.
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: W.edit_save('Sage\n{{{\n3^20\n}}}')
sage: sorted(os.listdir(W.directory()))
['snapshots', 'worksheet.txt']
sage: W.cell_list()[0].evaluate()
sage: sorted(os.listdir(W.directory()))
['cells', 'code', 'data', 'snapshots', 'worksheet.txt']
sage: W.delete_cells_directory()
sage: sorted(os.listdir(W.directory()))
['code', 'data', 'snapshots', 'worksheet.txt']
Delete data from this worksheet this is specific to a certain notebook. This means deleting the attached files, collaborators, and viewers.
EXAMPLES:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: nb.add_user('hilbert','sage','sage@sagemath.org',force=True)
sage: W = nb.create_new_worksheet('test1', 'admin')
sage: W.add_viewer('hilbert')
sage: W.delete_notebook_specific_data()
sage: W.viewers()
[]
sage: W.add_collaborator('hilbert')
sage: W.collaborators()
['admin', 'hilbert']
sage: W.delete_notebook_specific_data()
sage: W.collaborators()
['admin']
Delete a user from having any view or ownership of this worksheet.
INPUT:
EXAMPLES: We create a notebook with 2 users and 1 worksheet that both view.
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: nb.add_user('wstein','sage','wstein@sagemath.org',force=True)
sage: nb.add_user('sage','sage','sage@sagemath.org',force=True)
sage: W = nb.new_worksheet_with_title_from_text('Sage', owner='sage')
sage: W.add_viewer('wstein')
sage: W.owner()
'sage'
sage: W.viewers()
['wstein']
We delete the sage user from the worksheet W. This makes wstein the new owner.
sage: W.delete_user('sage')
sage: W.viewers()
['wstein']
sage: W.owner()
'wstein'
Then we delete wstein from W, which makes the owner None:
sage: W.delete_user('wstein')
sage: W.owner() is None
True
sage: W.viewers()
[]
Finally, we clean up.
sage: nb.delete()
Return the full path to the directory where this worksheet is stored.
OUTPUT: string
EXAMPLES:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: W = nb.create_new_worksheet('A Test Worksheet', 'admin')
sage: W.directory()
'.../worksheets/admin/0'
Return True if this is a docbrowser worksheet.
OUTPUT: bool
EXAMPLES: We first create a standard worksheet for which docbrowser is of course False:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: W = nb.create_new_worksheet('test1', 'admin')
sage: W.docbrowser()
False
We create a worksheet for which docbrowser is True:
sage: W = nb.create_new_worksheet('docs', 'admin', docbrowser=True)
sage: W.docbrowser()
True
Set the contents of this worksheet to the worksheet defined by the plain text string text, which should be a sequence of html and ‘s code blocks.
INPUT:
EXAMPLES: We create a new test notebook and a 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 Edit Save', 'sage')
We set the contents of the worksheet using the edit_save command.
sage: W.edit_save('Sage\n{{{\n2+3\n///\n5\n}}}\n{{{\n2+8\n///\n10\n}}}')
sage: W
[Cell 0; in=2+3, out=
5, Cell 1; in=2+8, out=
10]
sage: W.name()
'Sage'
Queue up the cell C for evaluation in this worksheet.
INPUT:
Note
If C.is_asap() is True, then we put C as close to the beginning of the queue as possible, but after all asap cells. Otherwise, C goes at the end of the queue.
Return True if all users have deleted this worksheet, so we know we can safely purge it from disk.
OUTPUT: bool
EXAMPLES:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: W = nb.create_new_worksheet('Publish Test', 'admin')
sage: W.everyone_has_deleted_this_worksheet()
False
sage: W.move_to_trash('admin')
sage: W.everyone_has_deleted_this_worksheet()
True
Return the filename (really directory) where the files associated to this worksheet are stored.
EXAMPLES:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: W = nb.create_new_worksheet('A Test Worksheet', 'admin')
sage: W.filename()
'admin/0'
sage: sorted(os.listdir(nb.directory() + '/worksheets/' + W.filename()))
['snapshots', 'worksheet.txt']
Return the part of the worksheet filename after the last /, i.e., without any information about the owner of this worksheet.
EXAMPLES:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: W = nb.create_new_worksheet('A Test Worksheet', 'admin')
sage: W.filename_without_owner()
'0'
sage: W.filename()
'admin/0'
Returns the system that will run the input in cell. This defaults to worksheet’s system if there is not one specifically given in the 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: W.edit_save('Sage\nsystem:sage\n{{{\n2+3\n}}}\n\n{{{\n%gap\nSymmetricGroup(5)\n}}}')
sage: c0, c1 = W.cell_list()
sage: W.get_cell_system(c0)
'sage'
sage: W.get_cell_system(c1)
'gap'
sage: W.edit_save('Sage\nsystem:gap\n{{{\n%sage\n2+3\n}}}\n\n{{{\nSymmetricGroup(5)\n}}}')
sage: c0, c1 = W.cell_list()
sage: W.get_cell_system(c0)
'sage'
sage: W.get_cell_system(c1)
'gap'
Return True if there is a published version of this worksheet.
OUTPUT: bool
EXAMPLES:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: W = nb.create_new_worksheet('Publish Test', 'admin')
sage: P = nb.publish_worksheet(W, 'admin')
sage: P.has_published_version()
False
sage: W.has_published_version()
True
Return html that renders to give a summary of how this worksheet has been rated.
OUTPUT:
EXAMPLES:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: W = nb.create_new_worksheet('Publish Test', 'admin')
sage: W.rate(0, 'this lacks content', 'riemann')
sage: W.rate(3, 'this is great', 'hilbert')
sage: W.html_ratings_info()
'<tr><td>hilbert</td><td align=center>3</td><td>this is great</td></tr>\n<tr><td>riemann</td><td align=center>0</td><td>this lacks content</td></tr>'
Interrupt all currently queued up calculations.
OUTPUT:
EXAMPLES: We create a worksheet and start a large factorization going:
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{{{\nfactor(2^997-1)\n}}}')
sage: W.cell_list()[0].evaluate()
It’s running still
sage: W.check_comp()
('w', Cell 0; in=factor(2^997-1), out=...)
We interrupt it successfully.
sage: W.interrupt() # random -- could fail on heavily loaded machine
True
Now we check and nothing is computing.
sage: W.check_comp() # random -- could fail on heavily loaded machine
('e', None)
Clean up.
sage: nb.delete()
Return True if this worksheet is active for the given user.
INPUT:
OUTPUT: bool
EXAMPLES:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: W = nb.create_new_worksheet('Active Test', 'admin')
sage: W.is_active('admin')
True
sage: W.move_to_archive('admin')
sage: W.is_active('admin')
False
Return True if this worksheet is archived for the given user.
INPUT:
OUTPUT: bool
EXAMPLES:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: W = nb.create_new_worksheet('Archived Test', 'admin')
sage: W.is_archived('admin')
False
sage: W.move_to_archive('admin')
sage: W.is_archived('admin')
True
Return True if this worksheet is a published worksheet.
OUTPUT:
EXAMPLES:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: W = nb.create_new_worksheet('Publish Test', 'admin')
sage: W.is_published()
False
sage: W.set_owner('pub')
sage: W.is_published()
True
Return True if username is the username of the publisher of this worksheet, assuming this worksheet was published.
INPUT:
EXAMPLES:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: W = nb.create_new_worksheet('Publish Test', 'admin')
sage: P = nb.publish_worksheet(W, 'admin')
sage: P.is_publisher('hearst')
False
sage: P.is_publisher('admin')
True
Return True is the user with given username has rated this worksheet.
INPUT:
OUTPUT: bool
EXAMPLES:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: W = nb.create_new_worksheet('Publish Test', 'admin')
sage: W.rate(0, 'this lacks content', 'riemann')
sage: W.is_rater('admin')
False
sage: W.is_rater('riemann')
True
Return True if this worksheet is in the trash for the given user.
INPUT:
OUTPUT: bool
EXAMPLES:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: W = nb.create_new_worksheet('Trash Test', 'admin')
sage: W.is_trashed('admin')
False
sage: W.move_to_trash('admin')
sage: W.is_trashed('admin')
True
This routine will limit the number of snapshots of a worksheet, as specified by a hard-cioded value below.
Prior behavior was to allow unlimited numbers of snapshots and so this routine will not delete files created prior to this change.
This assumes snapshot names correspond to the time.time() method used to create base filenames in seconds in UTC time, and that there are no other extraneous files in the directory.
Exactly the same as set_active(user).
INPUT:
EXAMPLES:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: W = nb.create_new_worksheet('Active Test', 'admin')
sage: W.move_to_trash('admin')
sage: W.is_active('admin')
False
sage: W.move_out_of_trash('admin')
sage: W.is_active('admin')
True
Move this worksheet to be archived for the given user.
INPUT:
EXAMPLES:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: W = nb.create_new_worksheet('Archive Test', 'admin')
sage: W.move_to_archive('admin')
sage: W.is_archived('admin')
True
Move this worksheet to the trash for the given user.
INPUT:
EXAMPLES:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: W = nb.create_new_worksheet('Trash Test', 'admin')
sage: W.move_to_trash('admin')
sage: W.is_trashed('admin')
True
Return the name of this worksheet.
OUTPUT: string
EXAMPLES:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: W = nb.create_new_worksheet('A Test Worksheet', 'admin')
sage: W.name()
'A Test Worksheet'
Insert a new cell into the cell list after the cell with the given integer id.
INPUT:
- id - integer
- input - string
OUTPUT:
A new cell with the given input text (empty by default).
Insert a new cell into the cell list before the cell with the given integer id. If the id is not the id of any cell, inserts a new cell at the end of the cell list.
INPUT:
OUTPUT:
A new cell with the given input text (empty by default).
Insert a new cell into the cell list after the cell with the given integer id. If the id is not the id of any cell, inserts a new cell at the end of the cell list.
INPUT:
OUTPUT:
A new cell with the given input text (empty by default).
Insert a new cell into the cell list before the cell with the given integer id. If the id is not the id of any cell, inserts a new cell at the end of the cell list.
INPUT:
OUTPUT:
A new cell with the given input text (empty by default).
Return the notebook that contains this worksheet.
OUTPUT: a Notebook object.
EXAMPLES: This really returns the Notebook object that is set as a global variable of the twist module.
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: W = nb.create_new_worksheet('A Test Worksheet', 'admin')
sage: W.notebook()
<class 'sage.server.notebook.notebook.Notebook'>
sage: W.notebook() is sage.server.notebook.twist.notebook
True
Return a plain-text version of the worksheet.
INPUT:
Return True if output should be pretty printed by default.
OUTPUT:
EXAMPLES:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: W = nb.create_new_worksheet('A Test Worksheet', 'admin')
sage: W.pretty_print()
False
sage: W.set_pretty_print('true')
sage: W.pretty_print()
True
If this worksheet was published, return the published version of this worksheet. Otherwise, raise a ValueError.
OUTPUT: a worksheet (or raise a ValueError)
EXAMPLES:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: W = nb.create_new_worksheet('Publish Test', 'admin')
sage: P = nb.publish_worksheet(W, 'admin')
sage: W.published_version() is P
True
Return username of user that published this worksheet.
OUTPUT: string
EXAMPLES:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: W = nb.create_new_worksheet('Publish Test', 'admin')
sage: S = nb.publish_worksheet(W, 'admin')
sage: S.publisher()
'admin'
Set the rating on this worksheet by the given user to x and also set the given comment.
INPUT:
EXAMPLES: We create a worksheet and rate it, then look at the ratings.
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: W = nb.create_new_worksheet('Publish Test', 'admin')
sage: W.rate(3, 'this is great', 'hilbert')
sage: W.ratings()
[('hilbert', 3, 'this is great')]
Note that only the last rating by a user counts:
sage: W.rate(1, 'this lacks content', 'riemann')
sage: W.rate(0, 'this lacks content', 'riemann')
sage: W.ratings()
[('hilbert', 3, 'this is great'), ('riemann', 0, 'this lacks content')]
Return overall aerage rating of self.
OUTPUT: float or the int -1 to mean “not rated”
EXAMPLES:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: W = nb.create_new_worksheet('Publish Test', 'admin')
sage: W.rating()
-1
sage: W.rate(0, 'this lacks content', 'riemann')
sage: W.rate(3, 'this is great', 'hilbert')
sage: W.rating()
1.5
Return all the ratings of this worksheet.
OUTPUT:
EXAMPLES:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: W = nb.create_new_worksheet('Publish Test', 'admin')
sage: W.ratings()
[]
sage: W.rate(0, 'this lacks content', 'riemann')
sage: W.rate(3, 'this is great', 'hilbert')
sage: W.ratings()
[('riemann', 0, 'this lacks content'), ('hilbert', 3, 'this is great')]
Return a started up copy of Sage initialized for computations.
If this is a published worksheet, just return None, since published worksheets must not have any compute functionality.
OUTPUT: a Sage interface
Return True if all words in search are in the saved text of the worksheet.
INPUT: search is a string that describes a search query, i.e., a space-separated collections of words.
OUTPUT: True if the search is satisfied by self, i.e., all the words appear in the text version of self.
Set his worksheet to be active for the given user.
INPUT:
EXAMPLES:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: W = nb.create_new_worksheet('Active Test', 'admin')
sage: W.move_to_archive('admin')
sage: W.is_active('admin')
False
sage: W.set_active('admin')
sage: W.is_active('admin')
True
Set the list of collaborators to those listed in the list v of strings.
INPUT:
EXAMPLES:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: nb.add_user('sage','sage','sage@sagemath.org',force=True)
sage: nb.add_user('hilbert','sage','sage@sagemath.org',force=True)
sage: W = nb.create_new_worksheet('test1', 'admin')
sage: W.set_collaborators(['sage', 'admin', 'hilbert', 'sage'])
Note that repeats are not added multiple times and admin - the owner - isn’t added:
sage: W.collaborators()
['hilbert', 'sage']
Set the worksheet filename (actually directory).
INPUT:
EXAMPLES:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: W = nb.create_new_worksheet('A Test Worksheet', 'admin')
sage: W.filename()
'admin/0'
sage: W.set_filename('admin/10')
sage: W.filename()
'admin/10'
Set this worksheet filename (actually directory) by getting the owner from the pre-stored owner via self.owner().
INPUT:
EXAMPLES:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: W = nb.create_new_worksheet('A Test Worksheet', 'admin')
sage: W.filename()
'admin/0'
sage: W.set_filename_without_owner('5')
sage: W.filename()
'admin/5'
Set the name of this worksheet.
INPUT:
EXAMPLES: We create a worksheet and change the name:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: W = nb.create_new_worksheet('A Test Worksheet', 'admin')
sage: W.set_name('A renamed worksheet')
sage: W.name()
'A renamed worksheet'
Set whether or not output should be pretty printed by default.
INPUT:
Note
The reason the input is a string and lower case instead of a Python bool is because this gets called indirectly from javascript. (And, Jason Grout wrote this and didn’t realize how unpythonic this design is - it should be redone to use True/False.)
EXAMPLES:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: W = nb.create_new_worksheet('A Test Worksheet', 'admin')
sage: W.set_pretty_print('false')
sage: W.pretty_print()
False
sage: W.set_pretty_print('true')
sage: W.pretty_print()
True
Set the published version of this worksheet to be the worksheet with given filename.
INPUT:
EXAMPLES:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: W = nb.create_new_worksheet('Publish Test', 'admin')
sage: P = nb.publish_worksheet(W, 'admin') # indirect test
sage: W._Worksheet__published_version
'pub/0'
sage: W.set_published_version('pub/0')
Set the math software system in which input is evaluated by default.
INPUT:
EXAMPLES:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: W = nb.create_new_worksheet('A Test Worksheet', 'admin')
sage: W.set_system('magma')
sage: W.system()
'magma'
Set the view on this worksheet for the given user.
INPUT:
EXAMPLES:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: W = nb.create_new_worksheet('Publish Test', 'admin')
sage: W.set_user_view('admin', sage.server.notebook.worksheet.ARCHIVED)
sage: W.user_view('admin') == sage.server.notebook.worksheet.ARCHIVED
True
Set the worksheet that was published to get self to W.
INPUT:
EXAMPLES:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: W = nb.create_new_worksheet('Publish Test', 'admin')
sage: P = nb.publish_worksheet(W, 'admin')
sage: P.worksheet_that_was_published() is W
True
We fake things and make it look like P published itself:
sage: P.set_worksheet_that_was_published(P)
sage: P.worksheet_that_was_published() is P
True
Return the math software system in which by default all input to the notebook is evaluated.
OUTPUT: string
EXAMPLES:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: W = nb.create_new_worksheet('A Test Worksheet', 'admin')
sage: W.system()
'sage'
sage: W.set_system('mathematica')
sage: W.system()
'mathematica'
Return True if the user with given name is allowed to edit this worksheet.
INPUT:
OUTPUT: bool
EXAMPLES: We create a notebook with one worksheet and two users.
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: nb.add_user('sage','sage','sage@sagemath.org',force=True)
sage: nb.add_user('william', 'william', 'wstein@sagemath.org', force=True)
sage: W = nb.create_new_worksheet('Test', 'sage')
sage: W.user_can_edit('sage')
True
At first the user ‘william’ can’t edit this worksheet:
sage: W.user_can_edit('william')
False
After adding ‘william’ as a collaborator he can edit the worksheet.
sage: W.add_collaborator('william')
sage: W.user_can_edit('william')
True
Clean up:
sage: nb.delete()
Return the view that the given user has of this worksheet. If the user currently doesn’t have a view set it to ACTIVE and return ACTIVE.
INPUT:
OUTPUT:
EXAMPLES: We create a new worksheet and get the view, which is ACTIVE:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: W = nb.create_new_worksheet('Publish Test', 'admin')
sage: W.user_view('admin')
1
sage: sage.server.notebook.worksheet.ACTIVE
1
Now for the admin user we move W to the archive:
sage: W.move_to_archive('admin')
The view is now archive.
sage: W.user_view('admin')
0
sage: sage.server.notebook.worksheet.ARCHIVED
0
For any other random viewer the view is set by default to ACTIVE.
sage: W.user_view('foo')
1
Return True if the user view of user is x.
INPUT:
EXAMPLES:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: W = nb.create_new_worksheet('Publish Test', 'admin')
sage: W.user_view_is('admin', sage.server.notebook.worksheet.ARCHIVED)
False
sage: W.user_view_is('admin', sage.server.notebook.worksheet.ACTIVE)
True
Returns a string of the non-owner viewers on this worksheet.
INPUT:
EXAMPLES:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: W = nb.create_new_worksheet('test1', 'admin')
sage: C = W.viewers(); C
[]
sage: C.append('sage')
sage: C.append('wstein')
sage: W.viewer_names()
'sage, wstein'
sage: W.viewer_names(max=1)
'sage, ...'
Return list of viewers of this worksheet.
OUTPUT:
EXAMPLES:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: nb.add_user('sage','sage','sage@sagemath.org',force=True)
sage: nb.add_user('hilbert','sage','sage@sagemath.org',force=True)
sage: W = nb.create_new_worksheet('test1', 'admin')
sage: W.add_viewer('hilbert')
sage: W.viewers()
['hilbert']
sage: W.add_viewer('sage')
sage: W.viewers()
['hilbert', 'sage']
Check to see if another user besides username was the last to edit this worksheet during the last threshold seconds. If so, return True and that user name. If not, return False.
INPUT:
Return the worksheet that was published to get this worksheet, if this worksheet was published. Otherwise just return this worksheet.
OUTPUT: Worksheet
EXAMPLES:
sage: nb = sage.server.notebook.notebook.Notebook(tmp_dir())
sage: W = nb.create_new_worksheet('Publish Test', 'admin')
sage: W.worksheet_that_was_published() is W
True
sage: S = nb.publish_worksheet(W, 'admin')
sage: S.worksheet_that_was_published() is S
False
sage: S.worksheet_that_was_published() is W
True
Return everything after the first whitespace in the string s. Returns the empty string if there is nothing after the first whitespace.
INPUT:
OUTPUT: a string
EXAMPLES:
sage: from sage.server.notebook.worksheet import after_first_word
sage: after_first_word("\%gap\n2+2\n")
'2+2\n'
sage: after_first_word("2+2")
''
INPUT:
OUTPUT:
INPUT: a block of wiki-like marked up text OUTPUT:
Returns everything before the first whitespace in the string s. If there is no whitespace, then the entire string s is returned.
EXAMPLES:
sage: from sage.server.notebook.worksheet import first_word
sage: first_word("\%gap\n2+2\n")
'\\%gap'
sage: first_word("2+2")
'2+2'
Given a string s that defines an input block of code, if the first line begins in sage: (or >>>), strip out all lines that don’t begin in either sage: (or >>>) or ..., and remove all sage: (or >>>) and ... from the beginning of the remaining lines.
TESTS:
sage: test1 = sage.server.notebook.worksheet.__internal_test1
sage: test1 == sage.server.notebook.worksheet.ignore_prompts_and_output(test1)
True
sage: test2 = sage.server.notebook.worksheet.__internal_test2
sage: sage.server.notebook.worksheet.ignore_prompts_and_output(test2)
'2 + 2\n'
Set the module-scope variable _a_sage to an initialized sage server.
INPUT:
EXAMPLES: The _a_sage variable is initially set to None:
sage: sage.server.notebook.worksheet._a_sage
We call init_sage_prestart and now _a_sage is a Sage instance:
sage: sage.server.notebook.worksheet.init_sage_prestart(None,None)
sage: sage.server.notebook.worksheet._a_sage
Sage
Return one copy of a Sage compute process that has initialization code run.
INPUT:
OUTPUT: a pexpect interface to a local or remote copy of Sage
EXAMPLES:
sage: S = sage.server.notebook.worksheet.initialized_sage(None,None)
sage: S
Sage
Return a Sage interface that has been initialized.
INPUT:
OUTPUT: an interface to a running copy of Sage
If the global variable multisession is true, each call to one_prestarted_sage returns a new Sage compute instance. Otherwise it always returns the same instance.
EXAMPLES:
sage: sage.server.notebook.worksheet.one_prestarted_sage(None,None)
Sage
sage: sage.server.notebook.worksheet.multisession=False
sage: sage.server.notebook.worksheet.one_prestarted_sage(None,None) is sage.server.notebook.worksheet._a_sage
True
sage: sage.server.notebook.worksheet.multisession=True
The point of this function is to allow for searches like this:
"ws 7" foo bar Modular '"the" end'
i.e., where search terms can be in quotes and the different quote types can be mixed.
INPUT:
OUTPUT:
Return the relative directory name of this worksheet with given name and owner.
INPUT:
OUTPUT: string
EXAMPLES:
sage: sage.server.notebook.worksheet.worksheet_filename('Example worksheet 3', 'sage10')
'sage10/Example_worksheet_3'
sage: sage.server.notebook.worksheet.worksheet_filename('Example#%&! work\sheet 3', 'sage10')
'sage10/Example_____work_sheet_3'