File size: 14,490 Bytes
e15a3ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
import os
import io
import json
import logging
import pandas as pd
from flask import Flask, render_template, request, jsonify, send_file, session
from werkzeug.utils import secure_filename

# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)

app = Flask(__name__)
app.secret_key = os.urandom(24)
app.config['MAX_CONTENT_LENGTH'] = 50 * 1024 * 1024  # 50MB limit
app.config['UPLOAD_FOLDER'] = '/tmp/uploads'

# Ensure upload directory exists
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)

ALLOWED_EXTENSIONS = {'csv', 'json', 'xlsx'}

def allowed_file(filename):
    return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS

def check_robustness(file_stream):
    """Check for null bytes and other safety constraints."""
    try:
        # Read a chunk to check for binary content
        chunk = file_stream.read(4096)
        file_stream.seek(0)
        
        # Text files shouldn't have null bytes usually, unless it's some specific encoding.
        # However, Excel files (xlsx) ARE binary (zip archives).
        # We should only check for null bytes if it claims to be CSV or JSON.
        # But we don't know the extension here reliably yet if we just pass the stream.
        # So we should probably pass the filename or extension to this function.
        if b'\0' in chunk:
             return True, "Binary content detected (warning)" # Changed to warning or handle in route
        return True, ""
    except Exception as e:
        return False, f"Error checking file robustness: {str(e)}"

def load_df(filepath, ext):
    if ext == 'csv':
        return pd.read_csv(filepath)
    elif ext == 'json':
        return pd.read_json(filepath)
    elif ext == 'xlsx':
        return pd.read_excel(filepath)
    return None

def df_to_json_preview(df, rows=50):
    """Convert first N rows of DF to JSON for preview."""
    preview = df.head(rows).fillna("").to_dict(orient='records')
    columns = list(df.columns)
    stats = {
        "rows": len(df),
        "columns": len(columns),
        "missing_values": int(df.isnull().sum().sum()),
        "duplicates": int(df.duplicated().sum())
    }
    return {"data": preview, "columns": columns, "stats": stats}

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/health')
def health():
    return jsonify({"status": "healthy"}), 200

@app.route('/api/load_demo', methods=['POST'])
def load_demo():
    try:
        # Create a simple demo dataframe
        data = {
            "Date": pd.date_range(start='2024-01-01', periods=100),
            "Category": ['A', 'B', 'C', 'A', 'B'] * 20,
            "Value": pd.Series(range(100)) + pd.Series([1, 2, 5] * 33 + [1]),
            "Status": ['Active', 'Inactive', 'Pending', 'Active'] * 25
        }
        df = pd.DataFrame(data)
        # Add some random missing values
        import numpy as np
        df.loc[5:10, 'Value'] = np.nan
        df.loc[15:20, 'Status'] = np.nan
        
        filename = "demo_data.csv"
        filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
        df.to_csv(filepath, index=False)
        
        return jsonify({
            "message": "Demo data loaded successfully",
            "filename": filename,
            "preview": df_to_json_preview(df)
        })
    except Exception as e:
        logger.error(f"Demo load error: {e}")
        return jsonify({"error": str(e)}), 500

