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, "sweetviz_report.html") | |
report.show_html(report_path, open_browser=False) | |
with open(report_path, 'r', encoding='utf-8') as f: | |
html_content = f.read() | |
# Add custom CSS to hide unwanted elements and improve interactivity | |
custom_css = """ | |
<style> | |
.container { | |
width: 100% !important; | |
max-width: none !important; | |
padding: 0 !important; | |
} | |
.nav-logo { | |
display: none !important; | |
} | |
.dataframe { | |
width: 100% !important; | |
} | |
.dataframe th { | |
cursor: pointer; | |
} | |
</style> | |
""" | |
html_content = custom_css + html_content | |
os.remove(report_path) | |
return html_content | |
def create_interface(): | |
analyzer = DataAnalyzer() | |
with gr.Blocks(theme=gr.themes.Soft()) as demo: | |
gr.Markdown("# Data Analysis Dashboard") | |
file_input = gr.File(label="Upload CSV") | |
report_html = gr.HTML() | |
def process_file(file): | |
if file is None: | |
return None | |
try: | |
df = pd.read_csv(file.name) | |
report = analyzer.generate_sweetviz_report(df) | |
# Add wrapper div for better styling | |
report_with_style = f""" | |
<div style="height: 800px; overflow: auto; padding: 20px;"> | |
{report} | |
</div> | |
""" | |
return report_with_style | |
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 | |
) |