Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
pystencils
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Jan Hönig
pystencils
Commits
26445cdf
Commit
26445cdf
authored
5 years ago
by
Stephan Seitz
Browse files
Options
Downloads
Patches
Plain Diff
Use `rich` for syntax highlighting of `show_code` also in terminal
parent
39209309
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
pystencils/display_utils.py
+23
-3
23 additions, 3 deletions
pystencils/display_utils.py
setup.py
+1
-1
1 addition, 1 deletion
setup.py
with
24 additions
and
4 deletions
pystencils/display_utils.py
+
23
−
3
View file @
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
():
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
):
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
()
console
.
print
(
syntax
)
except
ImportError
:
print
(
code
)
This diff is collapsed.
Click to expand it.
setup.py
+
1
−
1
View file @
26445cdf
...
@@ -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
'
],
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment