-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(category): categories are now tracked
- Loading branch information
1 parent
8d49cd0
commit fe47576
Showing
46 changed files
with
1,923 additions
and
1,780 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,62 @@ | ||
import persist_ext.internals.data.get_generated_df as df | ||
import altair as alt | ||
from persist_ext.internals.data.idfy import ID_COLUMN | ||
import persist_ext.internals.vis as vis | ||
from persist_ext.internals.utils.logger import Out, logger | ||
from persist_ext.internals.data.prepare import prepare | ||
from persist_ext.internals.utils import dev | ||
from persist_ext.internals.utils.logger import Out, logger | ||
from persist_ext.internals.widgets.persist_output.widget import ( | ||
DEFAULT_DATA_ACCESSOR, | ||
PersistWidget, | ||
) | ||
from persist_ext.internals.widgets.vegalite_chart.utils import ( | ||
pop_data_defs_from_charts_recursive, | ||
) | ||
|
||
dev.DEV = False | ||
|
||
|
||
def Persist( | ||
chart=None, | ||
data=None, | ||
df_name="persist_df", | ||
id_column=ID_COLUMN, | ||
data_accessor=DEFAULT_DATA_ACCESSOR, | ||
): | ||
if chart is None and data is None: | ||
raise ValueError( | ||
"Need a valid vega altair chart and/or dataframe to be provided." | ||
) | ||
|
||
# If visualizing charts | ||
if chart is not None: | ||
if data is None: # if data is not pass explicitly | ||
chart_data = getattr(chart, "data", alt.Undefined) | ||
if chart_data is alt.Undefined: # if chart does not have top level data | ||
raise ValueError( | ||
""" | ||
Cannot infer dataset from vega altair specification. The data might be specified in subcharts. | ||
Persist does not support such charts. | ||
Please provide data at the top, or pass in the dataset explicitly as second arugment. | ||
""" | ||
) | ||
chart_data = prepare(chart_data, id_column) | ||
chart.data = chart_data | ||
else: # if data is passed | ||
chart = pop_data_defs_from_charts_recursive(chart, []) | ||
chart.data = prepare(data, id_column) | ||
|
||
print("Displaying an interactive Vega-Altair Chart") | ||
return PersistWidget( | ||
chart, df_name=df_name, id_column=id_column, data_accessor=data_accessor | ||
) | ||
|
||
if data is not None: # if only showing dataframe | ||
data = prepare(data, id_column) | ||
print("Displaying an interactive DataTable") | ||
return PersistWidget( | ||
data, df_name=df_name, id_column=id_column, data_accessor=data_accessor | ||
) | ||
|
||
|
||
__all__ = ["vis", "logger", "Out", "df", "prepare"] | ||
__all__ = ["vis", "logger", "Out", "df", "prepare", "dev", "Persist"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
def is_int(value): | ||
if isinstance(value, int): | ||
return True | ||
|
||
try: | ||
int(value) | ||
return True | ||
except ValueError: | ||
return False | ||
|
||
|
||
def is_float(value): | ||
if isinstance(value, float): | ||
return True | ||
|
||
try: | ||
float(value) | ||
return True | ||
except ValueError: | ||
return False | ||
|
||
|
||
def is_numeric(value): | ||
return is_int(value) or is_float(value) | ||
|
||
|
||
def is_str(value): | ||
return isinstance(value, str) | ||
|
||
|
||
def set_df_attr(df, key, value): | ||
df.attrs[key] = value | ||
|
||
|
||
def get_df_attr(df, key): | ||
return df.attrs[key] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
from IPython import get_ipython | ||
from IPython.core.magic import Magics, magics_class, cell_magic, needs_local_scope | ||
from IPython.core.magic_arguments import magic_arguments, argument, parse_argstring | ||
from altair import TopLevelSpec | ||
from pandas import DataFrame | ||
|
||
from persist_ext.internals.widgets.persist_output.widget import PersistWidget | ||
|
||
|
||
@magics_class | ||
class PersistMagic(Magics): | ||
@staticmethod | ||
def initialize(): | ||
get_ipython().register_magics(PersistMagic) | ||
|
||
def __init__(self, shell): | ||
super(PersistMagic, self).__init__(shell) | ||
self.call_counter = 0 | ||
|
||
@cell_magic | ||
@magic_arguments() | ||
@needs_local_scope | ||
@argument( | ||
"--df_name", | ||
dest="df_name", | ||
help="Enter name to use for dynamic dataframe", | ||
default="persist_df", | ||
) | ||
def persist_cell(self, line, cell, local_ns): | ||
args = parse_argstring(self.persist_cell, line) | ||
|
||
execution_result = get_ipython().run_cell(cell) | ||
result = execution_result.result | ||
|
||
if not isinstance(result, DataFrame) and not isinstance(result, TopLevelSpec): | ||
return result | ||
|
||
# dataframe_name_ids = dict() | ||
# | ||
# tree = ast.parse(cell) | ||
# | ||
# # Walk throught the AST and assign uids to all dataframe variables | ||
# # Store the mappings in a dict | ||
# for node in ast.walk(tree): | ||
# if isinstance(node, ast.Name): | ||
# var_name = node.id | ||
# var_value = local_ns[var_name] | ||
# | ||
# if isinstance(var_value, DataFrame): | ||
# persist_uid = uuid.uuid4() | ||
# set_df_attr(local_ns[var_name], "", persist_uid) | ||
# print(local_ns[var_name].attrs) | ||
# dataframe_name_ids[persist_uid] = var_name | ||
|
||
PersistWidget(result, args.df_name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
DEV = True | ||
DEV = False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import anywidget | ||
|
||
from persist_ext.internals.utils.entry_paths import get_widget_esm_css | ||
|
||
|
||
class BaseAnyWidget(anywidget.AnyWidget): | ||
""" | ||
Basic anywidget setup by _esm and _css | ||
""" | ||
|
||
def __init__(self, widget_key=None, *args, **kwargs): | ||
if widget_key is None: | ||
raise ValueError("widget_key cannot be none") | ||
|
||
esm, css = get_widget_esm_css(widget_key) | ||
self._esm = esm | ||
self._css = css | ||
|
||
if type(self) is BaseAnyWidget: | ||
raise NotImplementedError("Cannot create instance of this base class") | ||
|
||
super(BaseAnyWidget, self).__init__(*args, **kwargs) |
Oops, something went wrong.