Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions examples/visualizations/observable_plot/app.py
Original file line number Diff line number Diff line change
@@ -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()
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions examples/visualizations/observable_plot/metadata.ini
Original file line number Diff line number Diff line change
@@ -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.