Commit a5ab1070 authored by Martin Bauer's avatar Martin Bauer
Browse files

Merge branch 'rich-syntax-highlighting' into 'master'

Use `rich` for syntax highlighting of `show_code` also in terminal

See merge request pycodegen/pystencils!139
parents 39209309 26445cdf
...@@ -71,11 +71,31 @@ def get_code_str(ast, custom_backend=None): ...@@ -71,11 +71,31 @@ def get_code_str(ast, custom_backend=None):
return str(get_code_obj(ast, custom_backend)) return str(get_code_obj(ast, custom_backend))
def _isnotebook():
shell = get_ipython().__class__.__name__
if shell == 'ZMQInteractiveShell':
return True # Jupyter notebook or qtconsole
elif shell == 'TerminalInteractiveShell':
return False # Terminal running IPython
return False # Other type (?)
except NameError:
return False
def show_code(ast: Union[KernelFunction, KernelWrapper], custom_backend=None): def show_code(ast: Union[KernelFunction, KernelWrapper], custom_backend=None):
code = get_code_obj(ast, custom_backend) code = get_code_obj(ast, custom_backend)
try: if _isnotebook():
from IPython.display import display from IPython.display import display
display(code) display(code)
except Exception: else:
print(code) try:
import rich.syntax
import rich.console
syntax = rich.syntax.Syntax(str(code), "c++", theme="monokai", line_numbers=True)
console = rich.console.Console()
except ImportError:
...@@ -112,7 +112,7 @@ setup(name='pystencils', ...@@ -112,7 +112,7 @@ setup(name='pystencils',
'opencl': ['pyopencl'], 'opencl': ['pyopencl'],
'alltrafos': ['islpy', 'py-cpuinfo'], 'alltrafos': ['islpy', 'py-cpuinfo'],
'bench_db': ['blitzdb', 'pymongo', 'pandas'], 'bench_db': ['blitzdb', 'pymongo', 'pandas'],
'interactive': ['matplotlib', 'ipy_table', 'imageio', 'jupyter', 'pyevtk'], 'interactive': ['matplotlib', 'ipy_table', 'imageio', 'jupyter', 'pyevtk', 'rich'],
'autodiff': ['pystencils-autodiff'], 'autodiff': ['pystencils-autodiff'],
'doc': ['sphinx', 'sphinx_rtd_theme', 'nbsphinx', 'doc': ['sphinx', 'sphinx_rtd_theme', 'nbsphinx',
'sphinxcontrib-bibtex', 'sphinx_autodoc_typehints', 'pandoc'], 'sphinxcontrib-bibtex', 'sphinx_autodoc_typehints', 'pandoc'],
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