entropy25 commited on
Commit
13fafc1
·
verified ·
1 Parent(s): 6582591

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -78
app.py CHANGED
@@ -324,9 +324,6 @@ def query_ai(model, stats, question, df=None):
324
  def save_plotly_as_image(fig, filename):
325
  """Convert Plotly figure to PNG for PDF"""
326
  try:
327
- # Check if kaleido is available
328
- import kaleido
329
-
330
  temp_dir = tempfile.gettempdir()
331
  filepath = os.path.join(temp_dir, filename)
332
 
@@ -338,44 +335,66 @@ def save_plotly_as_image(fig, filename):
338
  margin=dict(t=50, b=40, l=40, r=40)
339
  )
340
 
341
- pio.write_image(fig, filepath, format='png', width=800, height=400, scale=2)
342
- return filepath if os.path.exists(filepath) else None
343
 
344
- except ImportError:
345
- st.warning("📸 Kaleido not installed. Charts will not appear in PDF. Run: pip install kaleido")
346
- return None
 
 
 
 
 
 
 
 
 
347
  except Exception as e:
348
- st.warning(f"Chart save error: {e}")
 
349
  return None
350
 
351
  def create_pdf_charts(df, stats):
352
  """Generate charts for PDF report"""
353
  charts = {}
354
 
355
- # Production distribution pie chart
356
- materials = [k for k in stats.keys() if k != '_total_']
357
- values = [stats[mat]['total'] for mat in materials]
358
- labels = [mat.replace('_', ' ').title() for mat in materials]
359
-
360
- fig_pie = px.pie(values=values, names=labels, title="Production Distribution by Material")
361
- charts['pie'] = save_plotly_as_image(fig_pie, "distribution.png")
362
-
363
- # Daily production trend
364
- daily_data = df.groupby('date')['weight_kg'].sum().reset_index()
365
- fig_trend = px.line(daily_data, x='date', y='weight_kg', title="Daily Production Trend")
366
- fig_trend.update_layout(xaxis_title="Date", yaxis_title="Weight (kg)")
367
- charts['trend'] = save_plotly_as_image(fig_trend, "trend.png")
368
-
369
- # Material comparison bar chart
370
- fig_bar = px.bar(x=labels, y=values, title="Production by Material Type")
371
- fig_bar.update_layout(xaxis_title="Material Type", yaxis_title="Weight (kg)")
372
- charts['bar'] = save_plotly_as_image(fig_bar, "materials.png")
373
-
374
- # Shift analysis if available
375
- if 'shift' in df.columns:
376
- shift_data = df.groupby('shift')['weight_kg'].sum().reset_index()
377
- fig_shift = px.pie(shift_data, values='weight_kg', names='shift', title="Production by Shift")
378
- charts['shift'] = save_plotly_as_image(fig_shift, "shifts.png")
 
 
 
 
 
 
 
 
 
 
 
 
379
 
380
  return charts
381
 
@@ -477,53 +496,40 @@ def create_enhanced_pdf_report(df, stats, outliers):
477
  # Generate and add charts
478
  elements.append(Paragraph("Production Analysis Charts", subtitle_style))
479
 
480
- try:
481
- # Check if kaleido is available
482
- import kaleido
483
-
484
- with st.spinner("Generating charts for PDF..."):
485
- charts = create_pdf_charts(df, stats)
486
-
487
- # Add charts to PDF
488
- charts_added = False
489
-
490
- if charts.get('pie') and charts['pie'] and os.path.exists(charts['pie']):
491
- elements.append(Paragraph("Production Distribution", styles['Heading3']))
492
- elements.append(Image(charts['pie'], width=6*inch, height=3*inch))
493
- elements.append(Spacer(1, 20))
494
- charts_added = True
495
-
496
- if charts.get('trend') and charts['trend'] and os.path.exists(charts['trend']):
497
- elements.append(Paragraph("Production Trend", styles['Heading3']))
498
- elements.append(Image(charts['trend'], width=6*inch, height=3*inch))
499
- elements.append(Spacer(1, 20))
500
- charts_added = True
501
-
502
- if charts.get('bar') and charts['bar'] and os.path.exists(charts['bar']):
503
- elements.append(Paragraph("Material Comparison", styles['Heading3']))
504
- elements.append(Image(charts['bar'], width=6*inch, height=3*inch))
505
- elements.append(Spacer(1, 20))
506
- charts_added = True
507
-
508
- if charts.get('shift') and charts['shift'] and os.path.exists(charts['shift']):
509
- elements.append(Paragraph("Shift Analysis", styles['Heading3']))
510
- elements.append(Image(charts['shift'], width=6*inch, height=3*inch))
511
- elements.append(Spacer(1, 20))
512
- charts_added = True
513
-
514
- if not charts_added:
515
- elements.append(Paragraph("Charts could not be generated. Please install kaleido: pip install kaleido", styles['Normal']))
516
- elements.append(Spacer(1, 20))
517
-
518
- except ImportError:
519
- # Kaleido not available - add text summary instead
520
- elements.append(Paragraph("Charts Not Available", styles['Heading3']))
521
- elements.append(Paragraph("To enable charts in PDF reports, please install kaleido:", styles['Normal']))
522
- elements.append(Paragraph("<b>pip install kaleido</b>", styles['Normal']))
523
  elements.append(Spacer(1, 10))
524
 
525
- # Add text-based data summary instead
526
- elements.append(Paragraph("Production Summary (Text Format):", styles['Normal']))
527
  for material, info in stats.items():
528
  if material != '_total_':
