Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Stephan Seitz
pystencils
Commits
26445cdf
Commit
26445cdf
authored
Jan 30, 2020
by
Stephan Seitz
Browse files
Use `rich` for syntax highlighting of `show_code` also in terminal
parent
39209309
Pipeline
#21528
passed with stage
in 7 minutes and 40 seconds
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
pystencils/display_utils.py
View file @
26445cdf
...
...
@@ -71,11 +71,31 @@ def get_code_str(ast, custom_backend=None):
return
str
(
get_code_obj
(
ast
,
custom_backend
))
def
_isnotebook
():
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
def
show_code
(
ast
:
Union
[
KernelFunction
,
KernelWrapper
],
custom_backend
=
None
):
code
=
get_code_obj
(
ast
,
custom_backend
)
try
:
if
_isnotebook
()
:
from
IPython.display
import
display
display
(
code
)
except
Exception
:
print
(
code
)
else
:
try
:
import
rich.syntax
import
rich.console
syntax
=
rich
.
syntax
.
Syntax
(
str
(
code
),
"c++"
,
theme
=
"monokai"
,
line_numbers
=
True
)
console
=
rich
.
console
.
Console
()
console
.
print
(
syntax
)
except
ImportError
:
print
(
code
)
setup.py
View file @
26445cdf
...
...
@@ -112,7 +112,7 @@ setup(name='pystencils',
'opencl'
:
[
'pyopencl'
],
'alltrafos'
:
[
'islpy'
,
'py-cpuinfo'
],
'bench_db'
:
[
'blitzdb'
,
'pymongo'
,
'pandas'
],
'interactive'
:
[
'matplotlib'
,
'ipy_table'
,
'imageio'
,
'jupyter'
,
'pyevtk'
],
'interactive'
:
[
'matplotlib'
,
'ipy_table'
,
'imageio'
,
'jupyter'
,
'pyevtk'
,
'rich'
],
'autodiff'
:
[
'pystencils-autodiff'
],
'doc'
:
[
'sphinx'
,
'sphinx_rtd_theme'
,
'nbsphinx'
,
'sphinxcontrib-bibtex'
,
'sphinx_autodoc_typehints'
,
'pandoc'
],
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment