0

I'm working on a shiny app that analyzes pandas dataframes. The program displays an error for downloading after generating the new dataframe. I did not figure out how to have the app get the new pandas dataframe in the same way as pd.DataFrame.to_csv() does.

from shiny import App, ui,reactive from shiny.ui import tags, h2

import pandas as pd

# A card component wrapper.
def ui_card(title, *args):
    return (
        ui.div(
            {"class": "card mb-4"},
            ui.div(title, class_="card-header"),
            ui.div({"class": "card-body"}, *args),
        ),
    )
    
app_ui = ui.page_fluid(
    ui.panel_title("pyPARAGON network inference and analysis tool"),


    ### PANEL 1 Gene list
    ui.panel_well(
        tags.h4("Choose a file to upload"),
        ui.row(       
                ui.input_file(
                    "initial_file", "", multiple=True
                )
            )
         ,
        ui.row(
            ui.column(
                4,
                ui.input_select(
                    "Column1", "Select input column", {})
        
                    ),
            ui.column(
                4,
                ui.input_select(
                    "Column2", "Select weight column", {})
                
                    )
            )
),

            # download panel
            ui_card(
                ui.download_button("download_new_df", "Download_selected_tsv"),
            ),

    ui.output_text_verbatim("txt")
)
            
def server(input, output, session):

    
    @reactive.Effect
    def _():
        file_infos = input.initial_file()   
        
        Column1=input.Column1()
        Column2=input.Column2()
        
        if not file_infos:
            return
        #print(file_infos)
        for file_info in file_infos:
            input_df=pd.read_csv(file_info["datapath"],sep="\t")   
            input_name=".".join(file_info["name"].split(".")[:-1])       
            columns=input_df.columns.to_list()

        print (columns)
        # -- UPDATE and select the seed node column
        ui.update_select(
            id="Column1", 
            label="Select input column", 
            choices=[""]+columns,
            selected=f'{Column1}'
            ,
        )
                
        # -- UPDATE and select the seed node column
        ui.update_select(
            id="Column2", 
            label="Select weight column", 
            choices=[""]+columns,
            selected=f'{Column2}'
            ,
        )
        print(input_df)

        if Column1 and Column2:
            new_df=input_df[[Column1,Column2]]
        else:
            return

        @session.download(filename=f'{input_name}.tab')
        def download1():
            return new_df

app = App(app_ui, server, debug=True)
M--
  • 25,431
  • 8
  • 61
  • 93

1 Answers1

0

I solved the problem by changing the download1 function as follows:

def download1():  
    yield new_df.to_string(index=False)
NelsonGon
  • 13,015
  • 7
  • 27
  • 57