Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to Create a Button for Downloading data #326

Open
fcarpentey75012 opened this issue Dec 6, 2024 · 0 comments
Open

How to Create a Button for Downloading data #326

fcarpentey75012 opened this issue Dec 6, 2024 · 0 comments

Comments

@fcarpentey75012
Copy link

fcarpentey75012 commented Dec 6, 2024

Hello team,

I am currently working on a project (on Jupyter Notebook Server) where I need to provide users with the ability to download pandas DataFrames as Excel or JSON files directly from a Jupyter Notebook using ipyvuetify buttons.

I have implemented a solution that uses base64 encoding to create download links, but I am looking for feedback or suggestions on best practices or alternative approaches that might be more efficient or aligned with ipyvuetify's capabilities.

Here is a simplified version of my current implementation:


import ipyvuetify as v
import pandas as pd
from io import BytesIO
import base64

# Sample DataFrame
df = pd.DataFrame({
    'A': [1, 2, 3],
    'B': [4, 5, 6]
})

def create_excel_download_link(df):
    output = BytesIO()
    df.to_excel(output, index=False)
    output.seek(0)
    b64 = base64.b64encode(output.getvalue()).decode()
    return f"data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64,{b64}"

# Create download buttons
excel_output_button = v.Btn(
    children=[
        v.Icon(left=True, children=['mdi-file-excel']),
        'Download Output Excel'
    ],
    color='success',
    class_='ma-2'
)

excel_input_button = v.Btn(
    children=[
        v.Icon(left=True, children=['mdi-file-excel']),
        'Download Input Excel'
    ],
    color='success',
    class_='ma-2'
)

# Function to handle button click
def on_excel_button_click(button, df):
    button.href = create_excel_download_link(df)
    button.target = '_blank'
    button.download = 'data.xlsx'

# Attach event handlers
excel_input_button.on_event('click', lambda *args: on_excel_button_click(excel_input_button, df))
excel_output_button.on_event('click', lambda *args: on_excel_button_click(excel_output_button, df))

# Display buttons in a card
card = v.Card(
    children=[
        v.CardTitle(class_='headline', children=['Download Data']),
        v.CardText(children=[excel_input_button, excel_output_button])
    ]
)

Did you have an idea ?

Thank you,
Florian

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant