Skip to content

Add %toggle_traceback line magic #486

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Starting with v1.31.6, this file will contain a record of major features and upd

## Upcoming
- Added openCypher and local file path support to `%seed` ([Link to PR](https://github.com/aws/graph-notebook/pull/292))
- Added `%toggle_traceback` line magic ([Link to PR](https://github.com/aws/graph-notebook/pull/486))

## Release 3.8.1 (April 17, 2023)
- Reinstate Python 3.7 support for compatibility with legacy AL1 Neptune Notebooks ([Link to PR](https://github.com/aws/graph-notebook/pull/479))
Expand Down
33 changes: 23 additions & 10 deletions src/graph_notebook/decorators/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import functools
import json
import re
import traceback

from IPython.core.display import HTML, display, clear_output

Expand Down Expand Up @@ -66,9 +67,14 @@ def get_variable_injection_value(raw_var: str, local_ns: dict):
def display_exceptions(func):
@functools.wraps(func)
def do_display_exceptions(*args, **kwargs):
try:
show_traceback = kwargs['local_ns']['graph_notebook_show_traceback']
except KeyError:
show_traceback = False
clear_output()
tab = widgets.Tab()

server_error = False
try:
return func(*args, **kwargs)
except KeyboardInterrupt:
Expand All @@ -77,23 +83,30 @@ def do_display_exceptions(*args, **kwargs):
except HTTPError as http_ex:
caught_ex = http_ex
raw_html = http_ex_to_html(http_ex)
server_error = True
except GremlinServerError as gremlin_ex:
caught_ex = gremlin_ex
raw_html = gremlin_server_error_to_html(gremlin_ex)
server_error = True
except Exception as e:
caught_ex = e
raw_html = exception_to_html(e)
if show_traceback:
caught_ex = traceback.format_exception(e)
traceback.print_exception(e)
else:
caught_ex = e
raw_html = exception_to_html(e)

if 'local_ns' in kwargs:
kwargs['local_ns']['graph_notebook_error'] = caught_ex

html = HTML(raw_html)
html_output = widgets.Output(layout={'overflow': 'scroll'})
with html_output:
display(html)
tab.children = [html_output]
tab.set_title(0, 'Error')
display(tab)
if server_error or not show_traceback:
html = HTML(raw_html)
html_output = widgets.Output(layout={'overflow': 'scroll'})
with html_output:
display(html)
tab.children = [html_output]
tab.set_title(0, 'Error')
display(tab)

return do_display_exceptions

Expand Down Expand Up @@ -132,7 +145,7 @@ def http_ex_to_html(http_ex: HTTPError):
return error_html


def exception_to_html(ex: Exception):
def exception_to_html(ex):
content = {
'error': ex
}
Expand Down
25 changes: 22 additions & 3 deletions src/graph_notebook/magics/graph_magic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1149,8 +1149,9 @@ def status(self, line='', local_ns: dict = None):
return status_res

@line_magic
@needs_local_scope
@display_exceptions
def db_reset(self, line):
def db_reset(self, line, local_ns: dict = None):
logger.info(f'calling system endpoint {self.client.host}')
parser = argparse.ArgumentParser()
parser.add_argument('-g', '--generate-token', action='store_true', help='generate token for database reset')
Expand Down Expand Up @@ -1998,7 +1999,8 @@ def cancel_load(self, line, local_ns: dict = None):

@line_magic
@display_exceptions
def seed(self, line):
@needs_local_scope
def seed(self, line, local_ns: dict = None):
"""
Provides a way to bulk insert data to your endpoint via Gremlin, openCypher, or SPARQL queries. Via the form
generated by running %seed with no arguments, you can do either of the following:
Expand Down Expand Up @@ -2443,13 +2445,30 @@ def disable_debug(self, line):
logger.setLevel(logging.ERROR)
root_logger.setLevel(logging.CRITICAL)

@line_magic
@needs_local_scope
def toggle_traceback(self, line, local_ns: dict = None):
show_traceback_ns_var = 'graph_notebook_show_traceback'
try:
show_traceback = local_ns[show_traceback_ns_var]
if not isinstance(show_traceback, bool):
show_traceback = False
else:
show_traceback = not show_traceback
except KeyError:
show_traceback = True

print(f"Display of tracebacks from magics is toggled {'ON' if show_traceback else 'OFF'}.")
store_to_ns(show_traceback_ns_var, show_traceback, local_ns)

@line_magic
def graph_notebook_version(self, line):
print(graph_notebook.__version__)

@line_cell_magic
@display_exceptions
def graph_notebook_vis_options(self, line='', cell=''):
@needs_local_scope
def graph_notebook_vis_options(self, line='', cell='', local_ns: dict = None):
parser = argparse.ArgumentParser()
parser.add_argument('--silent', action='store_true', default=False, help="Display no output.")
line_args = line.split()
Expand Down