plotly / plotly/dash

dcc.send_data_frame polars support

Aperta
#3,153 3 commenti 2 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

feature P2
Lingua principale
Python
Stelle
24.4k
Fork
2.3k
Merge medio
2g 7h
PR unite (30g)
13

Descrizione

Currently dcc.send_data_frame only supports pandas-style writers. I was wondering if polars support can be added as well. Previously, I used to do all my operations in polars and then on the final step when I need to download the dataframe I converted it to pandas to utilize the dcc.send_data_frame function. However, now im using my solution below. I am wondering if we can add a feature to support both pandas and polars via the send_data_frame function

Workaround solution to use polars to send dataframe:

import polars as pl
from dash import Dash, html, dcc, callback, Output, Input
import io
import base64

def polars_to_send_data_frame(df: pl.DataFrame, filename: str, **csv_kwargs):
    buffer = io.StringIO()
    df.write_csv(buffer, **csv_kwargs)
    
    return {
        'content': base64.b64encode(buffer.getvalue().encode('utf-8')).decode('utf-8'),
        'filename': filename,
        'type': 'text/csv',
        'base64': True
    }

app = Dash(__name__)

# Sample data
df = pl.DataFrame({
    'A': range(5),
    'B': ['foo', 'bar', 'baz', 'qux', 'quux'],
    'C': [1.1, 2.2, 3.3, 4.4, 5.5]
})

app.layout = html.Div([
    html.Button("Download CSV", id="btn"),
    dcc.Download(id="download")
])

@callback(
    Output("download", "data"),
    Input("btn", "n_clicks"),
    prevent_initial_call=True
)
def download_csv(n_clicks):
    return polars_to_send_data_frame(df, "data.csv")

if __name__ == '__main__':
    app.run(debug=True)

I would love to contribute and make a pr to add polars support!

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia individuando l'entry point dcc.send_data_frame e i test esistenti per i writer in stile pandas. Aggiungi la copertura per il supporto richiesto ai dataframe Polars insieme al comportamento attuale e considera il workaround fornito come il comportamento CSV previsto. Il lavoro è completato quando dcc.send_data_frame accetta i dataframe Polars senza richiedere una conversione a pandas.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
data
Tipo di issue
Funzionalità
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.