File size: 4,474 Bytes
0cb60c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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
    )