Skip to content

Commit

Permalink
feat: implement editable dataframe to manage dataframe editor component
Browse files Browse the repository at this point in the history
* docs: improve documentation
* docs: add dataframe section
  • Loading branch information
FabienArcellier committed Jul 22, 2024
1 parent 8bdf021 commit 36a3ea8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
15 changes: 6 additions & 9 deletions docs/framework/dataframe.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ it offers components as `dataframe` to manipulate dataframes. These components a
| `polar.DataFrame` | x |
| `list of records` | x (with `EditableDataframe`) |

### Use a dataframe
## Use a dataframe

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

Expand All @@ -24,9 +24,9 @@ wf.init_state({
})
```

### Prepare a dataframe for editing
## Prepare a dataframe for editing

**writer provides a helper to facilitate dataframe manipulatione**. This helper makes it easier to write event handlers such as adding a line,
**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
Expand All @@ -40,9 +40,7 @@ wf.init_state({
})
```

<Info>An `EditableDataframe` value can also be displayed in the `dataframe` component</Info>

#### Handle events from a dataframe editor
### 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.

Expand Down Expand Up @@ -78,9 +76,9 @@ def on_record_action(state, payload):
state['record'] = state['df'].record(record_index) # dict representation of record
```

#### Alternative to pandas.DataFrame
### Datastructures supported by `EditableDataframe`

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

```python
import pandas
Expand All @@ -91,7 +89,6 @@ 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}]
list_of_records = [[1, 4], [2, 5], [3, 6]]

wf.init_state({
'mypandas': wf.EditableDataframe(panda_df),
Expand Down
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
10 changes: 7 additions & 3 deletions src/writer/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1705,9 +1705,13 @@ def record_add(df: 'pandas.DataFrame', payload: DataframeRecordAdded) -> 'pandas
_assert_record_match_pandas_df(df, payload['record'])

record, index = _split_record_as_pandas_record_and_index(payload['record'], df.index.names)

new_df = pandas.DataFrame([record], index=[index])
return pandas.concat([df, new_df])

if isinstance(df.index, pandas.RangeIndex):
new_df = pandas.DataFrame([record])
return pandas.concat([df, new_df], ignore_index=True)
else:
new_df = pandas.DataFrame([record], index=[index])
return pandas.concat([df, new_df])

@staticmethod
def record_update(df: 'pandas.DataFrame', payload: DataframeRecordUpdated) -> 'pandas.DataFrame':
Expand Down

0 comments on commit 36a3ea8

Please sign in to comment.