-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathallocation_table.py
More file actions
50 lines (40 loc) · 1.71 KB
/
allocation_table.py
File metadata and controls
50 lines (40 loc) · 1.71 KB
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
def create_allocation_table(allocation_df, title):
"""Create a compact table for the allocation data"""
from reportlab.lib import colors
from reportlab.platypus import Table, TableStyle
from reportlab.lib.units import inch
import pandas as pd
# Create header row
header = ["Strategy", "Allocation"]
# Create data rows
data = [header]
# Add strategy rows
for _, row in allocation_df.iterrows():
strategy = row['Strategy']
allocation = row['Allocation']
# Format the allocation as a percentage
allocation_str = f"{allocation:.1f}%" if pd.notnull(allocation) else "N/A"
data.append([strategy, allocation_str])
# Create the table
table = Table(data, colWidths=[1.5*inch, 1.3*inch])
# Apply styles
table.setStyle(TableStyle([
# Header row
('BACKGROUND', (0, 0), (-1, 0), colors.lightgrey),
('TEXTCOLOR', (0, 0), (-1, 0), colors.black),
('ALIGN', (0, 0), (-1, 0), 'CENTER'),
('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
('FONTSIZE', (0, 0), (-1, 0), 7), # Smaller font
('BOTTOMPADDING', (0, 0), (-1, 0), 2), # Less padding
('TOPPADDING', (0, 0), (-1, 0), 2), # Less padding
# Data rows
('FONTNAME', (0, 1), (-1, -1), 'Helvetica'),
('FONTSIZE', (0, 1), (-1, -1), 7), # Smaller font
('ALIGN', (0, 1), (0, -1), 'LEFT'),
('ALIGN', (1, 1), (1, -1), 'RIGHT'),
('BOTTOMPADDING', (0, 1), (-1, -1), 1), # Minimal padding
('TOPPADDING', (0, 1), (-1, -1), 1), # Minimal padding
# Grid
('GRID', (0, 0), (-1, -1), 0.25, colors.grey),
]))
return table