@app.route('/api/upload', methods=['POST'])
def upload_file():
    try:
        if 'file' not in request.files:
            return jsonify({"error": "No file part"}), 400
        file = request.files['file']
        if file.filename == '':
            return jsonify({"error": "No selected file"}), 400
        
        if not allowed_file(file.filename):
            return jsonify({"error": "File type not allowed. Use CSV, JSON, or XLSX."}), 400

        filename = secure_filename(file.filename)
        ext = filename.rsplit('.', 1)[1].lower()

        # Robustness check
        # Only check for null bytes if it is a text format (csv, json)
        if ext in ['csv', 'json']:
            is_safe, msg = check_robustness(file.stream)
            # If it returns True (safe) but with a message, it might be a warning, but for text files, binary content is usually bad.
            # However, my previous edit made it return True even if binary.
            # Let's fix that logic inline or revert/adjust check_robustness.
            # Actually, let's just do the check here properly.
            chunk = file.stream.read(4096)
            file.stream.seek(0)
            if b'\0' in chunk:
                 return jsonify({"error": "File contains null bytes (binary suspected). Please upload a valid text file for CSV/JSON."}), 400

        filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
        file.save(filepath)

        # Load and Preview
        try:
            df = load_df(filepath, ext)
        except Exception as e:
            return jsonify({"error": f"Failed to parse file: {str(e)}"}), 400
        
        # Store file info in session (stateless ideally, but for simplicity storing path)
        # For a more robust solution, we'd return a token. Let's return a token/filename.
        
        return jsonify({
            "message": "File uploaded successfully",
            "filename": filename,
            "preview": df_to_json_preview(df)
        })

    except Exception as e:
        logger.error(f"Upload error: {e}")
        return jsonify({"error": str(e)}), 500

@app.route('/api/process', methods=['POST'])
def process_data():
    try:
        data = request.json
        filename = data.get('filename')
        operations = data.get('operations', [])
        
        if not filename:
            return jsonify({"error": "Filename missing"}), 400
            
        filepath = os.path.join(app.config['UPLOAD_FOLDER'], secure_filename(filename))
        if not os.path.exists(filepath):
            return jsonify({"error": "File not found. Please upload again."}), 404
            
        ext = filename.rsplit('.', 1)[1].lower()
        df = load_df(filepath, ext)
        
        # Apply Operations Pipeline
        for op in operations:
            op_type = op.get('type')
            params = op.get('params', {})
            
            if op_type == 'drop_duplicates':
                subset = params.get('subset')
                if subset:
                    df = df.drop_duplicates(subset=subset)
                else:
                    df = df.drop_duplicates()
                    
            elif op_type == 'dropna':
                how = params.get('how', 'any')
                subset = params.get('subset')
                if subset:
                    df = df.dropna(how=how, subset=subset)
                else:
                    df = df.dropna(how=how)
                    
            elif op_type == 'fillna':
                value = params.get('value')
                method = params.get('method') # ffill, bfill
                subset = params.get('subset') # columns to apply
                
                if subset:
                    if method:
                        df[subset] = df[subset].fillna(method=method)
                    else:
                        df[subset] = df[subset].fillna(value)
                else:
                    if method:
                        df = df.fillna(method=method)
                    else:
                        df = df.fillna(value)
                        
            elif op_type == 'filter':
                # Simple filtering: col operator value
                col = params.get('column')
                operator = params.get('operator') # ==, !=, >, <, contains
                value = params.get('value')
                
                if col in df.columns:
                    if operator == '==':
                        df = df[df[col] == value]
                    elif operator == '!=':
                        df = df[df[col] != value]
                    elif operator == '>':
                        df = df[pd.to_numeric(df[col], errors='coerce') > float(value)]
                    elif operator == '<':
                        df = df[pd.to_numeric(df[col], errors='coerce') < float(value)]
                    elif operator == 'contains':
                        df = df[df[col].astype(str).str.contains(value, na=False)]
                        
            elif op_type == 'sort':
                col = params.get('column')
                ascending = params.get('ascending', True)
                if col in df.columns:
                    df = df.sort_values(by=col, ascending=ascending)
            
            elif op_type == 'rename':
                mapping = params.get('mapping') # {old: new}
                if mapping:
                    df = df.rename(columns=mapping)
                    
            elif op_type == 'select_columns':
                cols = params.get('columns')
                if cols:
                    valid_cols = [c for c in cols if c in df.columns]
                    df = df[valid_cols]

        return jsonify({
            "message": "Processed successfully",
            "preview": df_to_json_preview(df)
        })

    except Exception as e:
        logger.error(f"Processing error: {e}")
        return jsonify({"error": str(e)}), 500

