Spaces:
Build error
Build error
| import gradio as gr | |
| import pandas as pd | |
| import os | |
| # Fixed path to Excel file | |
| EXCEL_FILE_PATH = "data.xlsx" | |
| def search_excel(query): | |
| """ | |
| Search for a query string in all columns of the Excel file | |
| and return matching rows as a DataFrame | |
| """ | |
| try: | |
| # Return empty dataframe if no query | |
| if not query: | |
| return pd.DataFrame() | |
| # Check if file exists | |
| if not os.path.exists(EXCEL_FILE_PATH): | |
| return pd.DataFrame({"Error": [f"File not found: {EXCEL_FILE_PATH}"]}) | |
| # Read Excel file | |
| df = pd.read_excel(EXCEL_FILE_PATH) | |
| # Convert all columns to string for searching | |
| df_str = df.astype(str) | |
| # Create a mask for rows where any column contains the query (case-insensitive) | |
| mask = df_str.apply(lambda x: x.str.contains(query, case=False, na=False)).any(axis=1) | |
| # Filter the dataframe using the mask | |
| result_df = df[mask] | |
| if len(result_df) == 0: | |
| return pd.DataFrame({"Results": ["No matches found"]}) | |
| return result_df | |
| except Exception as e: | |
| return pd.DataFrame({"Error": [str(e)]}) | |
| # Create Gradio interface | |
| with gr.Blocks() as app: | |
| gr.Markdown(f"# Excel File Search App") | |
| with gr.Row(): | |
| # Search input - will trigger search on typing | |
| search_input = gr.Textbox(label="Search Text", placeholder="Start typing to search...") | |
| with gr.Row(): | |
| # Output table | |
| output_table = gr.Dataframe(label="Search Results") | |
| # Define action when text is typed (real-time search) | |
| search_input.change( | |
| fn=search_excel, | |
| inputs=[search_input], | |
| outputs=output_table | |
| ) | |
| # Launch the app | |
| if __name__ == "__main__": | |
| app.launch() |