|
1 | 1 | # textual-pandas
|
2 |
| -A module to display Pandas dataframes in Textual |
| 2 | + |
| 3 | +A module to display Pandas DataFrames in Textual's DataTable widget. |
| 4 | + |
| 5 | +## Background |
| 6 | + |
| 7 | +Pandas is a popular Python library for data analysis. One of the core components to Pandas is the DataFrame object. A DataFrame is a two-dimensional data structure that represents data in rows and columns. |
| 8 | + |
| 9 | +Given Pandas DataFrames are tabular in nature, it makes sense to be able to render them in Textual using the DataTable widget. |
| 10 | + |
| 11 | +## Installation |
| 12 | + |
| 13 | +Install `textual-pandas` via pip: |
| 14 | + |
| 15 | +``` |
| 16 | +pip install textual-pandas |
| 17 | +``` |
| 18 | + |
| 19 | +## Getting Started |
| 20 | + |
| 21 | +`textual-pandas` introduces a new widget called `DataFrameTable`, which uses Textual's DataTable widget as a base. |
| 22 | + |
| 23 | +First, you'll need to import the `DataFrameTable` widget. |
| 24 | + |
| 25 | +```python |
| 26 | +from textual_pandas.widgets import DataFrameTable |
| 27 | +``` |
| 28 | + |
| 29 | +After importing the widget, you'll need to yield the `DataFrameTable` widget in your app's `compose()` method (like you would with the DataTable widget). Once you're ready to draw the table with your DataFrame data (most likely `on_mount()`), you'll simply call the `add_df()` method and pass in a DataFrame object. |
| 30 | + |
| 31 | +```python |
| 32 | +from textual.app import App |
| 33 | +from textual_pandas.widgets import DataFrameTable |
| 34 | +import pandas as pd |
| 35 | + |
| 36 | +# Pandas DataFrame |
| 37 | +df = pd.DataFrame() |
| 38 | +df["Name"] = ["Dan", "Ben", "Don", "John", "Jim", "Harry"] |
| 39 | +df["Score"] = [77, 56, 90, 99, 83, 69] |
| 40 | +df["Grade"] = ["C", "F", "A", "A", "B", "D"] |
| 41 | + |
| 42 | +class ClassApp(App): |
| 43 | + def compose(self): |
| 44 | + yield DataFrameTable() |
| 45 | + |
| 46 | + def on_mount(self): |
| 47 | + table = self.query_one(DataFrameTable) |
| 48 | + table.add_df(df) |
| 49 | +``` |
| 50 | + |
| 51 | +That's it! Now you're DataFrame will display within a DataTable widget! |
| 52 | + |
| 53 | +### Updating the DataFrame |
| 54 | + |
| 55 | +If you update your DataFrame and would like to see the updates in your app, then you'll need to use the `update_df` method and pass in your updated DataFrame object. |
| 56 | + |
| 57 | +If you only want to update the rendered DataTable (without the need to change the DataFrame), you may also use the `update_cell` and `update_cell_at` methods that are provided by the DataTable widget. |
| 58 | + |
| 59 | +```python |
| 60 | +df.insert( |
| 61 | + 1, |
| 62 | + "Teacher", |
| 63 | + [ |
| 64 | + "Mr. Smith", |
| 65 | + "Mr. Smith", |
| 66 | + "Mr. Smith", |
| 67 | + "Mr. Smith", |
| 68 | + "Mr. Smith", |
| 69 | + "Mr. Smith", |
| 70 | + ], |
| 71 | +) |
| 72 | +table = self.query_one(DataFrameTable) |
| 73 | +# Provide new DataFrame to update DataFrameTable widget |
| 74 | +table.update_df(df) |
| 75 | +``` |
| 76 | + |
| 77 | +## Contributing |
| 78 | + |
| 79 | +I built this module for the sole purpose of displaying a Pandas DataFrame in a DataTable widget. I'm sure there's additional functionality with the Pandas library that others would like to see! |
| 80 | + |
| 81 | +If you have any ideas, questions, or issues, please feel free to open an issue or PR! |
0 commit comments