Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement editable dataframe to manage dataframe editor component #485

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions alfred/ci.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ def ci_mypy():
alfred.run("mypy ./src/writer --exclude app_templates/*")

@alfred.command("ci.ruff", help="linting with ruff")
def ci_ruff():
alfred.run("ruff check")
@alfred.option('--fix', '-f', help="fix linting errors", is_flag=True, default=False)
def ci_ruff(fix):
if fix:
alfred.run("ruff check --fix")
else:
alfred.run("ruff check")

@alfred.command("ci.pytest", help="run pytest on ./tests")
def ci_test():
Expand Down
98 changes: 98 additions & 0 deletions docs/framework/dataframe.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
---
title: "Dataframe"
---

**writer framework places the dataframe at the core of the application**. This is a great way for modeling a complex and massive data system.
it offers components as `dataframe` to manipulate dataframes. These components allow you to visualize and interact with dataframes.

| compatibility | dataframe |
|--------------------|---------------------------------------|
| `pandas.DataFrame` | x |
| `polar.DataFrame` | x |
| `list of records` | x (with `EditableDataframe`) |

## Use a dataframe

**a dataframe is simply added to the state**. A component like `dataframe` will be able to display it.

```python
import pandas
import writer as wf

wf.init_state({
'mydf': pandas.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]})
})
```

## Prepare a dataframe for editing

**writer provides `EditableDataframe` as a helper to facilitate manipulation**. it makes it easier to write event handlers such as adding a line,
deleting it or modifying a value, etc...

```python
import pandas
import writer as wf

df = pandas.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]})

wf.init_state({
'mydf': wf.EditableDataframe(df)
})
```

### Handle events from a dataframe editor

**The dataframe component emits events when an action is performed**. You must subscribe to events to integrate changes to the state of the application.

```python
import pandas
import writer as wf

df = pandas.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]})
wf.init_state({
'mydf': wf.EditableDataframe(df)
})

# Subscribe this event handler to the `wf-dataframe-add` event
def on_record_add(state, payload):
FabienArcellier marked this conversation as resolved.
Show resolved Hide resolved
payload['record']['sales'] = 0 # default value inside the dataframe
state['mydf'].record_add(payload)


# Subscribe this event handler to the `wf-dataframe-update` event
def on_record_change(state, payload):
state['mydf'].record_update(payload)


# Subscribe this event handler to the `wf-dataframe-action` event
def on_record_action(state, payload):
"""
This event corresponds to a quick action in the drop-down menu to the left of the dataframe.
"""
record_index = payload['record_index']
if payload.action == 'remove':
state['mydf'].record_remove(payload)
if payload.action == 'open':
state['record'] = state['df'].record(record_index) # dict representation of record
```

### Datastructures supported by `EditableDataframe`

`EditableDataframe` can be used with a panda dataframe, a polar dataframe and list of records.

```python
import pandas
import polars

import writer as wf

panda_df = pandas.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]})
polars_df = polars.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]})
list_of_records = [{'a': 1, 'b': 4}, {'a': 2, 'b': 5}, {'a': 3, 'b': 6}]

wf.init_state({
'mypandas': wf.EditableDataframe(panda_df),
'mypolars': wf.EditableDataframe(polars_df),
'mylistofrecords': wf.EditableDataframe(list_of_records)
})
```
1 change: 1 addition & 0 deletions docs/mint.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"framework/event-handlers",
"framework/builder-basics",
"framework/handling-inputs",
"framework/dataframe",
"framework/backend-driven-ui",
"framework/stylesheets",
"framework/frontend-scripts",
Expand Down
1 change: 1 addition & 0 deletions src/writer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from writer.core import (
BytesWrapper,
Config,
EditableDataframe,
FileWrapper,
Readable,
State,
Expand Down
Loading
Loading