529
  summary_text = f"• {material.replace('_', ' ').title()}: {info['total']:,.0f} kg ({info['percentage']:.1f}%)"
 
324
  def save_plotly_as_image(fig, filename):
325
  """Convert Plotly figure to PNG for PDF"""
326
  try:
 
 
 
327
  temp_dir = tempfile.gettempdir()
328
  filepath = os.path.join(temp_dir, filename)
329
 
 
335
  margin=dict(t=50, b=40, l=40, r=40)
336
  )
337
 
338
+ # Debug info
339
+ print(f"Attempting to save chart to: {filepath}")
340
 
341
+ # Use kaleido explicitly
342
+ pio.write_image(fig, filepath, format='png', width=800, height=400, scale=2, engine='kaleido')
343
+
344
+ # Check if file was created
345
+ if os.path.exists(filepath):
346
+ file_size = os.path.getsize(filepath)
347
+ print(f"Chart saved successfully: {filepath} ({file_size} bytes)")
348
+ return filepath
349
+ else:
350
+ print("Chart file was not created")
351
+ return None
352
+
353
  except Exception as e:
354
+ print(f"Chart save error: {str(e)}")
355
+ st.error(f"Chart save error: {str(e)}")
356
  return None
357
 
358
  def create_pdf_charts(df, stats):
359
  """Generate charts for PDF report"""
360
  charts = {}
361
 
362
+ try:
363
+ # Production distribution pie chart
364
+ materials = [k for k in stats.keys() if k != '_total_']
365
+ values = [stats[mat]['total'] for mat in materials]
366
+ labels = [mat.replace('_', ' ').title() for mat in materials]
367
+
368
+ if len(materials) > 0 and len(values) > 0:
369
+ fig_pie = px.pie(values=values, names=labels, title="Production Distribution by Material")
370
+ charts['pie'] = save_plotly_as_image(fig_pie, "distribution.png")
371
+
372
+ # Daily production trend
373
+ if len(df) > 0:
374
+ daily_data = df.groupby('date')['weight_kg'].sum().reset_index()
375
+ if len(daily_data) > 0:
376
+ fig_trend = px.line(daily_data, x='date', y='weight_kg', title="Daily Production Trend")
377
+ fig_trend.update_layout(xaxis_title="Date", yaxis_title="Weight (kg)")
378
+ charts['trend'] = save_plotly_as_image(fig_trend, "trend.png")
379
+
380
+ # Material comparison bar chart
381
+ if len(materials) > 0 and len(values) > 0:
382
+ fig_bar = px.bar(x=labels, y=values, title="Production by Material Type")
383
+ fig_bar.update_layout(xaxis_title="Material Type", yaxis_title="Weight (kg)")
384
+ charts['bar'] = save_plotly_as_image(fig_bar, "materials.png")
385
+
386
+ # Shift analysis if available
387
+ if 'shift' in df.columns and len(df) > 0:
388
+ shift_data = df.groupby('shift')['weight_kg'].sum().reset_index()
389
+ if len(shift_data) > 0 and shift_data['weight_kg'].sum() > 0:
390
+ fig_shift = px.pie(shift_data, values='weight_kg', names='shift', title="Production by Shift")
391
+ charts['shift'] = save_plotly_as_image(fig_shift, "shifts.png")
392
+
393
+ print(f"Charts created: {list(charts.keys())}")
394
+
395
+ except Exception as e:
396
+ print(f"Error creating charts: {str(e)}")
397
+ st.error(f"Error creating charts: {str(e)}")
398
 
399
  return charts
400
 
 
496
  # Generate and add charts
497
  elements.append(Paragraph("Production Analysis Charts", subtitle_style))
498
 
499
+ with st.spinner("Generating charts for PDF..."):
500
+ charts = create_pdf_charts(df, stats)
501
+
502
+ # Add charts to PDF with better error checking
503
+ charts_added = False
504
+
505
+ for chart_type, chart_titles in [
506
+ ('pie', "Production Distribution"),
507
+ ('trend', "Production Trend"),
508
+ ('bar', "Material Comparison"),
509
+ ('shift', "Shift Analysis")
510
+ ]:
511
+ chart_path = charts.get(chart_type)
512
+ if chart_path and os.path.exists(chart_path):
513
+ try:
514
+ elements.append(Paragraph(chart_titles, styles['Heading3']))
515
+ elements.append(Image(chart_path, width=6*inch, height=3*inch))
516
+ elements.append(Spacer(1, 20))
517
+ charts_added = True
518
+ print(f"Added chart: {chart_type}")
519
+ except Exception as e:
520
+ print(f"Error adding chart {chart_type}: {str(e)}")
521
+ st.warning(f"Could not add {chart_type} chart to PDF: {str(e)}")
522
+
523
+ if not charts_added:
524
+ elements.append(Paragraph("Charts Generation Failed", styles['Heading3']))
525
+ elements.append(Paragraph("Possible issues:", styles['Normal']))
526
+ elements.append(Paragraph("1. Kaleido installation problem", styles['Normal']))
527
+ elements.append(Paragraph("2. File permissions issue", styles['Normal']))
528
+ elements.append(Paragraph("3. Data format problem", styles['Normal']))
 
 
 
 
 
 
 
 
 
 
 
 
 
529
  elements.append(Spacer(1, 10))
530
 
531
+ # Add text-based summary instead
532
+ elements.append(Paragraph("Production Data Summary:", styles['Normal']))
533
  for material, info in stats.items():
534
  if material != '_total_':
535
  summary_text = f"• {material.replace('_', ' ').title()}: {info['total']:,.0f} kg ({info['percentage']:.1f}%)"