jinghan23 commited on
Commit
164e312
·
0 Parent(s):

data_viewer

Browse files
Files changed (1) hide show
  1. app.py +205 -0
app.py ADDED
@@ -0,0 +1,205 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import json
3
+ import pandas as pd
4
+ from typing import Dict, List
5
+ import numpy as np
6
+
7
+ def flatten_dict(d: Dict, parent_key: str = '', sep: str = '.') -> Dict:
8
+ items = []
9
+ for k, v in d.items():
10
+ new_key = f"{parent_key}{sep}{k}" if parent_key else k
11
+ if isinstance(v, dict):
12
+ items.extend(flatten_dict(v, new_key, sep=sep).items())
13
+ else:
14
+ items.append((new_key, v))
15
+ return dict(items)
16
+
17
+ def create_preview(text: str, max_length: int = 50) -> str:
18
+ if not isinstance(text, str):
19
+ text = str(text)
20
+ if len(text) <= max_length:
21
+ return text
22
+ return text[:max_length] + "..."
23
+
24
+ class JsonViewer:
25
+ def __init__(self):
26
+ self.data = None
27
+ self.df = None
28
+
29
+ def load_json(self, file):
30
+ if file is None:
31
+ return None, None
32
+
33
+ try:
34
+ # Read JSON file
35
+ if isinstance(file, str):
36
+ with open(file, 'r', encoding='utf-8') as f:
37
+ self.data = json.load(f)
38
+ else:
39
+ content = file.decode('utf-8')
40
+ self.data = json.loads(content)
41
+
42
+ # Convert to DataFrame
43
+ if isinstance(self.data, dict):
44
+ self.df = pd.DataFrame([self.data])
45
+ elif isinstance(self.data, list):
46
+ self.df = pd.DataFrame(self.data)
47
+ else:
48
+ raise ValueError("JSON must contain either a dictionary or a list of dictionaries")
49
+
50
+ # Create preview for all columns
51
+ preview_df = self.df.copy()
52
+ for col in preview_df.columns:
53
+ preview_df[col] = preview_df[col].apply(create_preview)
54
+
55
+ return preview_df, self.df.index.tolist()
56
+ except Exception as e:
57
+ return f"Error loading JSON: {str(e)}", None
58
+
59
+ def get_full_row(self, index):
60
+ if self.df is None or index is None:
61
+ return "Please load a JSON file first"
62
+
63
+ try:
64
+ row = self.df.iloc[int(index)]
65
+ formatted_data = json.dumps(row.to_dict(), indent=2)
66
+ return formatted_data
67
+ except Exception as e:
68
+ return f"Error displaying row: {str(e)}"
69
+
70
+ def create_interface():
71
+ viewer = JsonViewer()
72
+
73
+ with gr.Blocks(css="""
74
+ #full_content {
75
+ font-family: monospace;
76
+ }
77
+ .content-container {
78
+ display: flex;
79
+ flex-wrap: wrap;
80
+ gap: 20px;
81
+ padding: 15px;
82
+ background: #f5f5f5;
83
+ border-radius: 5px;
84
+ }
85
+ .content-section {
86
+ flex: 1;
87
+ min-width: 300px;
88
+ background: white;
89
+ padding: 15px;
90
+ border-radius: 5px;
91
+ box-shadow: 0 1px 3px rgba(0,0,0,0.1);
92
+ }
93
+ .section-title {
94
+ font-weight: bold;
95
+ color: #444;
96
+ margin-bottom: 10px;
97
+ padding-bottom: 5px;
98
+ border-bottom: 2px solid #eee;
99
+ }
100
+ .section-content {
101
+ white-space: pre-wrap;
102
+ line-height: 1.5;
103
+ }
104
+ .content-key {
105
+ font-weight: bold;
106
+ color: #444;
107
+ padding-right: 10px;
108
+ }
109
+ .content-value {
110
+ display: block;
111
+ padding-left: 0;
112
+ margin-top: 5px;
113
+ }
114
+ """) as interface:
115
+ gr.Markdown("# JSON Data Viewer")
116
+
117
+ with gr.Row():
118
+ file_input = gr.File(label="Upload JSON file")
119
+
120
+ with gr.Row():
121
+ preview_table = gr.Dataframe(
122
+ value=pd.DataFrame(),
123
+ label="Data Preview",
124
+ interactive=True,
125
+ wrap=True,
126
+ row_count=(5, "dynamic")
127
+ )
128
+
129
+ with gr.Row():
130
+ full_content = gr.HTML(
131
+ label="Full Content",
132
+ elem_id="full_content"
133
+ )
134
+
135
+ def update_table(file):
136
+ preview, _ = viewer.load_json(file)
137
+ if isinstance(preview, pd.DataFrame):
138
+ return preview
139
+ return pd.DataFrame()
140
+
141
+ def show_full_content(evt: gr.SelectData):
142
+ if viewer.df is None:
143
+ return "Please load a JSON file first"
144
+ try:
145
+ row = viewer.df.iloc[evt.index[0]]
146
+ row_dict = row.to_dict()
147
+
148
+ sections = {
149
+ 'Video Info': ['vid', 'video_url', 'title', 'category', 'extended_description'],
150
+ 'Content': ['description'],
151
+ 'Additional Info': ['vqa']
152
+ }
153
+
154
+ formatted_sections = []
155
+ for section_title, keys in sections.items():
156
+ section_content = []
157
+ for key in keys:
158
+ if key in row_dict:
159
+ value = row_dict[key]
160
+ if isinstance(value, str):
161
+ value = value.replace('\\n', '<br>')
162
+ value = value.replace('\\"', '"')
163
+ value = value.replace('<br>', '<br>&nbsp;&nbsp;&nbsp;&nbsp;')
164
+ section_content.append(
165
+ f'<span class="content-key">{key}:</span>'
166
+ f'<span class="content-value">{value}</span>'
167
+ )
168
+
169
+ if section_content:
170
+ formatted_section = f"""
171
+ <div class="content-section">
172
+ <div class="section-title">{section_title}</div>
173
+ <div class="section-content">
174
+ {'<br>'.join(section_content)}
175
+ </div>
176
+ </div>
177
+ """
178
+ formatted_sections.append(formatted_section)
179
+
180
+ formatted_data = f"""
181
+ <div class="content-container">
182
+ {''.join(formatted_sections)}
183
+ </div>
184
+ """
185
+ return formatted_data
186
+ except Exception as e:
187
+ return f"Error displaying row: {str(e)}"
188
+
189
+ file_input.change(
190
+ fn=update_table,
191
+ inputs=[file_input],
192
+ outputs=[preview_table]
193
+ )
194
+
195
+ preview_table.select(
196
+ fn=show_full_content,
197
+ inputs=None,
198
+ outputs=[full_content]
199
+ )
200
+
201
+ return interface
202
+
203
+ if __name__ == "__main__":
204
+ demo = create_interface()
205
+ demo.launch(share=True)