Skip to main content

A module to build Textual widgets that represent data from the Pandas library

Project description

textual-pandas

A module to display Pandas DataFrames in Textual's DataTable widget.

Background

Pandas is a popular Python library for data analysis. One of the core components of Pandas is the DataFrame object. A DataFrame is a two-dimensional data structure that represents data in rows and columns.

Given Pandas DataFrames are tabular in nature, it makes sense to be able to render them in Textual using the DataTable widget. Enter the DataFrameTable widget...

Installation

Install textual-pandas via pip:

pip install textual-pandas

Getting Started

textual-pandas introduces a new widget called DataFrameTable, which uses Textual's DataTable widget as a base.

First, you'll need to import the DataFrameTable widget.

from textual_pandas.widgets import DataFrameTable

After importing the widget, you'll need to yield the DataFrameTable widget in your Textual 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 Pandas DataFrame.

from textual.app import App
from textual_pandas.widgets import DataFrameTable
import pandas as pd

# Pandas DataFrame
df = pd.DataFrame()
df["Name"] = ["Dan", "Ben", "Don", "John", "Jim", "Harry"]
df["Score"] = [77, 56, 90, 99, 83, 69]
df["Grade"] = ["C", "F", "A", "A", "B", "D"]

class ClassApp(App):
    def compose(self):
        yield DataFrameTable()

    def on_mount(self):
        table = self.query_one(DataFrameTable)
        table.add_df(df)

That's it! Now your DataFrame will display within a DataTable widget!

Updating the DataFrame

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.

If you only want to update the rendered DataFrameTable (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.

df.insert(
    1,
    "Teacher",
    [
        "Mr. Smith",
        "Mr. Smith",
        "Mr. Smith",
        "Mr. Smith",
        "Mr. Smith",
        "Mr. Smith",
    ],
)
table = self.query_one(DataFrameTable)
# Provide new DataFrame to update DataFrameTable widget
table.update_df(df)

Contributing

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!

If you have any ideas, questions, or issues, please feel free to open an issue or PR!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

textual_pandas-0.2.2.tar.gz (3.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

textual_pandas-0.2.2-py3-none-any.whl (3.8 kB view details)

Uploaded Python 3

File details

Details for the file textual_pandas-0.2.2.tar.gz.

File metadata

  • Download URL: textual_pandas-0.2.2.tar.gz
  • Upload date:
  • Size: 3.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.9.13 Darwin/23.2.0

File hashes

Hashes for textual_pandas-0.2.2.tar.gz
Algorithm Hash digest
SHA256 0f2e7bff259389dbf5a2c498ff27214d47741b4773ccfaa8277aefa9bdd8469a
MD5 94eae0d4525a80df17aceb2fb635e480
BLAKE2b-256 e5700e633a2d502cc4e26a94805749543c5ccf9a84db4f1b31d008700734c8ea

See more details on using hashes here.

File details

Details for the file textual_pandas-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: textual_pandas-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 3.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.9.13 Darwin/23.2.0

File hashes

Hashes for textual_pandas-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 900d287460eb192429a4cbfa20bbe3b9aa57856520e47f3c8541d1ae8ee8c5e1
MD5 f20bb9bde6161772604b5213306f8f5c
BLAKE2b-256 58d3e418a162640dce84dc1221eae0b7aed1063c490b7c23d261692ed812b13b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page