forked from jbkoh/brick-tutorial-buildsys2017
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcommon.py
28 lines (26 loc) · 835 Bytes
/
common.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
try:
from IPython.display import display, Markdown
except:
pass
# Helper functions
def is_notebook():
try:
shell = get_ipython().__class__.__name__
if shell == 'ZMQInteractiveShell':
return True # Jupyter notebook or qtconsole
elif shell == 'TerminalInteractiveShell':
return False # Terminal running IPython
else:
return False # Other type (?)
except NameError:
return False # Probably standard Python interpreter
def print_graph(g):
g_str = g.serialize(format='turtle').decode('utf-8')
new_g_str = ''
for line in g_str.split('\n'):
if 'prefix' not in line:
new_g_str += line + '\n'
if is_notebook():
display(Markdown('```turtle\n' + new_g_str + '\n```'))
else:
print(g_str)