Spaces:
Running
Running
import gradio as gr | |
import pandas as pd | |
from ydata_profiling import ProfileReport | |
import sweetviz as sv | |
from dataprep.eda import create_report | |
import io | |
class DataAnalyzer: | |
def __init__(self): | |
self.current_df = None | |
def generate_profile_report(self, df, minimal=False): | |
profile = ProfileReport( | |
df, | |
minimal=minimal, | |
title="Pandas Profiling Report", | |
explorative=True, | |
dark_mode=True | |
) | |
# Get HTML directly as string | |
return profile.to_html() | |
def generate_sweetviz_report(self, df): | |
report = sv.analyze(df) | |
# Use StringIO to capture the HTML output | |
html_io = io.StringIO() | |
report.show_html(filepath=html_io, open_browser=False) | |
return html_io.getvalue() | |
def generate_dataprep_report(self, df): | |
report = create_report(df) | |
# Get HTML directly as string | |
return report.html() | |
def get_dataset_info(self, df): | |
return { | |
"Rows": len(df), | |
"Columns": len(df.columns), | |
"Memory Usage (MB)": df.memory_usage(deep=True).sum() / 1024**2, | |
"Missing Values": df.isnull().sum().sum(), | |
"Data Types": df.dtypes.value_counts().to_dict() | |
} | |
def create_interface(): | |
analyzer = DataAnalyzer() | |
with gr.Blocks(theme=gr.themes.Soft()) as demo: | |
gr.Markdown(""" | |
# Data Analysis Dashboard | |
Upload your CSV file to generate interactive analysis reports | |
""") | |
with gr.Row(): | |
file_input = gr.File(label="Upload CSV") | |
dataset_info = gr.JSON(label="Dataset Information") | |
with gr.Row(): | |
report_type = gr.Radio( | |
choices=["Full", "Minimal"], | |
value="Full", | |
label="Report Type" | |
) | |
with gr.Tabs(): | |
with gr.TabItem("Pandas Profiling"): | |
profile_html = gr.HTML() | |
with gr.TabItem("Sweetviz"): | |
sweet_html = gr.HTML() | |
with gr.TabItem("DataPrep"): | |
prep_html = gr.HTML() | |
def process_file(file, report_type): | |
if file is None: | |
return None, None, None, None | |
try: | |
df = pd.read_csv(file.name) | |
analyzer.current_df = df | |
# Get dataset info | |
info = analyzer.get_dataset_info(df) | |
# Generate reports | |
minimal = report_type == "Minimal" | |
with gr.Progress() as progress: | |
progress(0, desc="Generating Pandas Profiling report...") | |
profile_html = analyzer.generate_profile_report(df, minimal) | |
progress(0.33, desc="Generating Sweetviz report...") | |
sweet_html = analyzer.generate_sweetviz_report(df) | |
progress(0.66, desc="Generating DataPrep report...") | |
prep_html = analyzer.generate_dataprep_report(df) | |
progress(1.0, desc="Done!") | |
return ( | |
info, | |
profile_html, | |
sweet_html, | |
prep_html | |
) | |
except Exception as e: | |
return str(e), None, None, None | |
file_input.change( | |
fn=process_file, | |
inputs=[file_input, report_type], | |
outputs=[dataset_info, profile_html, sweet_html, prep_html] | |
) | |
report_type.change( | |
fn=process_file, | |
inputs=[file_input, report_type], | |
outputs=[dataset_info, profile_html, sweet_html, prep_html] | |
) | |
return demo | |
# Add custom CSS for better HTML rendering | |
custom_css = """ | |
<style> | |
.report-container { | |
width: 100%; | |
height: 800px; | |
overflow: auto; | |
} | |
.report-container iframe { | |
width: 100%; | |
height: 100%; | |
border: none; | |
} | |
</style> | |
""" | |
# Launch the interface | |
if __name__ == "__main__": | |
demo = create_interface() | |
demo.launch( | |
share=True, # Enable sharing | |
height=1000, # Set interface height | |
show_error=True, # Show detailed error messages | |
custom_css=custom_css # Apply custom styling | |
) |