Spaces:
Running
Running
import gradio as gr | |
import pandas as pd | |
import sweetviz as sv | |
import tempfile | |
import os | |
class DataAnalyzer: | |
def __init__(self): | |
self.temp_dir = tempfile.mkdtemp() | |
def generate_sweetviz_report(self, df): | |
report = sv.analyze(df) | |
report_path = os.path.join(self.temp_dir, "report.html") | |
report.show_html(report_path, open_browser=False) | |
with open(report_path, 'r', encoding='utf-8') as f: | |
html_content = f.read() | |
# Wrap the report in a table cell with styling | |
html_with_table = f""" | |
<table width="100%" style="border-collapse: collapse;"> | |
<tr> | |
<td style="padding: 20px; border: 1px solid #ddd;"> | |
<div style="height: 800px; overflow: auto;"> | |
{html_content} | |
</div> | |
</td> | |
</tr> | |
</table> | |
""" | |
os.remove(report_path) | |
return html_with_table | |
def create_interface(): | |
analyzer = DataAnalyzer() | |
with gr.Blocks(theme=gr.themes.Soft()) as demo: | |
gr.Markdown("# Data Analysis Dashboard") | |
with gr.Tabs(): | |
with gr.TabItem("Sweetviz Analysis"): | |
file_input = gr.File(label="Upload CSV") | |
report_html = gr.HTML() | |
with gr.TabItem("Custom Analysis"): | |
gr.Markdown("Custom analysis will be added here") | |
def process_file(file): | |
if file is None: | |
return None | |
try: | |
df = pd.read_csv(file.name) | |
return analyzer.generate_sweetviz_report(df) | |
except Exception as e: | |
return f"Error generating report: {str(e)}" | |
file_input.change( | |
fn=process_file, | |
inputs=[file_input], | |
outputs=[report_html] | |
) | |
return demo | |
if __name__ == "__main__": | |
demo = create_interface() | |
demo.launch(show_error=True) |