Skip to content

Commit

Permalink
feat: added filter to locate columns (#1115)
Browse files Browse the repository at this point in the history
* feat: added filter to locate columns

* Update .pre-commit-config.yaml

* Apply suggestions from code review

Co-authored-by: Aarni Koskela <[email protected]>

* feat: added support for variable search in ipywidgets

* fix: variables not being filtered according to dropdown

* fix: variables not being filtered according to dropdown

* fix: fixed order of sections

Co-authored-by: Aarni Koskela <[email protected]>
  • Loading branch information
g-kabra and akx authored Oct 20, 2022
1 parent e0a6c56 commit 7885d12
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 11 deletions.
2 changes: 2 additions & 0 deletions src/pandas_profiling/report/presentation/core/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from pandas_profiling.report.presentation.core.alerts import Alerts
from pandas_profiling.report.presentation.core.collapse import Collapse
from pandas_profiling.report.presentation.core.container import Container
from pandas_profiling.report.presentation.core.dropdown import Dropdown
from pandas_profiling.report.presentation.core.duplicate import Duplicate
from pandas_profiling.report.presentation.core.frequency_table import FrequencyTable
from pandas_profiling.report.presentation.core.frequency_table_small import (
Expand All @@ -19,6 +20,7 @@
"Collapse",
"Container",
"Duplicate",
"Dropdown",
"FrequencyTable",
"FrequencyTableSmall",
"HTML",
Expand Down
21 changes: 21 additions & 0 deletions src/pandas_profiling/report/presentation/core/dropdown.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from typing import Any, Callable

from pandas_profiling.report.presentation.core.item_renderer import ItemRenderer
from pandas_profiling.report.presentation.core.container import Container
from pandas_profiling.report.presentation.core.renderable import Renderable

class Dropdown(ItemRenderer):
def __init__(self, name: str, id: str, items: list, item: Container, anchor_id: str, **kwargs):
super().__init__("dropdown", {"name": name, "id": id, "items": items, "item": item, "anchor_id": anchor_id,}, **kwargs)

def __repr__(self) -> str:
return "Dropdown"

def render(self) -> Any:
raise NotImplementedError()

@classmethod
def convert_to_class(cls, obj: Renderable, flv: Callable) -> None:
obj.__class__ = cls
if "item" in obj.content:
flv(obj.content["item"])
6 changes: 6 additions & 0 deletions src/pandas_profiling/report/presentation/flavours/flavours.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def get_html_renderable_mapping() -> Dict[Type[Renderable], Type[Renderable]]:
Alerts,
Collapse,
Container,
Dropdown,
Duplicate,
FrequencyTable,
FrequencyTableSmall,
Expand All @@ -39,6 +40,7 @@ def get_html_renderable_mapping() -> Dict[Type[Renderable], Type[Renderable]]:
HTMLAlerts,
HTMLCollapse,
HTMLContainer,
HTMLDropdown,
HTMLDuplicate,
HTMLFrequencyTable,
HTMLFrequencyTableSmall,
Expand All @@ -63,6 +65,7 @@ def get_html_renderable_mapping() -> Dict[Type[Renderable], Type[Renderable]]:
FrequencyTableSmall: HTMLFrequencyTableSmall,
Alerts: HTMLAlerts,
Duplicate: HTMLDuplicate,
Dropdown: HTMLDropdown,
Sample: HTMLSample,
ToggleButton: HTMLToggleButton,
Collapse: HTMLCollapse,
Expand Down Expand Up @@ -90,6 +93,7 @@ def get_widget_renderable_mapping() -> Dict[Type[Renderable], Type[Renderable]]:
Collapse,
Container,
Duplicate,
Dropdown,
FrequencyTable,
FrequencyTableSmall,
Image,
Expand All @@ -105,6 +109,7 @@ def get_widget_renderable_mapping() -> Dict[Type[Renderable], Type[Renderable]]:
WidgetCollapse,
WidgetContainer,
WidgetDuplicate,
WidgetDropdown,
WidgetFrequencyTable,
WidgetFrequencyTableSmall,
WidgetHTML,
Expand All @@ -129,6 +134,7 @@ def get_widget_renderable_mapping() -> Dict[Type[Renderable], Type[Renderable]]:
FrequencyTableSmall: WidgetFrequencyTableSmall,
Alerts: WidgetAlerts,
Duplicate: WidgetDuplicate,
Dropdown: WidgetDropdown,
Sample: WidgetSample,
ToggleButton: WidgetToggleButton,
Collapse: WidgetCollapse,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from pandas_profiling.report.presentation.flavours.html.alerts import HTMLAlerts
from pandas_profiling.report.presentation.flavours.html.collapse import HTMLCollapse
from pandas_profiling.report.presentation.flavours.html.container import HTMLContainer
from pandas_profiling.report.presentation.flavours.html.dropdown import HTMLDropdown
from pandas_profiling.report.presentation.flavours.html.duplicate import HTMLDuplicate
from pandas_profiling.report.presentation.flavours.html.frequency_table import (
HTMLFrequencyTable,
Expand All @@ -25,6 +26,7 @@
"HTMLCollapse",
"HTMLContainer",
"HTMLDuplicate",
"HTMLDropdown",
"HTMLFrequencyTable",
"HTMLFrequencyTableSmall",
"HTMLHTML",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from pandas_profiling.report.presentation.core import Dropdown
from pandas_profiling.report.presentation.flavours.html import templates


class HTMLDropdown(Dropdown):
def render(self) -> str:
return templates.template("dropdown.html").render(**self.content)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<select name="{{ name }}" id="{{ id }}">
<option value="">Select Columns</option>
{% for item in items %}
<option value="{{ item }}">{{ item }}</option>
{% endfor %}
</select>

{{ item.render() }}
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,14 @@ $("a[href^='#'].anchor").on('click', function (e) {
window.location.hash = hash;
});

});
});

