Commit caddd8ce authored by Ivan Tyagov's avatar Ivan Tyagov

If user using Jupyter with ERP5 kernel is simply passing a comment inside a...

If user using Jupyter with ERP5 kernel is simply passing a comment inside a cell a server side error is raised thus this commit properly fixes it.
Clean up & style.
parent 034b5b85
...@@ -43,7 +43,8 @@ def Base_executeJupyter(self, python_expression=None, reference=None, \ ...@@ -43,7 +43,8 @@ def Base_executeJupyter(self, python_expression=None, reference=None, \
python_expression = '' python_expression = ''
# Get Data Notebook with the specific reference # Get Data Notebook with the specific reference
data_notebook = self.portal_catalog.getResultValue(portal_type='Data Notebook', data_notebook = self.portal_catalog.getResultValue(
portal_type='Data Notebook',
reference=reference) reference=reference)
# Create new Data Notebook if reference doesn't match with any from existing ones # Create new Data Notebook if reference doesn't match with any from existing ones
...@@ -107,13 +108,12 @@ def Base_executeJupyter(self, python_expression=None, reference=None, \ ...@@ -107,13 +108,12 @@ def Base_executeJupyter(self, python_expression=None, reference=None, \
u'evalue': None, u'evalue': None,
u'traceback': None, u'traceback': None,
u'status': u'error', u'status': u'error',
u'mime_type': result['mime_type'] u'mime_type': result['mime_type']}
}
serialized_result = json.dumps(result) serialized_result = json.dumps(result)
data_notebook_line.edit( data_notebook_line.edit(
notebook_code_result=result['code_result'], notebook_code_result = result['code_result'],
mime_type=result['mime_type']) mime_type = result['mime_type'])
return serialized_result return serialized_result
...@@ -168,13 +168,16 @@ def Base_runJupyterCode(self, jupyter_code, old_notebook_context): ...@@ -168,13 +168,16 @@ def Base_runJupyterCode(self, jupyter_code, old_notebook_context):
# dictionary, but that might hamper the speed of exec or eval. # dictionary, but that might hamper the speed of exec or eval.
# Something like -- user_context = globals(); user_context['context'] = self; # Something like -- user_context = globals(); user_context['context'] = self;
user_context = {} user_context = {}
output = '' output = ''
# Saving the initial globals dict so as to compare it after code execution # Saving the initial globals dict so as to compare it after code execution
globals_dict = globals() globals_dict = globals()
notebook_context = old_notebook_context notebook_context = old_notebook_context
inject_variable_dict = {}
current_var_dict = {}
current_setup_dict = {}
# Execute only if jupyter_code is not empty # Execute only if jupyter_code is not empty
if jupyter_code: if jupyter_code:
# Create ast parse tree # Create ast parse tree
...@@ -282,8 +285,7 @@ def Base_runJupyterCode(self, jupyter_code, old_notebook_context): ...@@ -282,8 +285,7 @@ def Base_runJupyterCode(self, jupyter_code, old_notebook_context):
'mime_type': 'text/plain', 'mime_type': 'text/plain',
'evalue': None, 'evalue': None,
'ename': None, 'ename': None,
'traceback': None, 'traceback': None}
}
return result return result
# Removing all the setup functions if user call environment.clearAll() # Removing all the setup functions if user call environment.clearAll()
...@@ -323,8 +325,7 @@ def Base_runJupyterCode(self, jupyter_code, old_notebook_context): ...@@ -323,8 +325,7 @@ def Base_runJupyterCode(self, jupyter_code, old_notebook_context):
) % (data['code'], func_name, func_name) ) % (data['code'], func_name, func_name)
notebook_context['setup'][data['alias']] = { notebook_context['setup'][data['alias']] = {
"func_name": func_name, "func_name": func_name,
"code": setup_string "code": setup_string}
}
# Iterating over envinronment.define calls captured by the environment collector # Iterating over envinronment.define calls captured by the environment collector
# that are simple variables and saving them in the setup. # that are simple variables and saving them in the setup.
...@@ -332,8 +333,7 @@ def Base_runJupyterCode(self, jupyter_code, old_notebook_context): ...@@ -332,8 +333,7 @@ def Base_runJupyterCode(self, jupyter_code, old_notebook_context):
setup_string = "%s = %s\n" % (variable, repr(value)) setup_string = "%s = %s\n" % (variable, repr(value))
notebook_context['setup'][variable] = { notebook_context['setup'][variable] = {
'func_name': variable, 'func_name': variable,
'code': setup_string 'code': setup_string}
}
user_context['_volatile_variable_list'] += variable user_context['_volatile_variable_list'] += variable
if environment_collector.showEnvironmentSetup(): if environment_collector.showEnvironmentSetup():
...@@ -349,7 +349,6 @@ def Base_runJupyterCode(self, jupyter_code, old_notebook_context): ...@@ -349,7 +349,6 @@ def Base_runJupyterCode(self, jupyter_code, old_notebook_context):
# Abort the current transaction. As a consequence, the notebook lines # Abort the current transaction. As a consequence, the notebook lines
# are not added if an exception occurs. # are not added if an exception occurs.
transaction.abort() transaction.abort()
return getErrorMessageForException(self, e, notebook_context) return getErrorMessageForException(self, e, notebook_context)
# Execute the interactive nodes with 'single' mode # Execute the interactive nodes with 'single' mode
...@@ -362,16 +361,14 @@ def Base_runJupyterCode(self, jupyter_code, old_notebook_context): ...@@ -362,16 +361,14 @@ def Base_runJupyterCode(self, jupyter_code, old_notebook_context):
# Abort the current transaction. As a consequence, the notebook lines # Abort the current transaction. As a consequence, the notebook lines
# are not added if an exception occurs. # are not added if an exception occurs.
transaction.abort() transaction.abort()
return getErrorMessageForException(self, e, notebook_context) return getErrorMessageForException(self, e, notebook_context)
mime_type = display_data['mime_type'] or mime_type mime_type = display_data['mime_type'] or mime_type
inject_variable_dict['_print'].write("\n".join(removed_setup_message_list) + display_data['result']) inject_variable_dict['_print'].write("\n".join(removed_setup_message_list) + display_data['result'])
# Saves a list of all the variables we injected into the user context and # Saves a list of all the variables we injected into the user context and
# shall be deleted before saving the context. # shall be deleted before saving the context.
volatile_variable_list = current_setup_dict.keys() + inject_variable_dict.keys() + user_context['_volatile_variable_list'] volatile_variable_list = current_setup_dict.keys() + inject_variable_dict.keys() + user_context.get('_volatile_variable_list', [])
volatile_variable_list.append('__builtins__') volatile_variable_list.append('__builtins__')
for key, val in user_context.items(): for key, val in user_context.items():
...@@ -394,6 +391,7 @@ def Base_runJupyterCode(self, jupyter_code, old_notebook_context): ...@@ -394,6 +391,7 @@ def Base_runJupyterCode(self, jupyter_code, old_notebook_context):
if not key in user_context: if not key in user_context:
del notebook_context['variables'][key] del notebook_context['variables'][key]
if inject_variable_dict.get('_print') is not None:
output = inject_variable_dict['_print'].getCapturedOutputString() output = inject_variable_dict['_print'].getCapturedOutputString()
result = { result = {
...@@ -403,8 +401,7 @@ def Base_runJupyterCode(self, jupyter_code, old_notebook_context): ...@@ -403,8 +401,7 @@ def Base_runJupyterCode(self, jupyter_code, old_notebook_context):
'mime_type': mime_type, 'mime_type': mime_type,
'evalue': evalue, 'evalue': evalue,
'ename': ename, 'ename': ename,
'traceback': tb_list, 'traceback': tb_list}
}
return result return result
......
...@@ -46,11 +46,11 @@ ...@@ -46,11 +46,11 @@
<key> <string>text_content_warning_message</string> </key> <key> <string>text_content_warning_message</string> </key>
<value> <value>
<tuple> <tuple>
<string>W:299, 10: Use of exec (exec-used)</string> <string>W:301, 10: Use of exec (exec-used)</string>
<string>W:347, 10: Use of exec (exec-used)</string> <string>W:347, 10: Use of exec (exec-used)</string>
<string>W:360, 10: Use of exec (exec-used)</string> <string>W:359, 10: Use of exec (exec-used)</string>
<string>W:446, 6: No exception type(s) specified (bare-except)</string> <string>W:443, 6: No exception type(s) specified (bare-except)</string>
<string>W:902, 2: Redefining name \'IFrame\' from outer scope (line 4) (redefined-outer-name)</string> <string>W:899, 2: Redefining name \'IFrame\' from outer scope (line 4) (redefined-outer-name)</string>
</tuple> </tuple>
</value> </value>
</item> </item>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment