|
1 |
| -# from flask import Flask, render_template |
2 |
| -import plotly.graph_objs as go |
3 |
| -import plotly.utils |
| 1 | +import asyncio |
| 2 | +import time |
4 | 3 | import json
|
| 4 | +import numpy as np |
| 5 | +from pyscript import display |
5 | 6 | import js # type: ignore
|
6 |
| -from pyscript import window, document |
7 |
| -# from pyscript.js_modules import plotly_js |
8 |
| -import asyncio |
| 7 | +from plotly.graph_objects import Figure |
| 8 | +from plotly.io import to_json |
9 | 9 |
|
10 |
| -# app = Flask(__name__) |
11 |
| -window.console.log("1-----") |
12 |
| -print("hi!") |
13 | 10 |
|
14 |
| -# Data for US presidential party control (simplified example) |
15 |
| -data = { |
16 |
| - 1960: "Democratic", 1961: "Democratic", 1962: "Democratic", 1963: "Democratic", |
17 |
| - 1964: "Democratic", 1965: "Democratic", 1966: "Democratic", 1967: "Democratic", |
18 |
| - 1968: "Democratic", 1969: "Republican", 1970: "Republican", 1971: "Republican", |
19 |
| - 1972: "Republican", 1973: "Republican", 1974: "Republican", 1975: "Republican", |
20 |
| - 1976: "Republican", 1977: "Democratic", 1978: "Democratic", 1979: "Democratic", |
21 |
| - 1980: "Democratic", 1981: "Republican", 1982: "Republican", 1983: "Republican", |
22 |
| - 1984: "Republican", 1985: "Republican", 1986: "Republican", 1987: "Republican", |
23 |
| - 1988: "Republican", 1989: "Republican", 1990: "Republican", 1991: "Republican", |
24 |
| - 1992: "Republican", 1993: "Democratic", 1994: "Democratic", 1995: "Democratic", |
25 |
| - 1996: "Democratic", 1997: "Democratic", 1998: "Democratic", 1999: "Democratic", |
26 |
| - 2000: "Democratic", 2001: "Republican", 2002: "Republican", 2003: "Republican", |
27 |
| - 2004: "Republican", 2005: "Republican", 2006: "Republican", 2007: "Republican", |
28 |
| - 2008: "Republican", 2009: "Democratic", 2010: "Democratic", 2011: "Democratic", |
29 |
| - 2012: "Democratic", 2013: "Democratic", 2014: "Democratic", 2015: "Democratic", |
30 |
| - 2016: "Democratic", 2017: "Republican", 2018: "Republican", 2019: "Republican", |
31 |
| - 2020: "Republican", 2021: "Democratic", 2022: "Democratic", 2023: "Democratic", |
32 |
| - 2024: "Democratic" |
33 |
| -} |
| 11 | +async def js_run_py_benchmark(event): |
| 12 | + try: |
| 13 | + # Limpieza y preparación |
| 14 | + js.clearCell("pyscript") |
| 15 | + js.clearGraphContainer("graph-container-py") |
| 16 | + js.startPyTimer() |
34 | 17 |
|
| 18 | + # 1. Generación de datos |
| 19 | + start_time = time.perf_counter() |
| 20 | + num_series = 5 |
| 21 | + num_points = 10_000 |
| 22 | + x = np.linspace(0, 10, num_points) |
| 23 | + rng = np.random.default_rng() |
| 24 | + ys = [np.sin(x + i) + rng.normal(0, 0.1, num_points) |
| 25 | + for i in range(num_series)] |
| 26 | + data_gen_time = (time.perf_counter() - start_time) * 1000 |
35 | 27 |
|
36 |
| -async def wait_for_plotly(): |
37 |
| - while not hasattr(js, "Plotly"): |
38 |
| - await asyncio.sleep(0.1) |
39 |
| - return js.Plotly |
| 28 | + # 2. Renderizado |
| 29 | + render_start = time.perf_counter() |
| 30 | + fig = Figure() |
| 31 | + for i, y in enumerate(ys): |
| 32 | + fig.add_scatter( |
| 33 | + x=x, |
| 34 | + y=y, |
| 35 | + mode="lines", |
| 36 | + name=f"Serie {i+1}", |
| 37 | + line=dict(width=1) |
| 38 | + ) |
40 | 39 |
|
| 40 | + fig.update_layout( |
| 41 | + title="Series Temporales (PyScript)", |
| 42 | + width=700, |
| 43 | + height=500 |
| 44 | + ) |
41 | 45 |
|
42 |
| -async def index(): |
43 |
| - years = list(data.keys()) |
44 |
| - parties = list(data.values()) |
| 46 | + render_time = (time.perf_counter() - render_start) * 1000 |
45 | 47 |
|
46 |
| - trace = go.Scatter( |
47 |
| - x=years, |
48 |
| - y=parties, |
49 |
| - mode='lines+markers', |
50 |
| - name='US Presidential Party', |
51 |
| - line=dict(color='blue'), |
52 |
| - marker=dict( |
53 |
| - size=8, |
54 |
| - color=['blue' if party == |
55 |
| - 'Democratic' else 'red' for party in parties], |
56 |
| - symbol=['circle' if party == |
57 |
| - 'Democratic' else 'square' for party in parties] |
58 |
| - ) |
59 |
| - ) |
| 48 | + # 3. Exportar la figura a JSON |
| 49 | + graph_json = to_json(fig) |
60 | 50 |
|
61 |
| - layout = go.Layout( |
62 |
| - title='US Presidential Party Control', |
63 |
| - xaxis=dict(title='Year'), |
64 |
| - yaxis=dict(title='Party', tickvals=['Democratic', 'Republican']), |
65 |
| - hovermode='closest' |
66 |
| - ) |
| 51 | + # 4. Mostrar resultados: Enviar el JSON al JS |
| 52 | + js.displayPlotPy(graph_json) |
| 53 | + update_ui({ |
| 54 | + "data_gen_time": data_gen_time, |
| 55 | + "render_time": render_time, |
| 56 | + "total_time": (time.perf_counter() - start_time) * 1000 |
| 57 | + }) |
| 58 | + js.stopPyTimer() |
67 | 59 |
|
68 |
| - fig = go.Figure(data=[trace], layout=layout) |
69 |
| - graphJSON = json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder) |
70 |
| - window.console.log("1-----") |
71 |
| - window.console.log(graphJSON) |
72 |
| - window.console.log("2-----") |
73 |
| - await wait_for_plotly() |
74 |
| - plot = window.Plotly.newPlot("chart1", window.JSON.parse(graphJSON)) |
| 60 | + except Exception as e: |
| 61 | + display(f"Error: {e}", target="pyscript-output") |
75 | 62 |
|
76 | 63 |
|
77 |
| -# Ejecutar la función asíncrona principal en el loop de PyScript |
78 |
| -asyncio.ensure_future(index()) |
| 64 | +def update_ui(metrics): |
| 65 | + display(f"Generación datos: {metrics['data_gen_time']:.2f} ms", |
| 66 | + target="pyscript-output") |
| 67 | + display(f"Renderizado: {metrics['render_time']:.2f} ms", |
| 68 | + target="pyscript-output") |
| 69 | + display(f"TOTAL: {metrics['total_time']:.2f} ms", |
| 70 | + target="pyscript-exact") |
0 commit comments