DB=False
NODB=True
from json import dumps as J
def get_cell_id():
return int(request.values.get('cell_id'))
def get_input():
return unicode_str(request.values.get('input', '')).replace('\r\n', '\n') #DOS
@json_output
@worksheet_command('eval')
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.
"""
if DB:
######################################################################
# Because of the decorator, we will have an object db,
# which is an interface to our database, and it automatically
# has a default worksheet_id and user_id, so these do not have
# to be specified.
id = get_cell_id()
D = {'id':id, 'status':'success'}
try:
db.increase_worksheet_state_number()
db.update_cell_input(cell_id=id, input_text=get_input())
db.evaluate_cell(cell_id=id)
if request.values.get('insert_cell_after', False):
new_cell_id = db.insert_cell_after(cell_id=id)
D['new_cell_id'] = new_cell_id
db.commit()
return D
except:
D['status'] = 'fail'
return D
######################################################################
# MOVE BOTH OF THESE TO A NEW URL!
if request.values.get('save_only', '0') == '1':
return J({'id':id, 'status':'success'})
elif request.values.get('text_only', '0') == '1':
return J({'id':id, 'status':'success'})
if NODB:
return s
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