Spaces:
Sleeping
Sleeping
Update app/export_utils.py
Browse files- app/export_utils.py +248 -250
app/export_utils.py
CHANGED
|
@@ -1,251 +1,249 @@
|
|
| 1 |
-
"""
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
import
|
| 6 |
-
import
|
| 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 |
return template
|
|
|
|
| 1 |
+
"""Export Utilities - CSV, Excel, and REAL Power BI export"""
|
| 2 |
+
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import io
|
| 5 |
+
import json
|
| 6 |
+
from datetime import datetime
|
| 7 |
+
|
| 8 |
+
class ExportUtils:
|
| 9 |
+
def __init__(self, df):
|
| 10 |
+
self.df = df
|
| 11 |
+
|
| 12 |
+
def to_csv(self):
|
| 13 |
+
"""Export to CSV"""
|
| 14 |
+
return self.df.to_csv(index=False).encode('utf-8')
|
| 15 |
+
|
| 16 |
+
def to_excel(self):
|
| 17 |
+
"""Export to Excel with formatting"""
|
| 18 |
+
output = io.BytesIO()
|
| 19 |
+
with pd.ExcelWriter(output, engine='openpyxl') as writer:
|
| 20 |
+
# Write main data
|
| 21 |
+
self.df.to_excel(writer, sheet_name='Data', index=False)
|
| 22 |
+
|
| 23 |
+
# Add summary sheet
|
| 24 |
+
numeric_cols = self.df.select_dtypes(include=['number']).columns
|
| 25 |
+
if len(numeric_cols) > 0:
|
| 26 |
+
summary = self.df[numeric_cols].describe()
|
| 27 |
+
summary.to_excel(writer, sheet_name='Summary', index=True)
|
| 28 |
+
|
| 29 |
+
# Add column info sheet
|
| 30 |
+
col_info = pd.DataFrame({
|
| 31 |
+
'Column': self.df.columns,
|
| 32 |
+
'Type': self.df.dtypes.astype(str),
|
| 33 |
+
'Nulls': self.df.isnull().sum(),
|
| 34 |
+
'Unique': self.df.nunique()
|
| 35 |
+
})
|
| 36 |
+
col_info.to_excel(writer, sheet_name='Column Info', index=False)
|
| 37 |
+
|
| 38 |
+
output.seek(0)
|
| 39 |
+
return output.getvalue()
|
| 40 |
+
|
| 41 |
+
def to_powerbi_ready(self):
|
| 42 |
+
"""Prepare data for Power BI - Creates CSV optimized for Power BI"""
|
| 43 |
+
df_powerbi = self.df.copy()
|
| 44 |
+
|
| 45 |
+
# Clean column names (Power BI friendly)
|
| 46 |
+
df_powerbi.columns = [col.replace(' ', '_').replace('-', '_').replace('/', '_') for col in df_powerbi.columns]
|
| 47 |
+
|
| 48 |
+
# Clean datetime columns for Power BI
|
| 49 |
+
for col in df_powerbi.columns:
|
| 50 |
+
if 'datetime' in col.lower() or 'date' in col.lower() or 'time' in col.lower():
|
| 51 |
+
try:
|
| 52 |
+
df_powerbi[col] = pd.to_datetime(df_powerbi[col])
|
| 53 |
+
except:
|
| 54 |
+
pass
|
| 55 |
+
|
| 56 |
+
# Convert to CSV for Power BI import
|
| 57 |
+
return df_powerbi.to_csv(index=False).encode('utf-8')
|
| 58 |
+
|
| 59 |
+
def to_powerbi_with_metadata(self):
|
| 60 |
+
"""Export to Power BI with metadata file"""
|
| 61 |
+
# Main data CSV
|
| 62 |
+
data_csv = self.to_powerbi_ready()
|
| 63 |
+
|
| 64 |
+
# Create metadata JSON
|
| 65 |
+
numeric_cols = self.df.select_dtypes(include=['number']).columns
|
| 66 |
+
categorical_cols = self.df.select_dtypes(include=['object']).columns
|
| 67 |
+
date_cols = self.df.select_dtypes(include=['datetime64']).columns
|
| 68 |
+
|
| 69 |
+
metadata = {
|
| 70 |
+
'export_date': datetime.now().isoformat(),
|
| 71 |
+
'table_name': 'Cleaned_Data',
|
| 72 |
+
'row_count': len(self.df),
|
| 73 |
+
'column_count': len(self.df.columns),
|
| 74 |
+
'columns': list(self.df.columns),
|
| 75 |
+
'numeric_columns': list(numeric_cols),
|
| 76 |
+
'categorical_columns': list(categorical_cols),
|
| 77 |
+
'date_columns': list(date_cols),
|
| 78 |
+
'recommended_measures': {},
|
| 79 |
+
'recommended_visuals': []
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
# Add recommended measures
|
| 83 |
+
for col in numeric_cols[:10]:
|
| 84 |
+
metadata['recommended_measures'][f'Total_{col}'] = f'SUM(Cleaned_Data[{col}])'
|
| 85 |
+
metadata['recommended_measures'][f'Average_{col}'] = f'AVERAGE(Cleaned_Data[{col}])'
|
| 86 |
+
|
| 87 |
+
# Add recommended visuals
|
| 88 |
+
if len(categorical_cols) > 0 and len(numeric_cols) > 0:
|
| 89 |
+
metadata['recommended_visuals'].append({
|
| 90 |
+
'type': 'bar_chart',
|
| 91 |
+
'category': categorical_cols[0],
|
| 92 |
+
'value': numeric_cols[0],
|
| 93 |
+
'title': f'{numeric_cols[0]} by {categorical_cols[0]}'
|
| 94 |
+
})
|
| 95 |
+
|
| 96 |
+
if len(date_cols) > 0 and len(numeric_cols) > 0:
|
| 97 |
+
metadata['recommended_visuals'].append({
|
| 98 |
+
'type': 'line_chart',
|
| 99 |
+
'date': date_cols[0],
|
| 100 |
+
'value': numeric_cols[0],
|
| 101 |
+
'title': f'{numeric_cols[0]} Over Time'
|
| 102 |
+
})
|
| 103 |
+
|
| 104 |
+
metadata_json = json.dumps(metadata, indent=2).encode('utf-8')
|
| 105 |
+
|
| 106 |
+
return {
|
| 107 |
+
'data': data_csv,
|
| 108 |
+
'metadata': metadata_json,
|
| 109 |
+
'instructions': self._get_powerbi_instructions()
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
+
def _get_powerbi_instructions(self):
|
| 113 |
+
"""Get step-by-step Power BI import instructions"""
|
| 114 |
+
instructions = """
|
| 115 |
+
=== POWER BI IMPORT INSTRUCTIONS ===
|
| 116 |
+
|
| 117 |
+
METHOD 1: Direct Import (Recommended)
|
| 118 |
+
1. Open Power BI Desktop
|
| 119 |
+
2. Click "Get Data" β "Text/CSV"
|
| 120 |
+
3. Select the exported CSV file
|
| 121 |
+
4. Click "Load"
|
| 122 |
+
5. Power BI will auto-detect data types
|
| 123 |
+
|
| 124 |
+
METHOD 2: Advanced Import
|
| 125 |
+
1. Click "Get Data" β "More..."
|
| 126 |
+
2. Search for "CSV" or "Text"
|
| 127 |
+
3. Select your file
|
| 128 |
+
4. Configure:
|
| 129 |
+
- First row as headers: YES
|
| 130 |
+
- Data type detection: Based on first 200 rows
|
| 131 |
+
5. Click "Load"
|
| 132 |
+
|
| 133 |
+
=== AFTER IMPORT ===
|
| 134 |
+
|
| 135 |
+
Recommended DAX Measures to Create:
|
| 136 |
+
|
| 137 |
+
"""
|
| 138 |
+
return instructions
|
| 139 |
+
|
| 140 |
+
def to_powerbi_zip(self):
|
| 141 |
+
"""Create a zip file with all Power BI resources"""
|
| 142 |
+
import zipfile
|
| 143 |
+
|
| 144 |
+
output = io.BytesIO()
|
| 145 |
+
with zipfile.ZipFile(output, 'w', zipfile.ZIP_DEFLATED) as zipf:
|
| 146 |
+
#Add data CSV
|
| 147 |
+
data_csv = self.to_powerbi_ready()
|
| 148 |
+
zipf.writestr('data.csv', data_csv)
|
| 149 |
+
|
| 150 |
+
#Add metadata
|
| 151 |
+
powerbi_data = self.to_powerbi_with_metadata()
|
| 152 |
+
zipf.writestr('metadata.json', powerbi_data['metadata'])
|
| 153 |
+
|
| 154 |
+
#Add instructions
|
| 155 |
+
zipf.writestr('instructions.txt', powerbi_data['instructions'])
|
| 156 |
+
|
| 157 |
+
#Add sample DAX file
|
| 158 |
+
dax_content = self._generate_dax_file()
|
| 159 |
+
zipf.writestr('measures.dax', dax_content)
|
| 160 |
+
|
| 161 |
+
output.seek(0)
|
| 162 |
+
return output.getvalue()
|
| 163 |
+
|
| 164 |
+
def _generate_dax_file(self):
|
| 165 |
+
"""Generate DAX file for Power BI"""
|
| 166 |
+
numeric_cols = self.df.select_dtypes(include=['number']).columns
|
| 167 |
+
|
| 168 |
+
dax = f"""// DAX Measures for Power BI
|
| 169 |
+
// Generated on: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}
|
| 170 |
+
// Table Name: Cleaned_Data
|
| 171 |
+
|
| 172 |
+
// ============ BASIC MEASURES ============
|
| 173 |
+
|
| 174 |
+
Total Records = COUNTROWS(Cleaned_Data)
|
| 175 |
+
|
| 176 |
+
"""
|
| 177 |
+
|
| 178 |
+
for col in numeric_cols[:15]:
|
| 179 |
+
dax += f"""
|
| 180 |
+
// {col} Measures
|
| 181 |
+
Total {col} = SUM(Cleaned_Data[{col}])
|
| 182 |
+
Average {col} = AVERAGE(Cleaned_Data[{col}])
|
| 183 |
+
Min {col} = MIN(Cleaned_Data[{col}])
|
| 184 |
+
Max {col} = MAX(Cleaned_Data[{col}])
|
| 185 |
+
|
| 186 |
+
"""
|
| 187 |
+
|
| 188 |
+
dax += """
|
| 189 |
+
// ============ HOW TO USE ============
|
| 190 |
+
// 1. In Power BI, go to "Modeling" tab
|
| 191 |
+
// 2. Click "New Measure"
|
| 192 |
+
// 3. Copy-paste any measure above
|
| 193 |
+
// 4. Press Enter to save
|
| 194 |
+
|
| 195 |
+
// ============ EXAMPLE VISUALS ============
|
| 196 |
+
// - Card Visual: Total Records
|
| 197 |
+
// - Bar Chart: Category vs Total Sales
|
| 198 |
+
// - Line Chart: Date vs Average Value
|
| 199 |
+
"""
|
| 200 |
+
|
| 201 |
+
return dax
|
| 202 |
+
|
| 203 |
+
def to_json(self):
|
| 204 |
+
"""Export to JSON"""
|
| 205 |
+
return self.df.to_json(orient='records', indent=2).encode('utf-8')
|
| 206 |
+
|
| 207 |
+
def get_powerbi_template(self):
|
| 208 |
+
"""Get Power BI DAX template (legacy - kept for compatibility)"""
|
| 209 |
+
numeric_cols = self.df.select_dtypes(include=['number']).columns
|
| 210 |
+
categorical_cols = self.df.select_dtypes(include=['object']).columns
|
| 211 |
+
|
| 212 |
+
template = f"""// Power BI DAX Template for your data
|
| 213 |
+
// Generated on: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}
|
| 214 |
+
// Table name: Cleaned_Data
|
| 215 |
+
|
| 216 |
+
// ============ BASIC MEASURES ============
|
| 217 |
+
|
| 218 |
+
Total Records = COUNTROWS(Cleaned_Data)
|
| 219 |
+
|
| 220 |
+
"""
|
| 221 |
+
|
| 222 |
+
for col in numeric_cols[:10]:
|
| 223 |
+
template += f"""
|
| 224 |
+
Total {col} = SUM(Cleaned_Data[{col}])
|
| 225 |
+
Average {col} = AVERAGE(Cleaned_Data[{col}])
|
| 226 |
+
"""
|
| 227 |
+
|
| 228 |
+
template += """
|
| 229 |
+
// ============ HOW TO USE ============
|
| 230 |
+
// 1. Export your data as CSV first
|
| 231 |
+
// 2. In Power BI: Get Data β CSV β Select your file
|
| 232 |
+
// 3. Go to Modeling tab β New Measure
|
| 233 |
+
// 4. Copy and paste any measure above
|
| 234 |
+
// 5. Drag measures to visuals
|
| 235 |
+
|
| 236 |
+
// ============ RECOMMENDED VISUALS ============
|
| 237 |
+
"""
|
| 238 |
+
|
| 239 |
+
if len(categorical_cols) > 0 and len(numeric_cols) > 0:
|
| 240 |
+
template += f"""
|
| 241 |
+
- Bar Chart: {categorical_cols[0]} vs {numeric_cols[0]}
|
| 242 |
+
"""
|
| 243 |
+
|
| 244 |
+
if len(self.df.select_dtypes(include=['datetime64']).columns) > 0:
|
| 245 |
+
template += f"""
|
| 246 |
+
- Line Chart: Date vs {numeric_cols[0] if len(numeric_cols) > 0 else 'Value'}
|
| 247 |
+
"""
|
| 248 |
+
|
|
|
|
|
|
|
| 249 |
return template
|