This package now serves two purposes:
- Display markdown from within code cells in IPython output, while ignoring it when run in normal Python (like usual comments). For example,
"V = {1 \over 3} \pi r^2 h".md()
- Display objects other than strings in IPython output using an extension-like function
.md()
. For example:dot = Digraph() # ... dot.md()
A note on how it can be useful can be found on randompearls.com.
These packages are auto-installed during the installation of this package.
- forbiddenfruit
- markdown
- beautifulsoup4
IPython is not installed automatically.
This is because if you use the overridden print statements (explained below), they are supposed to work without IPython as well.
But, if you do have IPython, those print statements will print formatted text.
Note that forbiddenfruit is installed as a dependency, so that statements of the form "*anymdstring*".md(dtype=DisplayType.Type)
can be used conveniently.
You can uninstall forbiddenfruit if you want and then use display_ccmd("*anymdstring*", dtype=DisplayType.MARKDOWN)
.
The package can be installed by running:
pip install git+https://github.com/manisar2/ipyccmd.git
Or, copy the code from src/ipyccmd/__init__.py into your project.
It can be used in two ways depending upon your taste:
- using function
display_ccmd()
(or curse function.md()
), or - by overriding
print()
.
Note that you will not need the import statements shown below if you have copy-pasted the code from src/ipyccmd/__init__.py into your current file.
# With curse .md():
from ipyccmd import DisplayType
"Now we'll *calculate* the **area** as per $A = \pi r^2 + 2 \pi r h$.".md()
"V = {1 \over 3} \pi r^2 h".md(dtype=DisplayType.MATH, python_print=True)
(2).md(DisplayType.MATH) # .md() is not limited to strings
object().md()
# Without using curse (can uninstall forbiddenfruit):
from ipyccmd import display_ccmd, DisplayType
display_ccmd("Now we'll calculate the area as per $A = \pi r^2 + 2 \pi r h$.")
display_ccmd("V = {1 \over 3} \pi r^2 h", dtype=DisplayType.MATH, python_print=True)
display_ccmd(2, DisplayType.MATH)
# In both the cases, if you pass python_print=True or set global PYTHON_PRINT=True (default),
# the string will also be printed when the code is run as normal Python - with markdown symbols
# and HTML tags removed (except MATH symbols).
# Thus, you can have your string displayed in both IPython and Python - with formatting, and
# without markdown symbols and HTML tags respectively.
from ipyccmd import md_print, DisplayType
print = md_print
print("Now we'll *calculate* the **area** as per $A = \pi r^2 + 2 \pi r h$.")
print("V = {1 \over 3} \pi r^2 h", is_md=True, dtype=DisplayType.MATH)
print(2, is_md=True, dtype=DisplayType.MATH) # the new print can handle other objects as well
print(object(), is_md=True) # any object can be displayed/printed
# This overriden print will ensure that the string is displayed in both IPython (formatted) and
# Python (with markdown symbols and HTML tags removed).
# The value of the argument is_md or global IS_MD will work as follows:
# True (default) => formatted text in IPython, plain_text (without markdown symbols and HTML tags) in Python
# False => unmodified text in both IPython and Python (like normal print)
The advantage with B. is that if print()
is used without any arguments (is_md
and type
), all the print statements will continue to work in both IPython and Python even without this package, thus giving you the option of not using this package in production.
The notebook shows some of the outputs generated by using different DisplayTypes.
-
- MARKDOWN
- LATEX
- MATH
- HTML
- PRETTY - use this for displaying / printing the default representation of the object
- NONE - this will let display() handle the object (and not any of its representations)
-
- If global
DEFAULT_DTYPE
is set, its value will be used; otherwise dtype
will be set to.MARKDOWN
if obj isstr
, else.PRETTY
- If global
-
This is governed by the argument
print_objname
or the globalPRINT_OBJNAME
. -
Pass
python_print
asFalse
or set the globalPYTHON_PRINT
toFalse
.
Note that this is applicable only todisplay_ccmd()
ormd()
.
If you are using overridenprint()
ormd_print()
, it will always print in both IPython and Python. -
The functions
display_ccmd()
,md()
,print()
andmd_print()
provide the following two features:- displaying formatted text in IPython; and
- printing markdown-html-stripped text in Python
If you want to suppress this functionality temporarily, either pass
is_md=False
, or set the globalIS_MD=False
. This will enable normal printing.