diff --git a/README.rst b/README.rst index 5fda9fcd..44191464 100644 --- a/README.rst +++ b/README.rst @@ -83,7 +83,7 @@ Versions * **add:** method :meth:`download_cluster ` * **add:** add magic command to test a streaming script for PIG * **add:** function :func:`file_head `, :func:`file_tail `, - * **add:** add magic command ``%lsrepo``, ``%compress`` + * **add:** add magic command ``%lsrepo``, ``%compress``, ``%mpl_style`` * **1.0 - 2014/11/10** * **add:** add magic command ``%tail_stderr`` for :class:`AzureClient ` * **add:** add magic commands for SQLite3 + a notebook diff --git a/_unittests/ut_graph/test_graph.py b/_unittests/ut_graph/test_graph.py new file mode 100644 index 00000000..67e366d0 --- /dev/null +++ b/_unittests/ut_graph/test_graph.py @@ -0,0 +1,39 @@ +""" +@brief test log(time=1s) + +You should indicate a time in seconds. The program ``run_unittests.py`` +will sort all test files by increasing time and run them. +""" + + +import sys, os, unittest, shlex + + +try : + import src + import pyquickhelper +except ImportError : + path = os.path.normpath(os.path.abspath( os.path.join( os.path.split(__file__)[0], "..", ".."))) + if path not in sys.path : sys.path.append (path) + path = os.path.normpath(os.path.abspath( os.path.join( os.path.split(__file__)[0], "..", "..", "..", "pyquickhelper", "src"))) + if path not in sys.path : sys.path.append (path) + import src + import pyquickhelper + +from pyquickhelper import fLOG, get_temp_folder +from src.pyensae.graph_helper.magic_graph import MagicGraph + + +class TestGraph (unittest.TestCase): + + def test_graph_style(self) : + fLOG (__file__, self._testMethodName, OutputPrint = __name__ == "__main__") + path = os.path.abspath(os.path.dirname(__file__)) + mg = MagicGraph() + cmd = "ggplot" + res = mg.mpl_style(cmd) + fLOG(res) + + +if __name__ == "__main__" : + unittest.main () \ No newline at end of file diff --git a/src/pyensae/__init__.py b/src/pyensae/__init__.py index 8acd2d32..e58dd80d 100644 --- a/src/pyensae/__init__.py +++ b/src/pyensae/__init__.py @@ -53,6 +53,7 @@ def check( log = False): raise e from .sql.magic_sql import register_sql_magics from .file_helper.magic_file import register_file_magics + from .graph_helper.pagic_graph import register_graph_magics ip = get_ipython() if ip is not None: # the program is not run from a notebook @@ -60,6 +61,7 @@ def check( log = False): if az: register_azure_magics() register_sql_magics() register_file_magics() + register_graph_magics() except ImportError: # IPython is not installed pass \ No newline at end of file diff --git a/src/pyensae/file_helper/magic_file.py b/src/pyensae/file_helper/magic_file.py index d4d7d35b..6241e5c7 100644 --- a/src/pyensae/file_helper/magic_file.py +++ b/src/pyensae/file_helper/magic_file.py @@ -10,30 +10,19 @@ from IPython.core.display import HTML from pyquickhelper.filehelper.synchelper import explore_folder_iterfile, explore_folder_iterfile_repo -from pyquickhelper import MagicCommandParser, run_cmd, zip_files, gzip_files, zip7_files +from pyquickhelper import MagicCommandParser, run_cmd, zip_files, gzip_files, zip7_files, MagicClassWithHelpers from .format_helper import format_file_size, format_file_mtime from .content_helper import file_head, file_tail @magics_class -class MagicFile(Magics): +class MagicFile(MagicClassWithHelpers): """ Defines magic commands to list the content of a folder .. versionadded:: 1.1 """ - @property - def Context(self): - """ - return the context or None - - .. versionadded:: 1.1 - """ - if self.shell is None : - return None - return self.shell.user_ns - @staticmethod def head_parser(): """ @@ -246,20 +235,6 @@ def lsrepo(self, line): rows.append(r) return pandas.DataFrame(rows) - def add_context(self, context): - """ - add context to the class - - @param context dictionary - """ - if self.shell is None: - class EmptyClass: - def __init__(self): - self.user_ns={} - self.shell = EmptyClass() - for k,v in context.items(): - self.shell.user_ns[k]=v - @staticmethod def _compress_parser(): """ diff --git a/src/pyensae/graph_helper/__init__.py b/src/pyensae/graph_helper/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/pyensae/graph_helper/magic_graph.py b/src/pyensae/graph_helper/magic_graph.py new file mode 100644 index 00000000..49019f7b --- /dev/null +++ b/src/pyensae/graph_helper/magic_graph.py @@ -0,0 +1,54 @@ +#-*- coding: utf-8 -*- +""" +@file +@brief Magic commands about graphs + +.. versionadded:: 1.1 +""" +import sys, os, pandas + +from IPython.core.magic import Magics, magics_class, line_magic, cell_magic +from IPython.core.magic import line_cell_magic +from IPython.core.display import HTML + +from pyquickhelper import MagicCommandParser, MagicClassWithHelpers +from .matplotlib_helper import mpl_switch_style + +@magics_class +class MagicGraph(MagicClassWithHelpers): + """ + Defines magic commands to list the content of a folder + + .. versionadded:: 1.1 + """ + + @staticmethod + def mpl_style_parser(): + """ + defines the way to parse the magic command ``%mpl_style`` + """ + parser = MagicCommandParser(description='changes matplotlib style') + parser.add_argument('style', type=str, help='style, ggplot for exemple', default="ggplot") + return parser + + @line_magic + def mpl_style(self, line): + """ + defines ``%mpl_style`` + which changes the style of matplotlib graphs, example: ``%mpl_style ggplot`` + """ + parser = self.get_parser(MagicGraph.mpl_style_parser, "mpl_style") + args = self.get_args(line, parser) + + if args is not None: + style = args.style + mpl_switch_style(style) + + +def register_graph_magics(): + """ + register magics function, can be called from a notebook + """ + from IPython import get_ipython + ip = get_ipython() + ip.register_magics(MagicGraph) \ No newline at end of file diff --git a/src/pyensae/graph_helper/matplotlib_helper.py b/src/pyensae/graph_helper/matplotlib_helper.py new file mode 100644 index 00000000..786a650c --- /dev/null +++ b/src/pyensae/graph_helper/matplotlib_helper.py @@ -0,0 +1,18 @@ +#-*- coding: utf-8 -*- +""" +@file +@brief Various functions about matplotlib +""" + +import matplotlib.pyplot + + +def mpl_switch_style(style="ggplot"): + """ + changes the graph style + + @param style see `Customizing plots with style sheets `_ + + .. versionadded:: 1.1 + """ + matplotlib.pyplot.style.use(style) \ No newline at end of file