Setting up Observable Notebook Kit
If you use a Mac computer, then please dowload and use Observable Desktop.
Go to https://observablehq.com/notebook-kit/desktop and then scroll down to the Download link. (If you have time and interest, watch the video.) Install the app.
If you use a Windows PC or other computer, then you will be using your own development editor (like Visual Studio Code) which is a little more complicated but can be made to work in consultation with myself.
To use Observable Desktop you will need a free Observable account. Go to the Observable Desktop Guide to learn how to do this and also read through the guide for information about editing.
Create your First Project Folder and Set Up the Notebook Kit
The desktop app is a notebook editor that creates, edits and saves files that are, basically, HTML (and have .html as their extension). You will be editing these files inside folders that are also set up to use the Observable Notebook Kit in order to turn your notebooks into publishable web sites.
- In whatever way is most comfortable for you (and best organized) create a folder called something appropriate like:
ObservableNotebooks. - Inside this folder make another folder called
docs. This is where you will create and store your notebook files. - Open Observable Desktop and then edit the
Untitleddefault document that appears. Edit the title and create a few more MarkDown (md) cells with some (placeholder) text in them. - Select
File —> Save As...to save what you’ve done, with a name based on the title you chose, inside thedocsfolder of your project folder. - Now use your Terminal program and its Command Line Interface (CLI) to navigate to your
ObservableNotebooksproject folder. (If you haven’t done this before, we’ll do it Together First In Workshop TFIW.) - And, if you’ve never done so before, you will need to install
node.jsand a Package Manager callednpm(Node Package Manager). Go to step 8 if you already havenpminstalled, otherwise: - Download the installer for
node.jsthat is best for your computer. If you have a Mac download from here. Verify at the terminal with (on separate lines):node -vnpm -v
(which will show the installed versions of these packages). - Next, at the Terminal prompt type:
npm add @observablehq/notebook-kit - Hopefully you have a plain text editor that you like to use. Find and edit the file
package.json(which has been created by the previous step) so that it looks like this (it’s thescriptspart that needs adding):
{
"dependencies": {
"@observablehq/notebook-kit": "^1.5.0"
},
"scripts": {
"preview": "notebooks preview --root docs --template docs/nochrome.tmpl",
"build": "notebooks build --root docs --template docs/nochrome.tmpl --out ../dist -- docs/*.html"
}
}
- This file refers to another, a template file, called
nochrome.tmplso now you also need to create this file inside yourdocsfolder. Make a new plain text file using you editor and save it in thedocsfolder. Make sure the file name has that.tmplextension. Its contents should look like this:
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style type="text/css">
@import url("observable:styles/index.css");
* {
margin-top:0 !important;
}
html {
background-color: white;
margin:0;
padding::2vw; /* change this to 0 to override default */
border:0;
}
body {
max-width: none;
margin:0;
padding:0;
border:0;
}
</style>
</head>
<body>
<main></main>
</body>
</html>
- Finally (for now) in your terminal, type:
npm run preview. Then typehand hit return. We’ll do and discuss the rest TIFW.
let count = 0;
let intervalId;
const displayElement = display(htl.html`<div class="demo">${count}</div>`);
function updateCounter() {
displayElement.textContent = count;
count = ++count % 10;
}
// Start the counter after initial display
intervalId = setInterval(updateCounter, 1000);
// Stop the counter when the cell is invalidated (e.g., when re-run)
invalidation.then(() => clearInterval(intervalId));o = {
let o = 0;
while (true) {
if (o >= 1) { o = 0; }
yield o;
o += 0.02;
}
}Blink
<p class="demo" style=opacity:${o}>Blink</p>
<style>
.demo {
color:red;
font-weight:bold;
font-size:2vw;
}
</style>Downloading Legacy Notebooks from the Observable Platform
Use a command of this form in a folder that has the Notebook Kit set up:
notebooks download https://observablehq.com/@[user]/[nbk] > docs/[nbk].html
viewof count = {
let i = 0;
const button = html`<button>Count: ${i}`;
button.onclick = () => {
button.textContent = `Count: ${++i}`;
button.dispatchEvent(new CustomEvent("input"));
};
return button;
}<div id="display"></div>document.getElementById("display").innerText = `This is the innerText of an HTML div element whose id is "display".
This text was put into the element by JavaScript in the following cell.`