Skip to content
This repository has been archived by the owner on Jan 13, 2024. It is now read-only.

Commit

Permalink
add magic command %ggplot + refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
sdpython committed Jan 5, 2015
1 parent 1a5fa10 commit fcbf337
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 28 deletions.
2 changes: 1 addition & 1 deletion README.rst
Expand Up @@ -83,7 +83,7 @@ Versions
* **add:** method :meth:`download_cluster <pyensae.remote.remote_connection.ASSHClient.download_cluster>`
* **add:** add magic command to test a streaming script for PIG
* **add:** function :func:`file_head <pyensae.file_helper.content_helper.file_head>`, :func:`file_tail <pyensae.file_helper.content_helper.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 <pyensae.remote.azure_connection.AzureClient>`
* **add:** add magic commands for SQLite3 + a notebook
Expand Down
39 changes: 39 additions & 0 deletions _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 ()
2 changes: 2 additions & 0 deletions src/pyensae/__init__.py
Expand Up @@ -53,13 +53,15 @@ 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
register_magics_ssh()
if az: register_azure_magics()
register_sql_magics()
register_file_magics()
register_graph_magics()
except ImportError:
# IPython is not installed
pass
29 changes: 2 additions & 27 deletions src/pyensae/file_helper/magic_file.py
Expand Up @@ -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():
"""
Expand Down Expand Up @@ -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():
"""
Expand Down
Empty file.
54 changes: 54 additions & 0 deletions 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)
18 changes: 18 additions & 0 deletions 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 <http://matplotlib.org/users/style_sheets.html>`_
.. versionadded:: 1.1
"""
matplotlib.pyplot.style.use(style)

0 comments on commit fcbf337

Please sign in to comment.