|
1 | 1 | from fasthtml.common import *
|
2 | 2 | import numpy as np
|
3 |
| -from starlette.responses import JSONResponse |
4 | 3 |
|
5 | 4 | plot_js = """
|
6 | 5 | function createPlot(data) {
|
|
20 | 19 | """
|
21 | 20 |
|
22 | 21 | app, rt = fast_app(
|
23 |
| - pico=False, |
24 |
| - hdrs=( |
25 |
| - Script(src="https://cdn.jsdelivr.net/npm/d3@7"), |
26 |
| - Script( src="https://cdn.jsdelivr.net/npm/@observablehq/[email protected]"), |
27 |
| - Script(src="https://unpkg.com/@tailwindcss/browser@4") |
28 |
| - ) |
29 |
| -) |
| 22 | + hdrs=(Script(src="https://cdn.jsdelivr.net/npm/d3@7"), |
| 23 | + Script( src="https://cdn.jsdelivr.net/npm/@observablehq/[email protected]"))) |
30 | 24 |
|
31 | 25 | @rt
|
32 | 26 | def index():
|
33 | 27 | return Div(
|
34 |
| - H1("Observable Plot Demo", cls="text-4xl font-bold"), |
35 |
| - P("This FastHTML example renders an ", A("Observable Plot", href="https://observablehq.com/@observablehq/plot", target="_blank", cls="visited:text-purple-600 underline"), " chart. The data is randomly generated on the server and is fetched on initial page load. You can also click the button to fetch new random data from the server. Try opening the browser developer tools and viewing the Network tab to see the data reponse for each http request.", cls="my-4"), |
36 |
| - Button("Refresh Data", hx_get="get_data", hx_target="#data-store", cls="cursor-pointer rounded-md bg-slate-600 px-3.5 py-2.5 my-2 text-sm font-semibold text-white shadow-xs hover:bg-slate-500 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-slate-600"), |
| 28 | + H1(A("Observable", href="https://observablehq.com/@observablehq/plot", target="_blank"), " Plot Demo"), |
| 29 | + P("The data is randomly generated on the server and is fetched on initial page load."), |
| 30 | + P("Try opening the browser developer tools and viewing the Network tab to see the data reponse for each http request."), |
| 31 | + # On bytton click it sends a get request to the `get_data` route and puts the response in the `data-store` div |
| 32 | + Button("Fetch New Data", get=get_data, hx_target="#data-store", cls='btn btn-primary'), |
37 | 33 | # Store for the JSON chart data
|
38 |
| - Div(id="data-store", hx_get="get_data", hx_trigger="load", cls="hidden"), |
| 34 | + Div(id="data-store", get=get_data, hx_trigger="load", hidden=True), |
39 | 35 | # Plot container
|
40 |
| - Div(id="plot", cls="mt-12"), |
| 36 | + Div(id="plot"), |
41 | 37 | # Include the JavaScript for the plot
|
42 |
| - Script(plot_js), |
43 |
| - cls="m-12" |
44 |
| - ) |
| 38 | + Script(plot_js) |
| 39 | + ) |
45 | 40 |
|
46 | 41 | @rt
|
47 | 42 | def get_data():
|
|
0 commit comments