Observable Framework try-out

My first Observable Framework project
Dashboard
Observable
til
Author
Published

June 19, 2024

Modified

June 19, 2024

Iโ€™ve heard that Observable released their Framework as open source in April 2024, and Quarto also supported Observable natively since version 1.4 (?), thus decided to give it a try. Could not agree more with their slogan:

The best dashboards are built with code.

This is my repo for the practice.

๐Ÿš€ Observable Framwork documentation.

Observable Framework โ€” or โ€œFrameworkโ€ for short โ€” is an open-source static-site generator for data apps. By data app we mean an application that is primarily a display of data. Data apps help you derive insights (to understand) and evaluate potential decisions (to take action).

It can be a set of coordinated interactive visualizations for โ€œself-serviceโ€ analysis, a live dashboard, or a point-in-time report that combines graphics and prose to present in-depth analysis.

Why use Framework for your data app?

Letโ€™s start!

Inititate a new Project

๐Ÿš€ Observable Framwork getting started

The prerequisite is we must have npm installed on our machine. Running npm -v, I know that I have version 10.5.0 installed.

We first create a folder and navigate to it:

#| eval: false
#| echo: true
mkdir first-observable-project
cd first-observable-project

Then simply run this command to initiate the project:

#| eval: false
#| echo: true
npm init "@observablehq"

We gotta fill some basic params:

#| eval: false
#| echo: true
โ”Œ   observable create  v1.5.1
โ”‚
โ—‡  Where to create your project?
โ”‚  hi-observable
โ”‚
โ—‡  What to title your project?
โ”‚  Hi Observable
โ”‚
โ—‡  Include sample files to help you get started?
โ”‚  No
โ”‚
โ—‡  Install dependencies?
โ”‚  Yes, via npm
โ”‚
โ—‡  Initialize git repository?
โ”‚  No
โ”‚
โ—‡  Installed! ๐ŸŽ‰
โ”‚
โ—‡  Next stepsโ€ฆ โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚                    โ”‚
โ”‚  cd hi-observable  โ”‚
โ”‚  npm run dev       โ”‚
โ”‚                    โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ
โ”‚
โ””  Problems? https://observablehq.com/framework/getting-started

We can now easilly preview the app with npm run dev, and access out dashboard via http://127.0.0.1:3000/. Letโ€™s play a little bit, we can modify the h1 tags in docs\index.md, then the dashboard will be updated once we save the file, instantly. Cool!

Home page layout

Here is the structure of my project (after adding those Data Loader, SQL, etc steps below):

Self hosting

We donโ€™t have to deploy to Observable โ€” Framework projects are simply static sites, so we can host them anywhere. With npm, we can run npm run build, which will generates the dist directory. We can then copy this directory to your static site server or preferred hosting service, like http-server, with command npx http-server dist.

For deployment, see the final section at the bottom of this article.

Write first Data Loader

In Observable project we can write Data Loader in JavaScript, Python, R or any other languages. Data Loader helps you to (1) minimize file sizes and (2) reduce client side processing.

A data loader file has the name like: dataloader.data_extension.engine_extension. For example, with the American weather forecast API we can have 2 kinds of Data Loader as below:

Javascript ๐ŸŸจ:

forecast.json.js
const longitude = -122.47;
const latitude = 37.8;

async function json(url) {
  const response = await fetch(url, {headers: {"User-Agent": "(observablehq.com, support@observablehq.com)"}});
  if (!response.ok) throw new Error(`fetch failed: ${response.status}`);
  return await response.json();
}

const station = await json(`https://api.weather.gov/points/${latitude},${longitude}`);
const forecast = await json(station.properties.forecastHourly);

process.stdout.write(JSON.stringify(forecast));

Python ๐Ÿ:

forecast.json.py
import json
import requests
import sys

longitude = -122.47
latitude = 37.80

station = requests.get(f"https://api.weather.gov/points/{latitude},{longitude}").json()
forecast = requests.get(station["properties"]["forecastHourly"]).json()

json.dump(forecast, sys.stdout)

Run the data loader, testing if it work:

#| eval: false
#| echo: true
node hi-observable\docs\data\forecast.json.js

# or

python hi-observable\docs\data\forecast.json.py

Now if we refresh the dashboard page, the weather forecasting tab should be there. My first run was failed as I miss a comma in JS file ๐Ÿ˜‚. If we change the data loader, the dashboard will change concurrently.

SQL in Observable Framework

Grids, Cards, and Themes

The Grid class allow us to organize multiple vizualization in a denser format, like a dashboard. The Grid class declares a grid container.

Deployment a Project

Imports: from npm, local files, remote URLs, and lazy loading