Spaces:
Runtime error
Runtime error
Update pdf_generator.py
Browse files- pdf_generator.py +31 -34
pdf_generator.py
CHANGED
|
@@ -8,13 +8,14 @@ from reportlab.lib.units import mm
|
|
| 8 |
from reportlab.graphics.shapes import Drawing, Rect, String, Line
|
| 9 |
|
| 10 |
class SliderFlowable(Flowable):
|
| 11 |
-
def __init__(self, name, value, min_val, max_val, is_percentage=False):
|
| 12 |
Flowable.__init__(self)
|
| 13 |
self.name = name
|
| 14 |
self.value = value
|
| 15 |
self.min_val = min_val
|
| 16 |
self.max_val = max_val
|
| 17 |
self.is_percentage = is_percentage
|
|
|
|
| 18 |
self.width = 400
|
| 19 |
self.height = 80
|
| 20 |
|
|
@@ -27,7 +28,7 @@ class SliderFlowable(Flowable):
|
|
| 27 |
|
| 28 |
# Draw slider value
|
| 29 |
if self.max_val == self.min_val:
|
| 30 |
-
value_width = 50 #
|
| 31 |
else:
|
| 32 |
value_width = 50 + ((self.value - self.min_val) / (self.max_val - self.min_val) * 300)
|
| 33 |
value_bar = Rect(50, 30, value_width - 50, 20, fillColor=colors.HexColor("#9999ff"), strokeColor=None)
|
|
@@ -54,8 +55,8 @@ class SliderFlowable(Flowable):
|
|
| 54 |
def format_value(self, value):
|
| 55 |
if self.is_percentage:
|
| 56 |
return f"{value:.1f}%"
|
| 57 |
-
elif
|
| 58 |
-
return f"{value}"
|
| 59 |
else:
|
| 60 |
return f"{value:.2f}"
|
| 61 |
|
|
@@ -140,49 +141,45 @@ def generate_pdf(session_state):
|
|
| 140 |
story.append(Spacer(1, 12))
|
| 141 |
|
| 142 |
# Add quantitative criteria visualization if applicable
|
| 143 |
-
if page['input_key']
|
| 144 |
-
story.extend(process_quantitative_criteria(
|
| 145 |
|
| 146 |
doc.build(story, onFirstPage=create_page_template, onLaterPages=create_page_template)
|
| 147 |
buffer.seek(0)
|
| 148 |
return buffer
|
| 149 |
|
| 150 |
-
def process_quantitative_criteria(
|
| 151 |
story = []
|
| 152 |
-
|
| 153 |
-
for
|
| 154 |
-
parsed = parse_quantitative_criteria(
|
| 155 |
if parsed:
|
| 156 |
-
name,
|
| 157 |
-
|
|
|
|
| 158 |
story.append(slider)
|
| 159 |
return story
|
| 160 |
|
| 161 |
def parse_quantitative_criteria(input_string):
|
| 162 |
-
# Match
|
| 163 |
-
match = re.match(r'(.+?)
|
| 164 |
-
|
| 165 |
if match:
|
| 166 |
-
name,
|
| 167 |
name = name.strip()
|
| 168 |
|
| 169 |
-
is_percentage = '%' in min_val or '%' in max_val
|
| 170 |
-
|
| 171 |
def parse_value(val):
|
| 172 |
-
if
|
| 173 |
-
return
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
return name, value, min_val, max_val, is_percentage
|
| 188 |
return None
|
|
|
|
| 8 |
from reportlab.graphics.shapes import Drawing, Rect, String, Line
|
| 9 |
|
| 10 |
class SliderFlowable(Flowable):
|
| 11 |
+
def __init__(self, name, value, min_val, max_val, is_percentage=False, is_integer=False):
|
| 12 |
Flowable.__init__(self)
|
| 13 |
self.name = name
|
| 14 |
self.value = value
|
| 15 |
self.min_val = min_val
|
| 16 |
self.max_val = max_val
|
| 17 |
self.is_percentage = is_percentage
|
| 18 |
+
self.is_integer = is_integer
|
| 19 |
self.width = 400
|
| 20 |
self.height = 80
|
| 21 |
|
|
|
|
| 28 |
|
| 29 |
# Draw slider value
|
| 30 |
if self.max_val == self.min_val:
|
| 31 |
+
value_width = 50 # or some default width
|
| 32 |
else:
|
| 33 |
value_width = 50 + ((self.value - self.min_val) / (self.max_val - self.min_val) * 300)
|
| 34 |
value_bar = Rect(50, 30, value_width - 50, 20, fillColor=colors.HexColor("#9999ff"), strokeColor=None)
|
|
|
|
| 55 |
def format_value(self, value):
|
| 56 |
if self.is_percentage:
|
| 57 |
return f"{value:.1f}%"
|
| 58 |
+
elif self.is_integer:
|
| 59 |
+
return f"{int(value)}"
|
| 60 |
else:
|
| 61 |
return f"{value:.2f}"
|
| 62 |
|
|
|
|
| 141 |
story.append(Spacer(1, 12))
|
| 142 |
|
| 143 |
# Add quantitative criteria visualization if applicable
|
| 144 |
+
if page['input_key'] == 'useful_assets':
|
| 145 |
+
story.extend(process_quantitative_criteria(answers['useful_assets'], styles))
|
| 146 |
|
| 147 |
doc.build(story, onFirstPage=create_page_template, onLaterPages=create_page_template)
|
| 148 |
buffer.seek(0)
|
| 149 |
return buffer
|
| 150 |
|
| 151 |
+
def process_quantitative_criteria(answers, styles):
|
| 152 |
story = []
|
| 153 |
+
quantitative_criteria = answers.get('quantitative', [])
|
| 154 |
+
for i, criterion in enumerate(quantitative_criteria):
|
| 155 |
+
parsed = parse_quantitative_criteria(criterion)
|
| 156 |
if parsed:
|
| 157 |
+
name, min_val, max_val, is_percentage, is_integer = parsed
|
| 158 |
+
value = answers.get(f'quant_value_{i}', min_val)
|
| 159 |
+
slider = SliderFlowable(name, value, min_val, max_val, is_percentage=is_percentage, is_integer=is_integer)
|
| 160 |
story.append(slider)
|
| 161 |
return story
|
| 162 |
|
| 163 |
def parse_quantitative_criteria(input_string):
|
| 164 |
+
# Match "Text [min-max]" format, where min and max can be int, float, or percentage
|
| 165 |
+
match = re.match(r'(.+?)\s*\[([-+]?(?:\d*\.*\d+)(?:%)?)\s*-\s*([-+]?(?:\d*\.*\d+)(?:%)?)\]', input_string)
|
|
|
|
| 166 |
if match:
|
| 167 |
+
name, min_val, max_val = match.groups()
|
| 168 |
name = name.strip()
|
| 169 |
|
|
|
|
|
|
|
| 170 |
def parse_value(val):
|
| 171 |
+
if '%' in val:
|
| 172 |
+
return float(val.rstrip('%')) / 100, True
|
| 173 |
+
elif '.' in val:
|
| 174 |
+
return float(val), False
|
| 175 |
+
else:
|
| 176 |
+
return int(val), False
|
| 177 |
+
|
| 178 |
+
min_val, is_min_percent = parse_value(min_val)
|
| 179 |
+
max_val, is_max_percent = parse_value(max_val)
|
| 180 |
+
|
| 181 |
+
is_percentage = is_min_percent or is_max_percent
|
| 182 |
+
is_integer = isinstance(min_val, int) and isinstance(max_val, int)
|
| 183 |
+
|
| 184 |
+
return name, min_val, max_val, is_percentage, is_integer
|
|
|
|
|
|
|
| 185 |
return None
|