@app.route('/api/export', methods=['POST'])
def export_data():
    try:
        data = request.json
        filename = data.get('filename')
        operations = data.get('operations', [])
        format_type = data.get('format', 'csv')
        
        filepath = os.path.join(app.config['UPLOAD_FOLDER'], secure_filename(filename))
        ext = filename.rsplit('.', 1)[1].lower()
        df = load_df(filepath, ext)
        
        # Re-apply operations (stateless)
        for op in operations:
            # ... (Duplicate logic, ideally refactor to function)
            # For simplicity, assuming same logic.
            # Let's refactor 'apply_operations'
            pass 
        
        # Actually, let's just copy-paste the logic for now to ensure it works, 
        # or better: refactor.
        df = apply_operations(df, operations)

        output = io.BytesIO()
        if format_type == 'csv':
            df.to_csv(output, index=False)
            mimetype = 'text/csv'
            download_name = 'processed_data.csv'
        elif format_type == 'json':
            df.to_json(output, orient='records')
            mimetype = 'application/json'
            download_name = 'processed_data.json'
        elif format_type == 'xlsx':
            df.to_excel(output, index=False)
            mimetype = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
            download_name = 'processed_data.xlsx'
        else:
            return jsonify({"error": "Invalid format"}), 400
            
        output.seek(0)
        return send_file(
            output,
            mimetype=mimetype,
            as_attachment=True,
            download_name=download_name
        )

    except Exception as e:
        logger.error(f"Export error: {e}")
        return jsonify({"error": str(e)}), 500

def apply_operations(df, operations):
    """Helper to apply operations to DF."""
    for op in operations:
        op_type = op.get('type')
        params = op.get('params', {})
        
        if op_type == 'drop_duplicates':
            subset = params.get('subset')
            if subset:
                df = df.drop_duplicates(subset=subset)
            else:
                df = df.drop_duplicates()
                
        elif op_type == 'dropna':
            how = params.get('how', 'any')
            subset = params.get('subset')
            if subset:
                df = df.dropna(how=how, subset=subset)
            else:
                df = df.dropna(how=how)
                
        elif op_type == 'fillna':
            value = params.get('value')
            method = params.get('method')
            subset = params.get('subset')
            
            if subset:
                # Handle list of columns
                if isinstance(subset, str):
                    subset = [subset]
                
                # Check if columns exist
                valid_subset = [c for c in subset if c in df.columns]
                
                if method:
                    df[valid_subset] = df[valid_subset].fillna(method=method)
                else:
                    df[valid_subset] = df[valid_subset].fillna(value)
            else:
                if method:
                    df = df.fillna(method=method)
                else:
                    df = df.fillna(value)
                    
        elif op_type == 'filter':
            col = params.get('column')
            operator = params.get('operator')
            value = params.get('value')
            
            if col in df.columns:
                if operator == '==':
                    df = df[df[col].astype(str) == str(value)]
                elif operator == '!=':
                    df = df[df[col].astype(str) != str(value)]
                elif operator == '>':
                    try:
                        df = df[pd.to_numeric(df[col], errors='coerce') > float(value)]
                    except: pass
                elif operator == '<':
                    try:
                        df = df[pd.to_numeric(df[col], errors='coerce') < float(value)]
                    except: pass
                elif operator == 'contains':
                    df = df[df[col].astype(str).str.contains(str(value), na=False)]
                    
        elif op_type == 'sort':
            col = params.get('column')
            ascending = params.get('ascending', True)
            if col in df.columns:
                df = df.sort_values(by=col, ascending=ascending)
        
        elif op_type == 'rename':
            mapping = params.get('mapping')
            if mapping:
                df = df.rename(columns=mapping)
                
        elif op_type == 'select_columns':
            cols = params.get('columns')
            if cols:
                valid_cols = [c for c in cols if c in df.columns]
                df = df[valid_cols]
                
    return df

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=7860, debug=False)