AUTHORS:
- William Stein
Bases: object
Add a user with the given credentials.
INPUT:
If the method get_accounts() returns False then user can only be added if force is True.
EXAMPLES:
sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb')
sage: nb.add_user('Mark', 'password', '', force=True)
sage: nb.user('Mark')
Mark
sage: nb.add_user('Sarah', 'password', ")
Traceback (most recent call last):
ValueError: creating new accounts disabled.
sage: nb.set_accounts(True)
sage: nb.add_user('Sarah', 'password', ")sage:
nb.user('Sarah')
Sarah
Change a user’s password.
INPUT:
EXAMPLES:
sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb')
sage: nb.add_user('Mark', 'password', '', force=True)
sage: nb.user('Mark').password()
'aajfMKNH1hTm2'
sage: nb.change_password('Mark', 'different_password')
sage: nb.user('Mark').password()
'aaTlXok5npQME'
Create the default users for a notebook.
INPUT:
EXAMPLES:
sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb')
sage: nb.create_default_users('password')
Creating default users.
sage: list(sorted(nb.users().iteritems()))
[('_sage_', _sage_), ('admin', admin), ('guest', guest), ('pub', pub)]
sage: list(sorted(nb.passwords().iteritems()))
[('_sage_', 'aaQSqAReePlq6'), ('admin', 'aajfMKNH1hTm2'), ('guest', 'aaQSqAReePlq6'), ('pub', 'aaQSqAReePlq6')]
sage: nb.create_default_users('newpassword')
Creating default users.
WARNING: User 'pub' already exists -- and is now being replaced.
WARNING: User '_sage_' already exists -- and is now being replaced.
WARNING: User 'guest' already exists -- and is now being replaced.
WARNING: User 'admin' already exists -- and is now being replaced.
sage: list(sorted(nb.passwords().iteritems()))
[('_sage_', 'aaQSqAReePlq6'), ('admin', 'aajH86zjeUSDY'), ('guest', 'aaQSqAReePlq6'), ('pub', 'aaQSqAReePlq6')]
Change the password of user to that of other_user.
INPUT:
EXAMPLES:
sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb')
sage: nb.add_user('bob', 'an**d', 'bob@gmail.com', force=True)
sage: nb.user('bob').password()
'aa4Q6Jbx/MiUs'
sage: nb.add_user('mary', 'ccd', 'mary@gmail.com', force=True)
sage: nb.user('mary').password()
'aaxr0gcWJMXKU'
sage: nb.create_user_with_same_password('bob', 'mary')
sage: nb.user('bob').password() == nb.user('mary').password()
True
Return a default login name that the user will see when confronted with the Sage notebook login page. Currently, this returns ‘admin’ if that is the only user. Otherwise it returns an empty string (‘’).
OUTPUT:
EXAMPLES:
sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb')
sage: nb.create_default_users('password')
Creating default users.
sage: nb.default_user()
'admin'
sage: nb.add_user('AnotherUser', 'password', '', force=True)
sage: nb.default_user()
''
Delete the given user.
INPUT:
EXAMPLES:
sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb')
sage: nb.add_user('Mark', 'password', '', force=True)
sage: nb.user('Mark')
Mark
sage: nb.del_user('Mark')
sage: nb.user('Mark')
Traceback (most recent call last):
KeyError: "no user 'Mark'"
Delete all files related to this notebook.
This is used for doctesting mainly. This command is obviously VERY dangerous to use on a notebook you actually care about. You could easily lose all data.
EXAMPLES:
sage: tmp = tmp_dir() + '.sagenb'
sage: nb = sagenb.notebook.notebook.Notebook(tmp)
sage: sorted(os.listdir(tmp))
['home']
sage: nb.delete()
Now the directory is gone.:
sage: os.listdir(tmp)
...
OSError: [Errno 2] No such file or directory: '...
Delete the given worksheet and remove its name from the worksheet list. Raise a KeyError, if it is missing.
INPUT:
Empty the trash for the given user.
INPUT:
This empties the trash for the given user and cleans up all files associated with the worksheets that are in the trash.
EXAMPLES:
sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb')
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.move_to_trash('sage')
sage: nb.worksheet_names()
['sage/0']
sage: nb.empty_trash('sage')
sage: nb.worksheet_names()
[]
Export a worksheet, creating a sws file on the file system.
INPUT:
worksheet_filename - a string e.g., ‘username/id_number’
output_filename - a string, e.g., ‘worksheet.sws’
- title - title to use for the exported worksheet (if
None, just use current title)
Return whether or not users can create new accounts.
OUTPUT:
EXAMPLES:
sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb')
sage: nb.get_accounts()
False
sage: nb.set_accounts(True)
sage: nb.get_accounts()
True
Get the worksheet with the given filename. If there is no such worksheet, raise a KeyError.
INPUT:
OUTPUT:
Return the HTML for a worksheet’s index page.
INPUT:
OUTPUT:
EXAMPLES:
sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb')
sage: W = nb.create_new_worksheet('Test', 'admin')
sage: nb.html(W.filename(), 'admin')
u'...Test...cell_input...plainclick...state_number...'
Return HTML for a given worksheet’s post-publication page.
INPUT:
OUTPUT:
Return HTML for the warning and decision page displayed prior to publishing the given worksheet.
INPUT:
OUTPUT:
EXAMPLES:
sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb')
sage: W = nb.create_new_worksheet('Test', 'admin')
sage: nb.html_beforepublish_window(W, 'admin')
u'...want to publish this worksheet?...re-publish when changes...'
Return the HTML for the download or delete datafile page.
INPUT:
OUTPUT:
EXAMPLES:
sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb')
sage: W = nb.create_new_worksheet('Test', 'admin')
sage: nb.html_download_or_delete_datafile(W, 'admin', 'bar')
u'...Data file: bar...DATA is a special variable...uploaded...'
Return HTML for a window for editing worksheet.
INPUT:
OUTPUT:
EXAMPLES:
sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb')
sage: W = nb.create_new_worksheet('Test', 'admin')
sage: nb.html_edit_window(W, 'admin')
u'...textarea class="plaintextedit"...{{{id=1|...//...}}}...'
Return HTML for the window that displays a plain text version of the worksheet.
INPUT:
OUTPUT:
EXAMPLES:
sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb')
sage: W = nb.create_new_worksheet('Test', 'admin')
sage: nb.html_plain_text_window(W, 'admin')
u'...pre class="plaintext"...cell_intext...textfield...'
Return the HTML for the “share” page of a worksheet.
INPUT:
OUTPUT:
EXAMPLES:
sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb')
sage: W = nb.create_new_worksheet('Test', 'admin')
sage: nb.html_share(W, 'admin')
u'...currently shared...add or remove collaborators...'
Return the HTML for a specific revision of a worksheet.
INPUT:
OUTPUT:
Return HTML for the “Upload Data” window.
INPUT:
OUTPUT:
EXAMPLES:
sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb')
sage: W = nb.create_new_worksheet('Test', 'admin')
sage: nb.html_upload_data_window(W, 'admin')
u'...Upload or Create Data File...Browse...url...name of a new...'
Return HTML for the revision list of a worksheet.
INPUT:
OUTPUT:
EXAMPLES:
sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb')
sage: W = nb.create_new_worksheet('Test', 'admin')
sage: W.body()
u'\n\n{{{id=1|\n\n///\n}}}'
sage: W.save_snapshot('admin')
sage: nb.html_worksheet_revision_list('admin', W)
u'...Revision...Last Edited...ago...'
Import a worksheet with the given filename and set its owner. If the file extension is not txt or sws, raise a ValueError.
INPUT:
OUTPUT:
EXAMPLES:
We create a notebook and import a plain text worksheet into it.
sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb')
sage: name = tmp_filename() + '.txt'
sage: open(name,'w').write('foo\n{{{\n2+3\n}}}')
sage: W = nb.import_worksheet(name, 'admin')
W is our newly-created worksheet, with the 2+3 cell in it:
sage: W.name()
u'foo'
sage: W.cell_list()
[TextCell 0: foo, Cell 1; in=2+3, out=]
Return a username:password dictionary.
OUTPUT:
EXAMPLES:
sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb')
sage: nb.create_default_users('password')
Creating default users.
sage: nb.add_user('Mark', 'password', '', force=True)
sage: list(sorted(nb.passwords().iteritems()))
[('Mark', 'aajfMKNH1hTm2'), ('_sage_', 'aaQSqAReePlq6'), ('admin', 'aajfMKNH1hTm2'), ('guest', 'aaQSqAReePlq6'), ('pub', 'aaQSqAReePlq6')]
Publish a user’s worksheet. This creates a new worksheet in the ‘pub’ directory with the same contents as worksheet.
INPUT:
OUTPUT:
EXAMPLES:
sage: nb = sagenb.notebook.notebook.load_notebook(tmp_dir()+'.sagenb')
sage: nb.add_user('Mark','password','',force=True)
sage: W = nb.new_worksheet_with_title_from_text('First steps', owner='Mark')
sage: nb.worksheet_names()
['Mark/0']
sage: nb.publish_worksheet(nb.get_worksheet_with_filename('Mark/0'), 'Mark')
pub/1: [Cell 1; in=, out=]
sage: sorted(nb.worksheet_names())
['Mark/0', 'pub/1']
Set the accounts attribute of the server configuration to value. This property determines whether users can create new accounts.
INPUT:
EXAMPLES:
sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb')
sage: nb.get_accounts()
False
sage: nb.set_accounts(True)
sage: nb.get_accounts()
True
sage: nb.set_accounts(False)
sage: nb.get_accounts()
False
Return an instance of the User class given the username of a user in a notebook.
INPUT:
OUTPUT:
EXAMPLES:
sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb')
sage: nb.create_default_users('password')
Creating default users.
sage: nb.user('admin')
admin
sage: nb.user('admin').get_email()
''
sage: nb.user('admin').password()
'aajfMKNH1hTm2'
Return a user’s configuration object.
OUTPUT:
EXAMPLES:
sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb')
sage: nb.create_default_users('password')
Creating default users.
sage: config = nb.user_conf('admin')
sage: config['max_history_length']
1000
sage: config['default_system']
'sage'
sage: config['autosave_interval']
3600
sage: config['default_pretty_print']
False
Return whether a user with the given username exists.
INPUT:
OUTPUT:
EXAMPLES:
sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb')
sage: nb.create_default_users('password')
Creating default users.
sage: nb.user_exists('admin')
True
sage: nb.user_exists('pub')
True
sage: nb.user_exists('mark')
False
sage: nb.user_exists('guest')
True
Return True if user is an admin.
INPUT:
OUTPUT:
EXAMPLES:
sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb')
sage: nb.add_user('Administrator', 'password', '', 'admin', True)
sage: nb.add_user('RegularUser', 'password', '', 'user', True)
sage: nb.user_is_admin('Administrator')
True
sage: nb.user_is_admin('RegularUser')
False
Return True if username is a guest.
INPUT:
OUTPUT:
EXAMPLES:
sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb')
sage: nb.create_default_users('password')
Creating default users.
sage: nb.user_is_guest('guest')
True
sage: nb.user_is_guest('admin')
False
Return a list of user objects.
OUTPUT:
EXAMPLES:
sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb')
sage: nb.create_default_users('password')
Creating default users.
sage: sorted(nb.user_list(), key=lambda k: k.username())
[_sage_, admin, guest, pub]
Return a list of usernames.
OUTPUT:
EXAMPLES:
sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb')
sage: nb.create_default_users('password')
Creating default users.
sage: sorted(nb.usernames())
['_sage_', 'admin', 'guest', 'pub']
Return a dictionary of users in a notebook.
OUTPUT:
EXAMPLES:
sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb')
sage: nb.create_default_users('password')
Creating default users.
sage: list(sorted(nb.users().iteritems()))
[('_sage_', _sage_), ('admin', admin), ('guest', guest), ('pub', pub)]
Return a list of users that can log in.
OUTPUT:
EXAMPLES:
sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb')
sage: nb.create_default_users('password')
Creating default users.
sage: nb.valid_login_names()
['admin']
sage: nb.add_user('Mark', 'password', '', force=True)
sage: nb.add_user('Sarah', 'password', '', force=True)
sage: nb.add_user('David', 'password', '', force=True)
sage: sorted(nb.valid_login_names())
['David', 'Mark', 'Sarah', 'admin']
Create a new worksheet with given id_number belonging to the user with given username, or return an already existing worksheet. If id_number is None, creates a new worksheet using the next available new id_number for the given user.
INPUT:
- username – string
- id_number - nonnegative integer or None (default)
Return a list of all the names of worksheets in this notebook.
OUTPUT:
EXAMPLES:
We make a new notebook with two users and two worksheets, then list their names:
sage: nb = sagenb.notebook.notebook.Notebook(tmp_dir()+'.sagenb')
sage: nb.add_user('sage','sage','sage@sagemath.org',force=True)
sage: W = nb.new_worksheet_with_title_from_text('Sage', owner='sage')
sage: nb.add_user('wstein','sage','wstein@sagemath.org',force=True)
sage: W2 = nb.new_worksheet_with_title_from_text('Elliptic Curves', owner='wstein')
sage: nb.worksheet_names()
['sage/0', 'wstein/1']
Load and return a notebook from a given directory. Create a new one in that directory, if one isn’t already there.
INPUT:
OUTPUT:
Replace an absolute path with a relative path, if possible. Otherwise, return the given path.
INPUT:
OUTPUT:
Sort a given list on a given key, in a given order.
INPUT:
OUTPUT: