File size: 2,122 Bytes
bb9042b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70b1129
 
e3f49e9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# Imports
import pandas as pd
import gradio as gr

# Hay que definir una funcion que tome el archivo, lo lea y aplique los filtros segun un listado criterios
def read_pdf(file):
    file_name = file.name
    global df
    df = pd.read_excel(file_name)
    return df.head(10)

# Esta funcion aplica el filtro seleccionado
def apply_filter(criteria):
    global df
    # Si los datos no se leyeron correctamente
    if df is None:
        return pd.DataFrame() # Devuelvo un df vacio
    # Aplico el filtro seleccionado
    if criteria == 'Criterio 1':
        filter_cond = (df['cyl']>=6) & (df['hp']>=150)
    elif criteria == 'Criterio 2':
        filter_cond = df['gear']>4
    elif criteria == 'Criterio 3':
        filter_cond = (df['disp']>150) & (df['disp']<230) & (df['qsec']>16) & (df['qsec']<18)

    global filtered_df    
    filtered_df = df[filter_cond]
    return filtered_df

def download_file(filter_df):
    output_name = 'output.xlsx'
    filter_df.to_excel(output_name)
    return output_name

with gr.Blocks() as demo:
    with gr.Tab(label='Carga Archivo'):
        input_file = gr.components.File(label='Excel-CSV File', file_types=['.xlsx', '.xls', '.csv'], file_count='single')
        send_file = gr.Button(label='Cargar')

    with gr.Tab(label='Ajustes'):
        table = gr.DataFrame(label='Tabla')
        filter_select = gr.Dropdown(choices=['Criterio 1', 'Criterio 2', 'Criterio 3'], label='Selección de criterios')
        apply_filter_button = gr.Button(label='Aplicar filtro')

    with gr.Tab(label='Descarga'):
        # output_file = gr.components.File(label='Descarga de archivo', file_count='single')
        # download_format = gr.Dropdown(choices=['csv', 'xlsx'], label='Format')
        download_button = gr.Button(label='Descarga de archivo')
        download_button.click(fn=download_file, inputs=[table], outputs=gr.components.File(label="Download Processed Excel file"))

    # Funcionalidades
    send_file.click(fn=read_pdf, inputs=[input_file], outputs=table)
    apply_filter_button.click(fn=apply_filter, inputs=[filter_select], outputs=table)

demo.launch(inline=False)