From 1a02991bbb0e1bbbbccdf6c5ff0887598808d2ba Mon Sep 17 00:00:00 2001 From: simon_graphkite Date: Thu, 11 Jul 2019 08:45:21 +0200 Subject: [PATCH] Commit for release 2.1.1. - Fix of [#206] - Improve code maintainability of the view (HTML templates, notebook) - Fix bug in dendrogram sizing --- docs/utils/notebook.html | 87 ++++++ docs/view/notebook.html | 264 ++++++++++++++++++ pandas_profiling/utils/notebook.py | 8 + pandas_profiling/view/notebook.py | 79 ++++++ .../view/templates/components/list.html | 12 + .../view/templates/components/section.html | 7 + .../view/templates/components/tabs.html | 26 ++ 7 files changed, 483 insertions(+) create mode 100644 docs/utils/notebook.html create mode 100644 docs/view/notebook.html create mode 100644 pandas_profiling/utils/notebook.py create mode 100644 pandas_profiling/view/notebook.py create mode 100644 pandas_profiling/view/templates/components/list.html create mode 100644 pandas_profiling/view/templates/components/section.html create mode 100644 pandas_profiling/view/templates/components/tabs.html diff --git a/docs/utils/notebook.html b/docs/utils/notebook.html new file mode 100644 index 000000000..f0370e58e --- /dev/null +++ b/docs/utils/notebook.html @@ -0,0 +1,87 @@ + + + + + + +pandas_profiling.utils.notebook API documentation + + + + + + + + + +
+
+
+

Module pandas_profiling.utils.notebook

+
+
+

Utility functions that come in handy when working with Jupyter notebooks

+
+Source code +
"""Utility functions that come in handy when working with Jupyter notebooks"""
+
+
+def full_width():
+    """Resize the notebook to use it's full width"""
+    from IPython.core.display import display, HTML
+
+    display(HTML("<style>.container { width:100% !important; }</style>"))
+
+
+
+
+
+
+
+

Functions

+
+
+def full_width() +
+
+

Resize the notebook to use it's full width

+
+Source code +
def full_width():
+    """Resize the notebook to use it's full width"""
+    from IPython.core.display import display, HTML
+
+    display(HTML("<style>.container { width:100% !important; }</style>"))
+
+
+
+
+
+
+
+ +
+ + + + + \ No newline at end of file diff --git a/docs/view/notebook.html b/docs/view/notebook.html new file mode 100644 index 000000000..573fcd335 --- /dev/null +++ b/docs/view/notebook.html @@ -0,0 +1,264 @@ + + + + + + +pandas_profiling.view.notebook API documentation + + + + + + + + + +
+
+
+

Module pandas_profiling.view.notebook

+
+
+

Functionality related to displaying the profile report in Jupyter notebooks.

+
+Source code +
"""Functionality related to displaying the profile report in Jupyter notebooks."""
+import html
+from pathlib import Path
+
+
+from pandas_profiling.config import config
+
+
+def get_notebook_iframe_srcdoc(profile):
+    """Get the IPython HTML object with ifram with the srcdoc attribute
+
+    Args:
+        profile: The profile report object
+
+    Returns:
+        IPython HTML object.
+    """
+    from IPython.core.display import HTML
+
+    iframe = """
+                <iframe
+                    width="{width}"
+                    height="{height}"
+                    srcdoc="{src}"
+                    frameborder="0"
+                    allowfullscreen
+                ></iframe>
+                """
+    iframe = iframe.format(
+        width=config["notebook"]["iframe"]["width"].get(str),
+        height=config["notebook"]["iframe"]["height"].get(str),
+        src=html.escape(profile.to_html()),
+    )
+    return HTML(iframe)
+
+
+def get_notebook_iframe_src(profile):
+    """Get the IPython IFrame object
+
+    Args:
+        profile: The profile report object
+
+    Returns:
+        IPython IFrame object.
+    """
+    tmp_file = Path("./ipynb_tmp") / profile.get_unique_file_name()
+    tmp_file.parent.mkdir(exist_ok=True)
+    profile.to_file(tmp_file)
+    from IPython.lib.display import IFrame
+
+    return IFrame(
+        str(tmp_file),
+        width=config["notebook"]["iframe"]["width"].get(str),
+        height=config["notebook"]["iframe"]["height"].get(str),
+    )
+
+
+def display_notebook_iframe(profile):
+    """Display the profile report in an iframe in the Jupyter notebook
+
+    Args:
+        profile: The profile report object
+
+    Returns:
+        Displays the Iframe
+    """
+    from IPython.core.display import display
+
+    attribute = config["notebook"]["iframe"]["attribute"].get(str)
+    if attribute == "src":
+        output = get_notebook_iframe_src(profile)
+    elif attribute == "srcdoc":
+        output = get_notebook_iframe_srcdoc(profile)
+    else:
+        raise ValueError(
+            'Iframe Attribute can be "src" or "srcdoc" (current: {}).'.format(attribute)
+        )
+
+    display(output)
+
+
+
+
+
+
+
+

Functions

+
+
+def display_notebook_iframe(profile) +
+
+

Display the profile report in an iframe in the Jupyter notebook

+

Args

+
+
profile
+
The profile report object
+
+

Returns

+
+
Displays the Iframe
+
 
+
+
+Source code +
def display_notebook_iframe(profile):
+    """Display the profile report in an iframe in the Jupyter notebook
+
+    Args:
+        profile: The profile report object
+
+    Returns:
+        Displays the Iframe
+    """
+    from IPython.core.display import display
+
+    attribute = config["notebook"]["iframe"]["attribute"].get(str)
+    if attribute == "src":
+        output = get_notebook_iframe_src(profile)
+    elif attribute == "srcdoc":
+        output = get_notebook_iframe_srcdoc(profile)
+    else:
+        raise ValueError(
+            'Iframe Attribute can be "src" or "srcdoc" (current: {}).'.format(attribute)
+        )
+
+    display(output)
+
+
+
+def get_notebook_iframe_src(profile) +
+
+

Get the IPython IFrame object

+

Args

+
+
profile
+
The profile report object
+
+

Returns

+

IPython IFrame object.

+
+Source code +
def get_notebook_iframe_src(profile):
+    """Get the IPython IFrame object
+
+    Args:
+        profile: The profile report object
+
+    Returns:
+        IPython IFrame object.
+    """
+    tmp_file = Path("./ipynb_tmp") / profile.get_unique_file_name()
+    tmp_file.parent.mkdir(exist_ok=True)
+    profile.to_file(tmp_file)
+    from IPython.lib.display import IFrame
+
+    return IFrame(
+        str(tmp_file),
+        width=config["notebook"]["iframe"]["width"].get(str),
+        height=config["notebook"]["iframe"]["height"].get(str),
+    )
+
+
+
+def get_notebook_iframe_srcdoc(profile) +
+
+

Get the IPython HTML object with ifram with the srcdoc attribute

+

Args

+
+
profile
+
The profile report object
+
+

Returns

+

IPython HTML object.

+
+Source code +
def get_notebook_iframe_srcdoc(profile):
+    """Get the IPython HTML object with ifram with the srcdoc attribute
+
+    Args:
+        profile: The profile report object
+
+    Returns:
+        IPython HTML object.
+    """
+    from IPython.core.display import HTML
+
+    iframe = """
+                <iframe
+                    width="{width}"
+                    height="{height}"
+                    srcdoc="{src}"
+                    frameborder="0"
+                    allowfullscreen
+                ></iframe>
+                """
+    iframe = iframe.format(
+        width=config["notebook"]["iframe"]["width"].get(str),
+        height=config["notebook"]["iframe"]["height"].get(str),
+        src=html.escape(profile.to_html()),
+    )
+    return HTML(iframe)
+
+
+
+
+
+
+
+ +
+ + + + + \ No newline at end of file diff --git a/pandas_profiling/utils/notebook.py b/pandas_profiling/utils/notebook.py new file mode 100644 index 000000000..17adf4cf5 --- /dev/null +++ b/pandas_profiling/utils/notebook.py @@ -0,0 +1,8 @@ +"""Utility functions that come in handy when working with Jupyter notebooks""" + + +def full_width(): + """Resize the notebook to use it's full width""" + from IPython.core.display import display, HTML + + display(HTML("")) diff --git a/pandas_profiling/view/notebook.py b/pandas_profiling/view/notebook.py new file mode 100644 index 000000000..1ede38a16 --- /dev/null +++ b/pandas_profiling/view/notebook.py @@ -0,0 +1,79 @@ +"""Functionality related to displaying the profile report in Jupyter notebooks.""" +import html +from pathlib import Path + + +from pandas_profiling.config import config + + +def get_notebook_iframe_srcdoc(profile): + """Get the IPython HTML object with ifram with the srcdoc attribute + + Args: + profile: The profile report object + + Returns: + IPython HTML object. + """ + from IPython.core.display import HTML + + iframe = """ + + """ + iframe = iframe.format( + width=config["notebook"]["iframe"]["width"].get(str), + height=config["notebook"]["iframe"]["height"].get(str), + src=html.escape(profile.to_html()), + ) + return HTML(iframe) + + +def get_notebook_iframe_src(profile): + """Get the IPython IFrame object + + Args: + profile: The profile report object + + Returns: + IPython IFrame object. + """ + tmp_file = Path("./ipynb_tmp") / profile.get_unique_file_name() + tmp_file.parent.mkdir(exist_ok=True) + profile.to_file(tmp_file) + from IPython.lib.display import IFrame + + return IFrame( + str(tmp_file), + width=config["notebook"]["iframe"]["width"].get(str), + height=config["notebook"]["iframe"]["height"].get(str), + ) + + +def display_notebook_iframe(profile): + """Display the profile report in an iframe in the Jupyter notebook + + Args: + profile: The profile report object + + Returns: + Displays the Iframe + """ + from IPython.core.display import display + + attribute = config["notebook"]["iframe"]["attribute"].get(str) + if attribute == "src": + output = get_notebook_iframe_src(profile) + elif attribute == "srcdoc": + output = get_notebook_iframe_srcdoc(profile) + else: + raise ValueError( + 'Iframe Attribute can be "src" or "srcdoc" (current: {}).'.format(attribute) + ) + + display(output) diff --git a/pandas_profiling/view/templates/components/list.html b/pandas_profiling/view/templates/components/list.html new file mode 100644 index 000000000..a30cea5ca --- /dev/null +++ b/pandas_profiling/view/templates/components/list.html @@ -0,0 +1,12 @@ +{% if values | length > 0 %} + {% for key, value in values.items() %} +
+

{{ value['name'] }}

+ {% if 'matrix' in value %} + {{ value['name'] }} + {% elif 'value' in value %} + {{ value['value'] }} + {% endif %} +
+ {% endfor %} +{% endif %} \ No newline at end of file diff --git a/pandas_profiling/view/templates/components/section.html b/pandas_profiling/view/templates/components/section.html new file mode 100644 index 000000000..4fb3eb0f4 --- /dev/null +++ b/pandas_profiling/view/templates/components/section.html @@ -0,0 +1,7 @@ +{% if section['content'] | length > 0 %} +
+ +

{{ section['title'] }}

+
+ {{ section['content'] }} +{% endif %} \ No newline at end of file diff --git a/pandas_profiling/view/templates/components/tabs.html b/pandas_profiling/view/templates/components/tabs.html new file mode 100644 index 000000000..274618ee0 --- /dev/null +++ b/pandas_profiling/view/templates/components/tabs.html @@ -0,0 +1,26 @@ +{% if values | length > 0 %} +
+ + +
+ {% for key, value in values.items() %} +
+ {% if 'matrix' in value %} + {{ value['name'] }} + {% elif 'value' in value %} + {{ value['value'] }} + {% endif %} +
+ {% endfor %} +
+
+{% endif %} \ No newline at end of file