Skip to content

Commit

Permalink
feat: code generation template ready
Browse files Browse the repository at this point in the history
  • Loading branch information
kirangadhave committed May 22, 2024
1 parent 24f9952 commit d2e902b
Show file tree
Hide file tree
Showing 22 changed files with 2,999 additions and 220 deletions.
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"typescript.tsdk": "node_modules/typescript/lib",
"python.analysis.typeCheckingMode": "off",
"python.defaultInterpreterPath": "./.venv/persist-ext/bin/python"
"python.defaultInterpreterPath": ".venv/persist-ext/bin/python",
"python.terminal.activateEnvironment": true,
"python.analysis.autoImportCompletions": true
}
116 changes: 116 additions & 0 deletions examples/NEW.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "0221fabc-37b3-401e-b082-ffff4eed5f74",
"metadata": {
"__GENERATED_DATAFRAMES__": "ᯢ粠 ",
"__has_persist_output": true,
"trrack_graph": "ᯡ࠽䈌ʀ匦㢠⹰ࢀ㌠ᰠヶĸ׀૦䁓ؤƄ偑䔢ᤰᶠፑ☤メ槔稃ʫैkᑄᄉ妐戱ᝅ⭾凋㭎ẜʤ䀡傠Ǒ‰㥀ॢȠா䠸Žᢀ㜰䁾巓•橀樠Ꮃ䤠ව׷核ᲁ⚨ⲝീ┪殠޳ŀ硘Œ䩌┫֧ᓤ⟰˸惌⡂⥐⑅⫴℮䱣ࠉ煌ö労㾧⑔ါ矱瑖䡰㑶ж硨฾匭⫐ʘ<௎漸ധ▂ੌ痗壀拤ዎ渺⤧ദ䍠抠悑⢓␤nㆺḏֲ晒ㆄなȋ㍌歩ňፄ‡嘴ᢡ繫ץኀ㢜ᢦⁱ呚Ƭ懑䰭ឯ戩䊘䀪砒ȧ⮺⍥搸抻ॱ爊㜨仳Уဠ "
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Persist extension loaded!\n",
"Dev mode enabled!\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "45de200cce45489dbee484aae6ef1036",
"version_major": 2,
"version_minor": 1
},
"text/plain": [
"PersistWidget(data_values=[{'__id_column': '1', ';Region': 'Salt Lake', 'Year': 2012, ';Trigger': 'Snowboarder…"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import pandas as pd\n",
"import altair as alt\n",
"import persist_ext as PR\n",
"PR.enable_dev_mode()\n",
"\n",
"data = pd.read_csv(\"avalanches_data.csv\")\n",
"data.head()\n",
"\n",
"PR.PersistTable(data)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "f2be88d8-4e58-4903-862e-e0549e996a6b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
";Region object\n",
"Year int64\n",
";Trigger object\n",
";Weak Layer object\n",
"Depth_inches object\n",
";Aspect object\n",
"Day int64\n",
"Elevation_feet float64\n",
"Vertical_inches float64\n",
"Month int64\n",
"dtype: object"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data.dtypes"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cdd70eba-1855-4d60-8e83-5e9d409a8eac",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"__persist_keys_record": [
"__GENERATED_DATAFRAMES__",
"__persist_nb_uuid__",
"trrack_graph"
],
"__persist_nb_uuid__": "747414a8-b617-4746-b620-527f7a62720d",
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
2,346 changes: 2,346 additions & 0 deletions examples/avalanches_data.csv

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
"build:prod": "jlpm clean && jlpm build:lib:prod && jlpm build:labextension",
"build:labextension": "jupyter labextension build .",
"build:labextension:dev": "jupyter labextension build --development True .",
"build:lib": "tsc --sourceMap",
"build:lib:prod": "tsc",
"build:lib": "npx tsc --sourceMap",
"build:lib:prod": "npx tsc",
"clean": "jlpm clean:lib",
"clean:lib": "rimraf lib tsconfig.tsbuildinfo",
"clean:lintcache": "rimraf .eslintcache .stylelintcache",
Expand Down Expand Up @@ -83,6 +83,7 @@
"@mantine/core": "6",
"@mantine/dates": "6",
"@mantine/hooks": "6",
"@mantine/prism": "^6.0.21",
"@tabler/icons-react": "^2.40.0",
"@trrack/core": "^1.3.0",
"@trrack/vis-react": "1.5.0",
Expand Down
8 changes: 8 additions & 0 deletions persist_ext/internals/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,18 @@

dev.DEV = False

print("Persist extension loaded!")


def enable_dev_mode():
dev.DEV = True
print("Dev mode enabled!")


__all__ = [
"plot",
"dev",
"enable_dev_mode",
"PersistWidget",
"Persist",
"PersistChart",
Expand Down
4 changes: 3 additions & 1 deletion persist_ext/internals/data/idfy.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import pandas as pd

ID_COLUMN = "__id_column"


def idfy_dataframe(df, id_column):
def idfy_dataframe(df: pd.DataFrame, id_column: str = ID_COLUMN) -> pd.DataFrame:
if id_column not in df:
ids = df.index + 1
df.insert(0, id_column, ids)
Expand Down
38 changes: 34 additions & 4 deletions persist_ext/internals/widgets/base/output_processor.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
from __future__ import annotations

import pandas as pd # noqa
import altair as alt
from typing import TYPE_CHECKING

import altair as alt
import pandas as pd # noqa
from altair import Undefined
from pandas.api.types import CategoricalDtype
from persist_ext.internals.data.idfy import ID_COLUMN
from persist_ext.internals.data.utils import is_float

from persist_ext.internals.widgets.interactions.annotation import (
ANNOTATE_COLUMN_NAME,
Expand Down Expand Up @@ -42,6 +40,20 @@ def _chart_check(self, chart):
def _apply_create(self, interaction, data, chart):
return data, chart

def _code_create(self, interaction):
id_column = f'"{self.widget.id_column}"'

code = f"""
# Copy dataframe
df = df.copy(deep=True)
# Add ID column
df.insert(0, {id_column}, df.index + 1)
df[{id_column}] = df[{id_column}].apply(str)
""".rstrip()

return [code]

# ------------ Select -------------------
def _update_selection_param(
self, chart, selection, param_list_idx, value=None, store=[]
Expand Down Expand Up @@ -132,6 +144,24 @@ def _apply_select(self, interaction, data, chart):

return data, chart

def _code_select(self, interaction):
brush_type = interaction["brush_type"]

code = []

if brush_type == "non-vega":
name = interaction["name"]
values = interaction["value"]

code.append(
f"""
df['{SELECTED_COLUMN_BRUSH}'] = False
df.loc[df['{name}'].isin({values}), '{SELECTED_COLUMN_BRUSH}'] = True
""".rstrip()
)

return code

# ------------ Filter -------------------
def _apply_filter(self, interaction, data, chart):
direction = interaction["direction"]
Expand Down
10 changes: 6 additions & 4 deletions persist_ext/internals/widgets/base/widget_with_data.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import traitlets
import json

import traitlets
import traittypes
from pandas.api.types import CategoricalDtype

from persist_ext.internals.data.idfy import ID_COLUMN, idfy_dataframe
from persist_ext.internals.data.process_generate_dataset import process_generate_dataset
from persist_ext.internals.data.utils import is_float
Expand All @@ -15,7 +18,6 @@
SELECTED_COLUMN_BRUSH,
SELECTED_COLUMN_INTENT,
)
from pandas.api.types import CategoricalDtype

SUPPORTED_COLUMN_TYPES = [
"Int64",
Expand Down Expand Up @@ -78,10 +80,10 @@ def __init__(self, data, id_column, *args, **kwargs):
Add an annotation column and set it to NO_ANNOTATION
Try and infer object data types
"""

self.id_column = id_column
data = data.copy(deep=True)

if ID_COLUMN not in data:
if id_column not in data:
data = idfy_dataframe(data, id_column)

data[SELECTED_COLUMN_BRUSH] = False
Expand Down
8 changes: 7 additions & 1 deletion persist_ext/internals/widgets/base/widget_with_generation.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from pandas import DataFrame
import traitlets
from IPython import get_ipython
from pandas import DataFrame

from persist_ext.internals.data.process_generate_dataset import process_generate_dataset
from persist_ext.internals.widgets.base.widget_with_intents import WidgetWithIntents

Expand Down Expand Up @@ -30,6 +31,8 @@ def __init__(self, df_name, *args, **kwargs):
GLOBAL_GENERATION_COUNT[df_name] = 0
gdr_dynamic_counter = GLOBAL_GENERATION_COUNT[df_name] + 1

self.df_template_name = f"{df_name}_{gdr_dynamic_counter}"

super(WidgetWithGeneration, self).__init__(
gdr_dynamic_name=df_name,
gdr_dynamic_counter=gdr_dynamic_counter,
Expand Down Expand Up @@ -151,6 +154,9 @@ def create_df_name(count):
df_name = create_df_name(i)

self._only_create_dynamic_df(df_name)
self.df_template_name = df_name

self.render_code(df_name)

gen_record = self.gdr_record.copy()
gen_record[df_name] = {"dfName": df_name, "isDynamic": True}
Expand Down
Loading

0 comments on commit d2e902b

Please sign in to comment.