# -*- coding: utf-8 -*-
"""
Magic command to help within notebooks
:githublink:`%|py|6`
"""
from IPython.core.magic import magics_class, line_magic
try:
import qgrid
except ImportError:
# We ignore that error since it is not
# part of the mandatory requirements.
qgrid = None
from jyquickhelper import add_notebook_menu
from pyquickhelper.ipythonhelper import MagicCommandParser, MagicClassWithHelpers
[docs]@magics_class
class MagicNotebook(MagicClassWithHelpers):
"""
Defines magic commands to help with notebooks
.. versionadded:: 1.1
:githublink:`%|py|24`
"""
[docs] @staticmethod
def jsdf_parser():
"""
defines the way to parse the magic command ``%jsdf``
:githublink:`%|py|101`
"""
parser = MagicCommandParser(
description='display a pandas DataFrame based on module qgrid', prog="jsdf")
parser.add_argument(
'df',
help='dataframe to display')
parser.add_argument(
'--defaultColumnWidth',
type=int,
default=80,
help='see https://github.com/mleibman/SlickGrid/wiki/Grid-Options')
parser.add_argument(
'--enableColumnReorder',
type=bool,
default=True,
help='see https://github.com/mleibman/SlickGrid/wiki/Grid-Options')
parser.add_argument(
'--multiColumnSort',
type=bool,
default=False,
help='see https://github.com/mleibman/SlickGrid/wiki/Grid-Options')
parser.add_argument(
'--rowHeight',
type=int,
default=25,
help='see https://github.com/mleibman/SlickGrid/wiki/Grid-Options')
parser.add_argument(
'--showHeaderRow',
type=bool,
default=False,
help='see https://github.com/mleibman/SlickGrid/wiki/Grid-Options')
parser.add_argument(
'--forceFitColumns',
type=bool,
default=False,
help='see https://github.com/mleibman/SlickGrid/wiki/Grid-Options')
parser.add_argument(
'--autoHeight',
type=bool,
default=False,
help='see https://github.com/mleibman/SlickGrid/wiki/Grid-Options')
parser.add_argument(
'--enableCellNavigation',
type=bool,
default=True,
help='see https://github.com/mleibman/SlickGrid/wiki/Grid-Options')
parser.add_argument(
'--precision',
type=int,
default=4,
help='see function qgrid.set_defaults')
parser.add_argument(
'--editable',
type=bool,
default=True,
help='make the output editable')
return parser
[docs] @line_magic
def jsdf(self, line):
"""
defines ``%jsdf``
which displays a pandas dataframe into a notebook using qgrid (javascript)
.. nbref::
:title: jsdf
The magic command ``%jsdf`` displays a dataframe using
`qgrid <https://qgrid.readthedocs.io/en/latest/>`_ module.
The code is the following::
import qgrid
if firt_call:
qgrid.set_defaults(precision=<precision>)
self.first_jsdf_call = False
df = args.df
grid_options = dict(defaultColumnWidth=<defaultColumnWidth>,
enableColumnReorder=<enableColumnReorder>,
multiColumnSort=<multiColumnSort>,
rowHeight=<rowHeight>,
showHeaderRow=<showHeaderRow>,
forceFitColumns=<forceFitColumns>,
autoHeight=<autoHeight>,
enableCellNavigation=<enableCellNavigation>)
qgrid.show_grid(df, grid_options=grid_options)
:githublink:`%|py|187`
"""
parser = self.get_parser(MagicNotebook.jsdf_parser, "jsdf")
args = self.get_args(line, parser)
if qgrid is None:
return "qgrid is not installed."
if not hasattr(self, "first_jsdf_call") or self.first_jsdf_call:
if args is not None:
qgrid.set_defaults(precision=args.precision)
else:
qgrid.set_defaults()
self.first_jsdf_call = False
if args is not None:
df = args.df
grid_options = dict(defaultColumnWidth=args.defaultColumnWidth,
enableColumnReorder=args.enableColumnReorder,
multiColumnSort=args.multiColumnSort,
rowHeight=args.rowHeight,
showHeaderRow=args.showHeaderRow,
forceFitColumns=args.forceFitColumns,
autoHeight=args.autoHeight,
enableCellNavigation=args.enableCellNavigation,
editable=args.editable)
res = qgrid.show_grid(df, grid_options=grid_options)
return "" if res is None else res
[docs]def register_notebook_magics(ip=None):
"""
register magics function, can be called from a notebook
:param ip: from ``get_ipython()``
:githublink:`%|py|221`
"""
if ip is None:
from IPython import get_ipython
ip = get_ipython()
ip.register_magics(MagicNotebook)