eoda-dev / py-tabulator

Shiny bindings for Tabulator JS
https://eoda-dev.github.io/py-tabulator/
MIT License
16 stars 2 forks source link

having trouble using the column parameter correctly #24

Closed bkxndata closed 9 months ago

bkxndata commented 9 months ago

first off, this project is awesome. love playing around with it.

i tried to incorporate filters into the data table and had some trouble. it seems whenever I use the columns parameter it gets rid of / hides the data in the table.

Here is my sample code. just uncomment either of the columns parameters and run

import pandas as pd
from pytabulator import TableOptions, Tabulator, render_tabulator, theme, TabulatorContext
from shiny import render, reactive
from shiny.express import input, ui

table_options = TableOptions(
    height=600,
    pagination=True,
    layout="fitDataStretch",
    # columns=[{"title": "Test Title"}]
    # columns=["headerFilter": True]
)

# Set theme
theme.tabulator_modern()

ui.div("Click on row to print name.", style="padding: 10px;")
ui.input_action_button("trigger_download", "Download")

@render.code
async def txt():
    print(input.tabulator_row_clicked())
    return input.tabulator_row_clicked()["Name"]

@render_tabulator
def tabulator():
    df = pd.read_csv(
        "https://raw.githubusercontent.com/datasciencedojo/datasets/master/titanic.csv"
    )
    return Tabulator(df, table_options)
crazycapivara commented 9 months ago

@bkxndata The columns parameter is a list of column definitions, where each item describes a column to be included. See pytabulator column docs. By default all columns are included with some default values.

The easiest way to get an example of how to use the columns parameter is by using utils.create_columns:

import pandas as pd
from pytabulator.utils import create_columns

df = pd.read_csv("https://raw.githubusercontent.com/datasciencedojo/datasets/master/titanic.csv")

for col in create_columns(df):
   print(col)

# Adding a default filter to all columns:
for col in create_columns(df, default_filter=True):
   print(col)

{'title': 'PassengerId', 'field': 'PassengerId', 'hozAlign': 'right', 'headerFilter': 'number'}
{'title': 'Survived', 'field': 'Survived', 'hozAlign': 'right', 'headerFilter': 'number'}
...
crazycapivara commented 9 months ago

@bkxndata If everything works for you, I would close this issue.

bkxndata commented 9 months ago

yes! thank you so much