File size: 1,333 Bytes
a4af9c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
51
52
from flask import Flask, render_template, request, jsonify
import altair as alt
import numpy as np
import pandas as pd
import json

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/health')
def health():
    return {'status': 'healthy'}

@app.route('/generate_chart', methods=['POST'])
def generate_chart():
    data = request.get_json()
    num_points = data.get('num_points', 1100)
    num_turns = data.get('num_turns', 31)
    
    # Generate spiral data
    indices = np.linspace(0, 1, num_points)
    theta = 2 * np.pi * num_turns * indices
    radius = indices

    x = radius * np.cos(theta)
    y = radius * np.sin(theta)

    df = pd.DataFrame({
        "x": x,
        "y": y,
        "idx": indices,
        "rand": np.random.randn(num_points),
    })

    # Create Altair chart
    chart = alt.Chart(df, height=700, width=700).mark_point(filled=True).encode(
        x=alt.X("x", axis=None),
        y=alt.Y("y", axis=None),
        color=alt.Color("idx", legend=None, scale=alt.Scale()),
        size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
    )
    
    # Convert chart to JSON
    chart_json = chart.to_json()
    
    return jsonify({'chart': chart_json})

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8501, debug=True)