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json
|
| 3 |
from tempfile import NamedTemporaryFile
|
|
@@ -53,16 +207,15 @@ def generate_timeline_diagram(json_input: str, output_format: str) -> str:
|
|
| 53 |
name='Timeline',
|
| 54 |
format='png',
|
| 55 |
graph_attr={
|
| 56 |
-
'rankdir': 'TB',
|
| 57 |
-
'splines': 'ortho',
|
| 58 |
-
'bgcolor': 'white',
|
| 59 |
-
'pad': '0.8',
|
| 60 |
-
'nodesep': '3.0',
|
| 61 |
-
'ranksep': '2.5'
|
| 62 |
}
|
| 63 |
)
|
| 64 |
|
| 65 |
-
# base_color = '#19191a'
|
| 66 |
base_color = '#BEBEBE'
|
| 67 |
|
| 68 |
title = data.get('title', '')
|
|
@@ -79,7 +232,7 @@ def generate_timeline_diagram(json_input: str, output_format: str) -> str:
|
|
| 79 |
shape='plaintext',
|
| 80 |
fontsize='18',
|
| 81 |
fontweight='bold',
|
| 82 |
-
fontcolor=
|
| 83 |
pos="6,2!"
|
| 84 |
)
|
| 85 |
|
|
@@ -100,14 +253,21 @@ def generate_timeline_diagram(json_input: str, output_format: str) -> str:
|
|
| 100 |
else:
|
| 101 |
full_label = event_label
|
| 102 |
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
-
node_color = f
|
| 110 |
-
font_color = '
|
| 111 |
|
| 112 |
row = i // events_per_row
|
| 113 |
col = i % events_per_row
|
|
@@ -137,7 +297,7 @@ def generate_timeline_diagram(json_input: str, output_format: str) -> str:
|
|
| 137 |
dot.edge(
|
| 138 |
current_event_id,
|
| 139 |
next_event_id,
|
| 140 |
-
color='#
|
| 141 |
arrowsize='0.8',
|
| 142 |
penwidth='2'
|
| 143 |
)
|
|
|
|
| 1 |
+
# import graphviz
|
| 2 |
+
# import json
|
| 3 |
+
# from tempfile import NamedTemporaryFile
|
| 4 |
+
# import os
|
| 5 |
+
|
| 6 |
+
# def generate_timeline_diagram(json_input: str, output_format: str) -> str:
|
| 7 |
+
# """
|
| 8 |
+
# Generates a serpentine timeline diagram from JSON input.
|
| 9 |
+
|
| 10 |
+
# Args:
|
| 11 |
+
# json_input (str): A JSON string describing the timeline structure.
|
| 12 |
+
# It must follow the Expected JSON Format Example below.
|
| 13 |
+
|
| 14 |
+
# Expected JSON Format Example:
|
| 15 |
+
# {
|
| 16 |
+
# "title": "AI Development Timeline",
|
| 17 |
+
# "events_per_row": 4,
|
| 18 |
+
# "events": [
|
| 19 |
+
# {
|
| 20 |
+
# "id": "event_1",
|
| 21 |
+
# "label": "Machine Learning Foundations",
|
| 22 |
+
# "date": "1950-1960",
|
| 23 |
+
# "description": "Early neural networks and perceptrons"
|
| 24 |
+
# },
|
| 25 |
+
# {
|
| 26 |
+
# "id": "event_2",
|
| 27 |
+
# "label": "Expert Systems Era",
|
| 28 |
+
# "date": "1970-1980",
|
| 29 |
+
# "description": "Rule-based AI systems"
|
| 30 |
+
# },
|
| 31 |
+
# {
|
| 32 |
+
# "id": "event_3",
|
| 33 |
+
# "label": "Neural Network Revival",
|
| 34 |
+
# "date": "1980-1990",
|
| 35 |
+
# "description": "Backpropagation algorithm"
|
| 36 |
+
# }
|
| 37 |
+
# ]
|
| 38 |
+
# }
|
| 39 |
+
|
| 40 |
+
# Returns:
|
| 41 |
+
# str: The filepath to the generated PNG image file.
|
| 42 |
+
# """
|
| 43 |
+
# try:
|
| 44 |
+
# if not json_input.strip():
|
| 45 |
+
# return "Error: Empty input"
|
| 46 |
+
|
| 47 |
+
# data = json.loads(json_input)
|
| 48 |
+
|
| 49 |
+
# if 'events' not in data:
|
| 50 |
+
# raise ValueError("Missing required field: events")
|
| 51 |
+
|
| 52 |
+
# dot = graphviz.Digraph(
|
| 53 |
+
# name='Timeline',
|
| 54 |
+
# format='png',
|
| 55 |
+
# graph_attr={
|
| 56 |
+
# 'rankdir': 'TB', # Top-to-Bottom
|
| 57 |
+
# 'splines': 'ortho', # Straight lines with 90-degree bends
|
| 58 |
+
# 'bgcolor': 'white', # White background
|
| 59 |
+
# 'pad': '0.8', # Padding around the graph
|
| 60 |
+
# 'nodesep': '3.0', # Increased spacing between nodes horizontally
|
| 61 |
+
# 'ranksep': '2.5' # Increased spacing between ranks vertically
|
| 62 |
+
# }
|
| 63 |
+
# )
|
| 64 |
+
|
| 65 |
+
# # base_color = '#19191a'
|
| 66 |
+
# base_color = '#BEBEBE'
|
| 67 |
+
|
| 68 |
+
# title = data.get('title', '')
|
| 69 |
+
# events = data.get('events', [])
|
| 70 |
+
# events_per_row = data.get('events_per_row', 4)
|
| 71 |
+
|
| 72 |
+
# if not events:
|
| 73 |
+
# raise ValueError("Timeline must contain at least one event")
|
| 74 |
+
|
| 75 |
+
# if title:
|
| 76 |
+
# dot.node(
|
| 77 |
+
# 'title',
|
| 78 |
+
# title,
|
| 79 |
+
# shape='plaintext',
|
| 80 |
+
# fontsize='18',
|
| 81 |
+
# fontweight='bold',
|
| 82 |
+
# fontcolor=base_color,
|
| 83 |
+
# pos="6,2!"
|
| 84 |
+
# )
|
| 85 |
+
|
| 86 |
+
# total_events = len(events)
|
| 87 |
+
|
| 88 |
+
# for i, event in enumerate(events):
|
| 89 |
+
# event_id = event.get('id', f'event_{i}')
|
| 90 |
+
# event_label = event.get('label', f'Event {i+1}')
|
| 91 |
+
# event_date = event.get('date', '')
|
| 92 |
+
# event_description = event.get('description', '')
|
| 93 |
+
|
| 94 |
+
# if event_date and event_description:
|
| 95 |
+
# full_label = f"{event_date}\\n{event_label}\\n{event_description}"
|
| 96 |
+
# elif event_date:
|
| 97 |
+
# full_label = f"{event_date}\\n{event_label}"
|
| 98 |
+
# elif event_description:
|
| 99 |
+
# full_label = f"{event_label}\\n{event_description}"
|
| 100 |
+
# else:
|
| 101 |
+
# full_label = event_label
|
| 102 |
+
|
| 103 |
+
# if total_events == 1:
|
| 104 |
+
# opacity = 'FF'
|
| 105 |
+
# else:
|
| 106 |
+
# opacity_value = int(255 * (1.0 - (i * 0.7 / (total_events - 1))))
|
| 107 |
+
# opacity = format(opacity_value, '02x')
|
| 108 |
+
|
| 109 |
+
# node_color = f"{base_color}{opacity}"
|
| 110 |
+
# font_color = 'white' if i < total_events * 0.7 else 'black'
|
| 111 |
+
|
| 112 |
+
# row = i // events_per_row
|
| 113 |
+
# col = i % events_per_row
|
| 114 |
+
|
| 115 |
+
# if row % 2 == 1:
|
| 116 |
+
# visual_col = events_per_row - 1 - col
|
| 117 |
+
# else:
|
| 118 |
+
# visual_col = col
|
| 119 |
+
|
| 120 |
+
# dot.node(
|
| 121 |
+
# event_id,
|
| 122 |
+
# full_label,
|
| 123 |
+
# shape='box',
|
| 124 |
+
# style='filled,rounded',
|
| 125 |
+
# fillcolor=node_color,
|
| 126 |
+
# fontcolor=font_color,
|
| 127 |
+
# fontsize='12',
|
| 128 |
+
# width='2.5',
|
| 129 |
+
# height='1.2',
|
| 130 |
+
# pos=f"{visual_col * 4.5},{-row * 3}!"
|
| 131 |
+
# )
|
| 132 |
+
|
| 133 |
+
# for i in range(len(events) - 1):
|
| 134 |
+
# current_event_id = events[i].get('id', f'event_{i}')
|
| 135 |
+
# next_event_id = events[i + 1].get('id', f'event_{i + 1}')
|
| 136 |
+
|
| 137 |
+
# dot.edge(
|
| 138 |
+
# current_event_id,
|
| 139 |
+
# next_event_id,
|
| 140 |
+
# color='#666666',
|
| 141 |
+
# arrowsize='0.8',
|
| 142 |
+
# penwidth='2'
|
| 143 |
+
# )
|
| 144 |
+
|
| 145 |
+
# dot.engine = 'neato'
|
| 146 |
+
|
| 147 |
+
# with NamedTemporaryFile(delete=False, suffix=f'.{output_format}') as tmp:
|
| 148 |
+
# dot.render(tmp.name, format=output_format, cleanup=True)
|
| 149 |
+
# return f"{tmp.name}.{output_format}"
|
| 150 |
+
|
| 151 |
+
# except json.JSONDecodeError:
|
| 152 |
+
# return "Error: Invalid JSON format"
|
| 153 |
+
# except Exception as e:
|
| 154 |
+
# return f"Error: {str(e)}"
|
| 155 |
import graphviz
|
| 156 |
import json
|
| 157 |
from tempfile import NamedTemporaryFile
|
|
|
|
| 207 |
name='Timeline',
|
| 208 |
format='png',
|
| 209 |
graph_attr={
|
| 210 |
+
'rankdir': 'TB',
|
| 211 |
+
'splines': 'ortho',
|
| 212 |
+
'bgcolor': 'white',
|
| 213 |
+
'pad': '0.8',
|
| 214 |
+
'nodesep': '3.0',
|
| 215 |
+
'ranksep': '2.5'
|
| 216 |
}
|
| 217 |
)
|
| 218 |
|
|
|
|
| 219 |
base_color = '#BEBEBE'
|
| 220 |
|
| 221 |
title = data.get('title', '')
|
|
|
|
| 232 |
shape='plaintext',
|
| 233 |
fontsize='18',
|
| 234 |
fontweight='bold',
|
| 235 |
+
fontcolor='#404040',
|
| 236 |
pos="6,2!"
|
| 237 |
)
|
| 238 |
|
|
|
|
| 253 |
else:
|
| 254 |
full_label = event_label
|
| 255 |
|
| 256 |
+
lightening_factor = 0.08
|
| 257 |
+
base_r = int(base_color[1:3], 16)
|
| 258 |
+
base_g = int(base_color[3:5], 16)
|
| 259 |
+
base_b = int(base_color[5:7], 16)
|
| 260 |
+
|
| 261 |
+
current_r = base_r + int((255 - base_r) * i * lightening_factor)
|
| 262 |
+
current_g = base_g + int((255 - base_g) * i * lightening_factor)
|
| 263 |
+
current_b = base_b + int((255 - base_b) * i * lightening_factor)
|
| 264 |
+
|
| 265 |
+
current_r = min(255, current_r)
|
| 266 |
+
current_g = min(255, current_g)
|
| 267 |
+
current_b = min(255, current_b)
|
| 268 |
|
| 269 |
+
node_color = f'#{current_r:02x}{current_g:02x}{current_b:02x}'
|
| 270 |
+
font_color = 'black'
|
| 271 |
|
| 272 |
row = i // events_per_row
|
| 273 |
col = i % events_per_row
|
|
|
|
| 297 |
dot.edge(
|
| 298 |
current_event_id,
|
| 299 |
next_event_id,
|
| 300 |
+
color='#4a4a4a',
|
| 301 |
arrowsize='0.8',
|
| 302 |
penwidth='2'
|
| 303 |
)
|