th Claude commited on
Commit
b039044
·
1 Parent(s): 6ef027d

Fix: Use Kerykeion 4.x API for SVG chart generation

Browse files

🐛 Fixes:
- Use KerykeionChartSVG instead of ChartDrawer (4.x vs 5.x)
- Use AstrologicalSubject instead of AstrologicalSubjectFactory
- Compatible with kerykeion==4.18.1 installed in requirements.txt

✨ Features:
- Professional SVG chart with aspects
- POST /api/generate-chart-svg endpoint
- Returns SVG directly for frontend embedding

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

Files changed (1) hide show
  1. app.py +10 -26
app.py CHANGED
@@ -7,9 +7,7 @@ from fastapi import FastAPI, HTTPException
7
  from fastapi.middleware.cors import CORSMiddleware
8
  from fastapi.responses import Response
9
  from pydantic import BaseModel
10
- from kerykeion import AstrologicalSubject, AstrologicalSubjectFactory
11
- from kerykeion.chart_data_factory import ChartDataFactory
12
- from kerykeion.charts.chart_drawer import ChartDrawer
13
  from typing import Optional, Any, Dict
14
  import logging
15
  import io
@@ -125,46 +123,32 @@ async def calculate_chart(req: BirthRequest):
125
 
126
  @app.post("/api/generate-chart-svg")
127
  async def generate_chart_svg(req: BirthRequest):
128
- """Generate professional birth chart SVG with aspects"""
129
  try:
130
  logger.info(f"🎨 Generating SVG chart for: {req.name}")
131
 
132
  year, month, day = map(int, req.date.split('-'))
133
  hour, minute = map(int, req.time.split(':'))
134
 
135
- # Use new Kerykeion 5.x API
136
- subject = AstrologicalSubjectFactory.from_birth_data(
137
  name=req.name,
138
  year=year, month=month, day=day,
139
  hour=hour, minute=minute,
140
- lng=req.longitude or 0.0,
141
  lat=req.latitude or 0.0,
 
142
  tz_str=req.timezone,
143
  zodiac_type="Tropic",
144
  houses_system_identifier="P",
145
  online=False
146
  )
147
 
148
- # Create chart data
149
- chart_data = ChartDataFactory.create_natal_chart_data(subject)
150
-
151
- # Generate SVG
152
- chart_drawer = ChartDrawer(chart_data=chart_data)
153
-
154
- # Get SVG content (ChartDrawer internally generates SVG)
155
- # We need to save to a temp location then read it
156
- import tempfile
157
- from pathlib import Path
158
-
159
- with tempfile.TemporaryDirectory() as tmpdir:
160
- output_path = Path(tmpdir)
161
- chart_drawer.save_svg(output_path=output_path, filename="chart")
162
-
163
- svg_file = output_path / "chart.svg"
164
- if not svg_file.exists():
165
- raise HTTPException(status_code=500, detail="Failed to generate SVG")
166
 
167
- svg_content = svg_file.read_text()
 
168
 
169
  logger.info(f"✅ SVG generated for {req.name}")
170
 
 
7
  from fastapi.middleware.cors import CORSMiddleware
8
  from fastapi.responses import Response
9
  from pydantic import BaseModel
10
+ from kerykeion import AstrologicalSubject, KerykeionChartSVG
 
 
11
  from typing import Optional, Any, Dict
12
  import logging
13
  import io
 
123
 
124
  @app.post("/api/generate-chart-svg")
125
  async def generate_chart_svg(req: BirthRequest):
126
+ """Generate professional birth chart SVG with aspects (Kerykeion 4.x)"""
127
  try:
128
  logger.info(f"🎨 Generating SVG chart for: {req.name}")
129
 
130
  year, month, day = map(int, req.date.split('-'))
131
  hour, minute = map(int, req.time.split(':'))
132
 
133
+ # Create AstrologicalSubject (Kerykeion 4.x)
134
+ subject = AstrologicalSubject(
135
  name=req.name,
136
  year=year, month=month, day=day,
137
  hour=hour, minute=minute,
138
+ city=req.location,
139
  lat=req.latitude or 0.0,
140
+ lng=req.longitude or 0.0,
141
  tz_str=req.timezone,
142
  zodiac_type="Tropic",
143
  houses_system_identifier="P",
144
  online=False
145
  )
146
 
147
+ # Generate SVG chart (Kerykeion 4.x)
148
+ chart = KerykeionChartSVG(subject)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
149
 
150
+ # Get SVG content as string
151
+ svg_content = chart.makeSVG()
152
 
153
  logger.info(f"✅ SVG generated for {req.name}")
154