baconnier commited on
Commit
c9d2489
·
verified ·
1 Parent(s): 9138597

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -15
app.py CHANGED
@@ -2,8 +2,12 @@ import gradio as gr
2
  import pandas as pd
3
  import sweetviz as sv
4
  import io
 
5
 
6
  class DataAnalyzer:
 
 
 
7
  def generate_sweetviz_report(self, df):
8
  report = sv.analyze(df)
9
  html_io = io.StringIO()
@@ -11,47 +15,49 @@ class DataAnalyzer:
11
  return html_io.getvalue()
12
 
13
  def get_dataset_info(self, df):
14
- return {
15
  "Rows": len(df),
16
  "Columns": len(df.columns),
17
- "Memory Usage (MB)": df.memory_usage(deep=True).sum() / 1024**2,
18
- "Missing Values": df.isnull().sum().sum(),
19
- "Data Types": df.dtypes.value_counts().to_dict()
20
  }
 
21
 
22
  def create_interface():
23
  analyzer = DataAnalyzer()
24
 
25
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
26
- gr.Markdown("# Data Analysis Dashboard")
27
 
28
  with gr.Row():
29
  file_input = gr.File(label="Upload CSV")
30
- dataset_info = gr.JSON(label="Dataset Information")
31
 
32
- with gr.Tabs():
33
- with gr.TabItem("Sweetviz Report"):
34
- sweet_html = gr.HTML()
35
- download_btn = gr.Button("Download Report")
36
 
37
  def process_file(file):
38
  if file is None:
39
- return None, None
40
 
41
  try:
42
  df = pd.read_csv(file.name)
43
  info = analyzer.get_dataset_info(df)
44
- report_html = analyzer.generate_sweetviz_report(df)
 
 
 
 
45
 
46
- return info, report_html
47
 
48
  except Exception as e:
49
- return str(e), None
50
 
51
  file_input.change(
52
  fn=process_file,
53
  inputs=[file_input],
54
- outputs=[dataset_info, sweet_html]
55
  )
56
 
57
  return demo
 
2
  import pandas as pd
3
  import sweetviz as sv
4
  import io
5
+ import base64
6
 
7
  class DataAnalyzer:
8
+ def __init__(self):
9
+ self.current_df = None
10
+
11
  def generate_sweetviz_report(self, df):
12
  report = sv.analyze(df)
13
  html_io = io.StringIO()
 
15
  return html_io.getvalue()
16
 
17
  def get_dataset_info(self, df):
18
+ info_dict = {
19
  "Rows": len(df),
20
  "Columns": len(df.columns),
21
+ "Memory Usage (MB)": round(df.memory_usage(deep=True).sum() / 1024**2, 2),
22
+ "Missing Values": int(df.isnull().sum().sum()),
23
+ "Data Types": df.dtypes.astype(str).to_dict()
24
  }
25
+ return str(info_dict) # Convert to string to avoid JSON serialization issues
26
 
27
  def create_interface():
28
  analyzer = DataAnalyzer()
29
 
30
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
31
+ gr.Markdown("# CSV Data Analysis Dashboard")
32
 
33
  with gr.Row():
34
  file_input = gr.File(label="Upload CSV")
35
+ info_output = gr.Textbox(label="Dataset Information", lines=10)
36
 
37
+ report_html = gr.HTML()
 
 
 
38
 
39
  def process_file(file):
40
  if file is None:
41
+ return "No file uploaded.", None
42
 
43
  try:
44
  df = pd.read_csv(file.name)
45
  info = analyzer.get_dataset_info(df)
46
+ report = analyzer.generate_sweetviz_report(df)
47
+
48
+ # Create download link
49
+ b64 = base64.b64encode(report.encode()).decode()
50
+ download_link = f'<a href="data:text/html;base64,{b64}" download="analysis_report.html">Download Report</a>'
51
 
52
+ return info, report + download_link
53
 
54
  except Exception as e:
55
+ return f"Error: {str(e)}", None
56
 
57
  file_input.change(
58
  fn=process_file,
59
  inputs=[file_input],
60
+ outputs=[info_output, report_html]
61
  )
62
 
63
  return demo