$("select#variables-dropdown").on("change", function (e) {
var searchText = $("select#variables-dropdown").val().toLowerCase();
var variables = $(".variable");
variables.each(function (index) {
var isMatch = $(this.firstChild.firstChild).attr("title").toLowerCase() == (searchText);
if(searchText == ""){isMatch = true};
$(this).parent().toggle(isMatch);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
from pandas_profiling.report.presentation.flavours.widget.duplicate import (
WidgetDuplicate,
)
from pandas_profiling.report.presentation.flavours.widget.dropdown import (
WidgetDropdown,
)
from pandas_profiling.report.presentation.flavours.widget.frequency_table import (
WidgetFrequencyTable,
)
Expand All @@ -29,6 +32,7 @@
"WidgetCollapse",
"WidgetContainer",
"WidgetDuplicate",
"WidgetDropdown",
"WidgetFrequencyTable",
"WidgetFrequencyTableSmall",
"WidgetHTML",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from ipywidgets import widgets

from pandas_profiling.report.presentation.core import Dropdown


class WidgetDropdown(Dropdown):
def render(self) -> widgets.VBox:
dropdown = widgets.Dropdown(
options=self.content["items"],
description=self.content["name"]
)
titles = []
item = self.content["item"].content["items"]
for i in item:
titles.append(i.name)
item = self.content["item"].render()
def change_view(widg: dict) -> None:
if(dropdown.value == ""):
item.selected_index = None
else:
for i in range(len(titles)):
if(titles[i] == dropdown.value):
item.selected_index = i
break

dropdown.observe(change_view, names=["value"])

if(self.content["item"] != None):
return widgets.VBox([dropdown, item])
else:
return widgets.Vbox([dropdown])
42 changes: 32 additions & 10 deletions src/pandas_profiling/report/structure/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
HTML,
Collapse,
Container,
Dropdown,
Duplicate,
)
from pandas_profiling.report.presentation.core import Image as ImageWidget
Expand Down Expand Up @@ -59,6 +60,12 @@ def render_variables_section(config: Settings, dataframe_summary: dict) -> list:
"""

templs = []
# dropdown = Dropdown(
# name="variables-dropdown",
# id="variables-dropdown",
# items=list(dataframe_summary["variables"]),
# )
# templs.append(dropdown)

descriptions = config.variables.descriptions
show_description = config.show_variable_description
Expand Down Expand Up @@ -98,7 +105,8 @@ def render_variables_section(config: Settings, dataframe_summary: dict) -> list:
template_variables.update(summary)

# Per type template variables
render_map_type = render_map.get(summary["type"], render_map["Unsupported"])
render_map_type = render_map.get(
summary["type"], render_map["Unsupported"])
template_variables.update(render_map_type(config, template_variables))

# Ignore these
Expand All @@ -109,7 +117,8 @@ def render_variables_section(config: Settings, dataframe_summary: dict) -> list:

bottom = None
if "bottom" in template_variables and template_variables["bottom"] is not None:
btn = ToggleButton("Toggle details", anchor_id=template_variables["varid"])
btn = ToggleButton(
"Toggle details", anchor_id=template_variables["varid"])
bottom = Collapse(btn, template_variables["bottom"])

var = Variable(
Expand Down Expand Up @@ -177,7 +186,8 @@ def get_sample_items(sample: dict) -> List[Sample]:
List of sample items to show in the interface.
"""
items = [
Sample(sample=obj.data, name=obj.name, anchor_id=obj.id, caption=obj.caption)
Sample(sample=obj.data, name=obj.name,
anchor_id=obj.id, caption=obj.caption)
for obj in sample
]
return items
Expand Down Expand Up @@ -242,25 +252,37 @@ def get_report_structure(config: Settings, summary: dict) -> Root:
name="Overview",
anchor_id="overview",
),
Container(
render_variables_section(config, summary),
sequence_type="accordion",
name="Variables",
anchor_id="variables",
),
]

if len(summary["variables"]) > 0:
section_items.append(
Dropdown(
name="Variables",
anchor_id="variables-dropdown",
id="variables-dropdown",
items=list(summary["variables"]),
item=Container(
render_variables_section(config, summary),
sequence_type="accordion",
name="Variables",
anchor_id="variables",
),
)
)

scatter_items = get_scatter_matrix(config, summary["scatter"])
if len(scatter_items) > 0:
section_items.append(
Container(
scatter_items,
sequence_type="tabs" if len(scatter_items) <= 10 else "select",
sequence_type="tabs" if len(
scatter_items) <= 10 else "select",
name="Interactions",
anchor_id="interactions",
),
)


corr = get_correlation_items(config, summary)
if corr is not None:
section_items.append(corr)
Expand Down

0 comments on commit 7885d12

Please sign in to comment.