def worksheet_eval(worksheet): """ Evaluate a worksheet cell. If the request is not authorized (the requester did not enter the correct password for the given worksheet), then the request to evaluate or introspect the cell is ignored. If the cell contains either 1 or 2 question marks at the end (not on a comment line), then this is interpreted as a request for either introspection to the documentation of the function, or the documentation of the function and the source code of the function respectively. """ from sagenb.notebook.twist import encode_list from base import notebook_updates id = get_cell_id() input_text = unicode_str(request.values.get('input', '')).replace('\r\n', '\n') #DOS worksheet.increase_state_number() cell = worksheet.get_cell_with_id(id) cell.set_input_text(input_text) if request.values.get('save_only', '0') == '1': notebook_updates() return '' elif request.values.get('text_only', '0') == '1': notebook_updates() return encode_list([str(id), cell.html()]) else: new_cell = int(request.values.get( 'newcell', 0)) #wheter to insert a new cell or not cell.evaluate(username=g.username) if cell.is_last(): new_cell = worksheet.append_new_cell() s = encode_list( [new_cell.id(), 'append_new_cell', new_cell.html(div_wrap=False)]) elif new_cell: new_cell = worksheet.new_cell_after(id) s = encode_list([ new_cell.id(), 'insert_cell', new_cell.html(div_wrap=False), str(id) ]) else: s = encode_list([cell.next_id(), 'no_new_cell', str(id)]) notebook_updates() return s
def worksheet_delete_cell_output(worksheet): """Delete's a cell's output.""" id = get_cell_id() worksheet.get_cell_with_id(id).delete_output() from sagenb.notebook.twist import encode_list return encode_list(['delete_output', id])
def worksheet_eval(worksheet): """ Evaluate a worksheet cell. If the request is not authorized (the requester did not enter the correct password for the given worksheet), then the request to evaluate or introspect the cell is ignored. If the cell contains either 1 or 2 question marks at the end (not on a comment line), then this is interpreted as a request for either introspection to the documentation of the function, or the documentation of the function and the source code of the function respectively. """ from sagenb.notebook.twist import encode_list from base import notebook_updates id = get_cell_id() input_text = unicode_str(request.values.get('input', '')).replace('\r\n', '\n') #DOS worksheet.increase_state_number() cell = worksheet.get_cell_with_id(id) cell.set_input_text(input_text) if request.values.get('save_only', '0') == '1': notebook_updates() return '' elif request.values.get('text_only', '0') == '1': notebook_updates() return encode_list([str(id), cell.html()]) else: new_cell = int(request.values.get('newcell', 0)) #wheter to insert a new cell or not cell.evaluate(username=g.username) if cell.is_last(): new_cell = worksheet.append_new_cell() s = encode_list([new_cell.id(), 'append_new_cell', new_cell.html(div_wrap=False)]) elif new_cell: new_cell = worksheet.new_cell_after(id) s = encode_list([new_cell.id(), 'insert_cell', new_cell.html(div_wrap=False), str(id)]) else: s = encode_list([cell.next_id(), 'no_new_cell', str(id)]) notebook_updates() return s
def worksheet_cell_list(worksheet): """ Return the state number and the HTML for the main body of the worksheet, which consists of a list of cells. """ # TODO: Send and actually use the body's HTML. from sagenb.notebook.twist import encode_list return encode_list([worksheet.state_number(), ''])
def worksheet_new_cell_after(worksheet): """Add a new cell after a given cell.""" id = get_cell_id() input = unicode_str(request.values.get('input', '')) cell = worksheet.new_cell_after(id, input=input) worksheet.increase_state_number() from sagenb.notebook.twist import encode_list return encode_list([cell.id(), cell.html(div_wrap=False), id])
def worksheet_new_text_cell_after(worksheet): """Add a new text cell after a given cell.""" id = get_cell_id() input = unicode_str(request.values.get('input', '')) cell = worksheet.new_text_cell_after(id, input=input) worksheet.increase_state_number() from sagenb.notebook.twist import encode_list # XXX: Does editing correspond to TinyMCE? If so, we should try # to centralize that code. return encode_list([cell.id(), cell.html(editing=True), id])
def worksheet_introspect(worksheet): """ Cell introspection. This is called when the user presses the tab key in the browser in order to introspect. """ id = get_cell_id() before_cursor = request.values.get('before_cursor', '') after_cursor = request.values.get('after_cursor', '') cell = worksheet.get_cell_with_id(id) cell.evaluate(introspect=[before_cursor, after_cursor]) from sagenb.notebook.twist import encode_list return encode_list([cell.next_id(), 'introspect', id])
def worksheet_cell_update(worksheet): import time from sagenb.notebook.twist import encode_list id = get_cell_id() # update the computation one "step". worksheet.check_comp() # now get latest status on our cell status, cell = worksheet.check_cell(id) if status == 'd': new_input = cell.changed_input_text() out_html = cell.output_html() H = "Worksheet '%s' (%s)\n" % ( worksheet.name(), time.strftime("%Y-%m-%d at %H:%M", time.localtime(time.time()))) H += cell.edit_text(ncols=g.notebook.HISTORY_NCOLS, prompts=False, max_out=g.notebook.HISTORY_MAX_OUTPUT) g.notebook.add_to_user_history(H, g.username) else: new_input = '' out_html = '' if cell.interrupted(): inter = 'true' else: inter = 'false' raw = cell.output_text(raw=True).split("\n") if "Unhandled SIGSEGV" in raw: inter = 'restart' print "Segmentation fault detected in output!" msg = '%s%s %s' % (status, cell.id(), encode_list([ cell.output_text(html=True), cell.output_text( g.notebook.conf()['word_wrap_cols'], html=True), out_html, new_input, inter, cell.introspect_html() ])) # There may be more computations left to do, so start one if there is one. worksheet.start_next_comp() return msg
def worksheet_delete_cell(worksheet): """ Deletes a notebook cell. If there is only one cell left in a given worksheet, the request to delete that cell is ignored because there must be a least one cell at all times in a worksheet. (This requirement exists so other functions that evaluate relative to existing cells will still work, and so one can add new cells.) """ id = get_cell_id() if len(worksheet.compute_cell_id_list()) <= 1: return 'ignore' else: prev_id = worksheet.delete_cell_with_id(id) from sagenb.notebook.twist import encode_list return encode_list(['delete', id, prev_id, worksheet.cell_id_list()])
def worksheet_cell_update(worksheet): import time from sagenb.notebook.twist import encode_list id = get_cell_id() # update the computation one "step". worksheet.check_comp() # now get latest status on our cell status, cell = worksheet.check_cell(id) if status == 'd': new_input = cell.changed_input_text() out_html = cell.output_html() H = "Worksheet '%s' (%s)\n"%(worksheet.name(), time.strftime("%Y-%m-%d at %H:%M",time.localtime(time.time()))) H += cell.edit_text(ncols=g.notebook.HISTORY_NCOLS, prompts=False, max_out=g.notebook.HISTORY_MAX_OUTPUT) g.notebook.add_to_user_history(H, g.username) else: new_input = '' out_html = '' if cell.interrupted(): inter = 'true' else: inter = 'false' raw = cell.output_text(raw=True).split("\n") if "Unhandled SIGSEGV" in raw: inter = 'restart' print "Segmentation fault detected in output!" msg = '%s%s %s'%(status, cell.id(), encode_list([cell.output_text(html=True), cell.output_text(g.notebook.conf()['word_wrap_cols'], html=True), out_html, new_input, inter, cell.introspect_html()])) # There may be more computations left to do, so start one if there is one. worksheet.start_next_comp() return msg