diff --git a/examples/visualizations/observable_plot/app.py b/examples/visualizations/observable_plot/app.py new file mode 100644 index 0000000..cb1fda5 --- /dev/null +++ b/examples/visualizations/observable_plot/app.py @@ -0,0 +1,48 @@ +from fasthtml.common import * +import numpy as np + +plot_js = """ +function createPlot(data) { + const plot = Plot.rectY(data, Plot.binX({y: "count"},{x: d => d.value,fill:"steelblue"})).plot(); + const div = document.querySelector("#plot"); + div.replaceChildren(plot); +} + +// Set up initial load via HTMX +htmx.on('htmx:afterSettle', evt => { + if (evt.detail.target.id === 'data-store') { + // The data is now properly JSON-encoded + const data = JSON.parse(evt.detail.target.textContent); + createPlot(data); + } +}); +""" + +app, rt = fast_app( + hdrs=(Script(src="https://cdn.jsdelivr.net/npm/d3@7"), + Script(src="https://cdn.jsdelivr.net/npm/@observablehq/plot@0.6"))) + +@rt +def index(): + return Div( + H1(A("Observable", href="https://observablehq.com/@observablehq/plot", target="_blank"), " Plot Demo"), + P("The data is randomly generated on the server and is fetched on initial page load."), + P("Try opening the browser developer tools and viewing the Network tab to see the data reponse for each http request."), + # On bytton click it sends a get request to the `get_data` route and puts the response in the `data-store` div + Button("Fetch New Data", get=get_data, hx_target="#data-store", cls='btn btn-primary'), + # Store for the JSON chart data + Div(id="data-store", get=get_data, hx_trigger="load", hidden=True), + # Plot container + Div(id="plot"), + # Include the JavaScript for the plot + Script(plot_js) + ) + +@rt +def get_data(): + # Generate sample data + data = [{"value": float(x)} for x in np.random.randn(100)] + # Return as proper JSON response + return JSONResponse(data) + +serve() diff --git a/examples/visualizations/observable_plot/card_thumbnail.gif b/examples/visualizations/observable_plot/card_thumbnail.gif new file mode 100644 index 0000000..844ff75 Binary files /dev/null and b/examples/visualizations/observable_plot/card_thumbnail.gif differ diff --git a/examples/visualizations/observable_plot/card_thumbnail.png b/examples/visualizations/observable_plot/card_thumbnail.png new file mode 100644 index 0000000..0cff987 Binary files /dev/null and b/examples/visualizations/observable_plot/card_thumbnail.png differ diff --git a/examples/visualizations/observable_plot/metadata.ini b/examples/visualizations/observable_plot/metadata.ini new file mode 100644 index 0000000..e92b630 --- /dev/null +++ b/examples/visualizations/observable_plot/metadata.ini @@ -0,0 +1,4 @@ +[REQUIRED] +ImageAltText=Demo of Observable Plot charts in FastHTML +ComponentName=Observable Plot Bar Chart +ComponentDescription=How to display an Observable Plot bar chart in FastHTML, by David Gwyer. \ No newline at end of file diff --git a/examples/visualizations/plotly_selections/card_thumnail.gif b/examples/visualizations/plotly_selections/card_thumbnail.gif similarity index 100% rename from examples/visualizations/plotly_selections/card_thumnail.gif rename to examples/visualizations/plotly_selections/card_thumbnail.gif