Skip to content

Commit 570334c

Browse files
committed
checkpoint documentation
1 parent 85961bb commit 570334c

File tree

3 files changed

+352
-10
lines changed

3 files changed

+352
-10
lines changed

docs/javascript/generators.md

Lines changed: 66 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,32 @@
11
# Generators
22

3-
When code refers to a [generator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Generator) defined in another code block, the referencing code automatically runs each time the generator yields a value. Values that change over time, such as interactive inputs and animation parameters, are often represented as generators. For example, you can use [Observable Inputs](https://github.com/observablehq/inputs) and the built-in [`view` function](./inputs) to construct a live text input. Try entering your name into the box below:
3+
Values that change over time, such as interactive inputs and animation parameters, can represented as [async generators](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Generator). When a top-level generator is declared, code in other blocks sees the generator’s latest yielded value and runs whenever the generator yields a new value.
4+
5+
For example, here is a generator that increments once a second:
46

57
```js show
6-
const name = view(Inputs.text({label: "Name", placeholder: "Enter your name"}));
8+
const j = (async function* () {
9+
for (let j = 0; true; ++j) {
10+
yield j;
11+
await new Promise((resolve) => setTimeout(resolve, 1000));
12+
}
13+
})();
714
```
815

9-
Name is: ${name}.
10-
11-
The `view` function calls `Generators.input` under the hood, which takes an input element and returns a generator that yields the input’s value whenever it changes. The code above can be written more explicitly as:
16+
The value of j is: ${j}.
1217

13-
```js no-run
14-
const nameInput = Inputs.text({label: "Name", placeholder: "Enter your name"});
15-
const name = Generators.input(nameInput);
18+
If the generator is synchronous, the generator will yield every animation frame, which is typically 60 frames per second:
1619

17-
display(nameInput);
20+
```js show
21+
const i = (function* () {
22+
for (let i = 0; true; ++i) {
23+
yield i;
24+
}
25+
})();
1826
```
1927

28+
The value of i is: ${i}.
29+
2030
As another example, you can use the built-in `Generators.observe` to represent the current pointer coordinates:
2131

2232
```js show
@@ -29,3 +39,50 @@ const pointer = Generators.observe((notify) => {
2939
```
3040

3141
Pointer is: ${pointer.map(Math.round).join(", ")}.
42+
43+
A WebSocket that lists for Blockchain transactions:
44+
45+
```js show
46+
const socket = new WebSocket("wss://ws.blockchain.info/inv");
47+
invalidation.then(() => socket.close());
48+
socket.addEventListener("open", () => socket.send(JSON.stringify({op: "unconfirmed_sub"})));
49+
const messages = Generators.observe((notify) => {
50+
const messages = [];
51+
const messaged = (event) => {
52+
messages.unshift(JSON.parse(event.data));
53+
if (messages.length > 30) {
54+
messages.pop();
55+
socket.close();
56+
}
57+
notify(messages.slice());
58+
};
59+
socket.addEventListener("message", messaged);
60+
return () => socket.removeEventListener("message", messaged);
61+
});
62+
```
63+
64+
```js
65+
messages
66+
```
67+
68+
```js
69+
Inputs.table(
70+
messages.map((d) => ({
71+
time: new Date(d.x.time * 1000),
72+
hash: d.x.hash,
73+
ins: d3.sum(d.x.inputs.map((d) => d.prev_out.value)) / 1e8,
74+
outs: d3.sum(d.x.out.map((d) => d.value)) / 1e8
75+
}))
76+
)
77+
```
78+
79+
An HTML input element and `Generators.input`:
80+
81+
```js show
82+
const nameInput = display(document.createElement("input"));
83+
const name = Generators.input(nameInput);
84+
```
85+
86+
Name is: ${name}.
87+
88+
See the [`view` function](./inputs) for shorthand inputs.

docs/javascript/inputs.md

Lines changed: 235 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,239 @@
11
# Inputs
22

3-
As described above, this function displays the given `input` and then returns its corresponding generator via `Generators.input`. Use this to display an input element while also declaring the input’s current value as a reactive top-level variable.
3+
You can use the built-in [`view` function](#view(input)) and an HTML input element to create a reactive input. For example, here is a slider:
4+
5+
```js show
6+
const gain = view(Inputs.range([0, 11], {value: 5, step: 0.1, label: "Gain"}));
7+
```
8+
9+
Now you can reference the input’s value (here `gain`) anywhere. The code will run whenever the input changes; no event listeners required!
10+
11+
```md
12+
The current gain is ${gain.toFixed(1)}!
13+
```
14+
15+
The current gain is ${gain.toFixed(1)}!
416

517
## view(*input*)
18+
19+
As described above, this function [displays](./display) the given input element and then returns its corresponding [generator](./generators) via `Generators.input`. Use this to display an input element while also exposing the input’s value as a [reactive top-level variable](./reactivity).
20+
21+
## Built-in inputs
22+
23+
These basic inputs will get you started.
24+
25+
* [Button](#button) - do something when a button is clicked
26+
* [Toggle](#toggle) - toggle between two values (on or off)
27+
* [Checkbox](#checkbox) - choose any from a set
28+
* [Radio](#radio) - choose one from a set
29+
* [Range](#range) or [Number](https://observablehq.com/@observablehq/input-range) - choose a number in a range (slider)
30+
* [Select](#select) - choose one or any from a set (drop-down menu)
31+
* [Text](#text) - enter freeform single-line text
32+
* [Textarea](#textarea) - enter freeform multi-line text
33+
* [Date](#date) or [Datetime](https://observablehq.com/@observablehq/input-date) - choose a date
34+
* [Color](#color) - choose a color
35+
* [File](#file) - choose a local file
36+
37+
These fancy inputs are designed to work with tabular data such as CSV or TSV [file attachments](./files).
38+
39+
* [Search](#search) - query a tabular dataset
40+
* [Table](#table) - browse a tabular dataset
41+
42+
---
43+
44+
### Button
45+
46+
Do something when a button is clicked. [Examples ›](https://observablehq.com/@observablehq/input-button) [API Reference ›](https://github.com/observablehq/inputs/blob/main/README.md#button)
47+
48+
```js show
49+
const clicks = view(Inputs.button("Click me"));
50+
```
51+
52+
```js
53+
clicks
54+
```
55+
56+
---
57+
58+
### Toggle
59+
60+
Toggle between two values (on or off). [Examples ›](https://observablehq.com/@observablehq/input-toggle) [API Reference ›](https://github.com/observablehq/inputs/blob/main/README.md#toggle)
61+
62+
```js show
63+
const mute = view(Inputs.toggle({label: "Mute"}));
64+
```
65+
66+
```js
67+
mute
68+
```
69+
70+
---
71+
72+
### Checkbox
73+
74+
Choose any from a set. [Examples ›](https://observablehq.com/@observablehq/input-checkbox) [API Reference ›](https://github.com/observablehq/inputs/blob/main/README.md#checkbox)
75+
76+
```js show
77+
const flavors = view(Inputs.checkbox(["salty", "sweet", "bitter", "sour", "umami"], {label: "Flavors"}));
78+
```
79+
80+
```js
81+
flavors
82+
```
83+
84+
---
85+
86+
### Radio
87+
88+
Choose one from a set. [Examples ›](https://observablehq.com/@observablehq/input-radio) [API Reference ›](https://github.com/observablehq/inputs/blob/main/README.md#radio)
89+
90+
```js show
91+
const flavor = view(Inputs.radio(["salty", "sweet", "bitter", "sour", "umami"], {label: "Flavor"}));
92+
```
93+
94+
```js
95+
flavor
96+
```
97+
98+
---
99+
100+
### Range
101+
102+
Pick a number. [Examples ›](https://observablehq.com/@observablehq/input-range) [API Reference ›](https://github.com/observablehq/inputs/blob/main/README.md#range)
103+
104+
```js show
105+
const n = view(Inputs.range([0, 255], {step: 1, label: "Favorite number"}));
106+
```
107+
108+
```js
109+
n
110+
```
111+
112+
---
113+
114+
### Select
115+
116+
Choose one, or any, from a menu. [Examples ›](https://observablehq.com/@observablehq/input-select) [API Reference ›](https://github.com/observablehq/inputs/blob/main/README.md#select)
117+
118+
```js
119+
const capitals = FileAttachment("us-state-capitals.tsv").tsv({typed: true});
120+
const stateNames = capitals.then((capitals) => capitals.map(d => d.State));
121+
```
122+
123+
```js show
124+
const homeState = view(Inputs.select([null].concat(stateNames), {label: "Home state"}));
125+
```
126+
127+
```js
128+
homeState
129+
```
130+
131+
```js show
132+
const visitedStates = view(Inputs.select(stateNames, {label: "Visited states", multiple: true}));
133+
```
134+
135+
```js
136+
visitedStates
137+
```
138+
139+
---
140+
141+
### Text
142+
143+
Enter freeform single-line text. [Examples ›](https://observablehq.com/@observablehq/input-text) [API Reference ›](https://github.com/observablehq/inputs/blob/main/README.md#text)
144+
145+
```js show
146+
const name = view(Inputs.text({label: "Name", placeholder: "What’s your name?"}));
147+
```
148+
149+
```js
150+
name
151+
```
152+
153+
---
154+
155+
### Textarea
156+
157+
Enter freeform multi-line text. [Examples ›](https://observablehq.com/@observablehq/input-textarea) [API Reference ›](https://github.com/observablehq/inputs/blob/main/README.md#textarea)
158+
159+
```js show
160+
const bio = view(Inputs.textarea({label: "Biography", placeholder: "What’s your story?"}));
161+
```
162+
163+
```js
164+
bio
165+
```
166+
167+
---
168+
169+
### Date
170+
171+
Choose a date, or a date and time. [Examples ›](https://observablehq.com/@observablehq/input-date) [API Reference ›](https://github.com/observablehq/inputs/blob/main/README.md#date)
172+
173+
```js show
174+
const birthday = view(Inputs.date({label: "Birthday"}));
175+
```
176+
177+
```js
178+
birthday
179+
```
180+
181+
---
182+
183+
### Color
184+
185+
Choose a color. [Examples ›](https://observablehq.com/@observablehq/input-color) [API Reference ›](https://github.com/observablehq/inputs/blob/main/README.md#color)
186+
187+
```js show
188+
const color = view(Inputs.color({label: "Favorite color", value: "#4682b4"}));
189+
```
190+
191+
```js
192+
color
193+
```
194+
195+
---
196+
197+
### File
198+
199+
Choose a local file. [Examples ›](https://observablehq.com/@observablehq/input-file) [API Reference ›](https://github.com/observablehq/inputs/blob/main/README.md#file)
200+
201+
```js show
202+
const file = view(Inputs.file({label: "CSV file", accept: ".csv", required: true}));
203+
```
204+
205+
```js
206+
data = file.csv({typed: true})
207+
```
208+
209+
---
210+
211+
### Search
212+
213+
Query a tabular dataset. [Examples ›](https://observablehq.com/@observablehq/input-search) [API Reference ›](https://github.com/observablehq/inputs/blob/main/README.md#search)
214+
215+
```js show
216+
const search = view(Inputs.search(capitals, {placeholder: "Search U.S. capitals"}));
217+
```
218+
219+
```js
220+
search // see table below!
221+
```
222+
223+
---
224+
225+
### Table
226+
227+
Browse a tabular dataset. [Examples ›](https://observablehq.com/@observablehq/input-table) [API Reference ›](https://github.com/observablehq/inputs/blob/main/README.md#table)
228+
229+
```js show
230+
const rows = view(Inputs.table(search));
231+
```
232+
233+
```js
234+
rows // click a checkbox in the leftmost column
235+
```
236+
237+
---
238+
239+
* [Form](https://observablehq.com/@observablehq/input-form?collection=@observablehq/inputs) - combine multiple inputs for a compact display

docs/javascript/us-state-capitals.tsv

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
State Capital Since Area Population MSA/µSA CSA Rank
2+
Alabama Montgomery 1846-01-01 159.8 198525 373290 461516 3
3+
Alaska Juneau 1906-01-01 2716.7 32113 32113 3
4+
Arizona Phoenix 1912-01-01 517.6 1680992 4948203 5002221 1
5+
Arkansas Little Rock 1821-01-01 116.2 197312 742384 908941 1
6+
California Sacramento 1854-01-01 97.9 513624 2363730 2639124 6
7+
Colorado Denver 1867-01-01 153.3 727211 2967239 3617927 1
8+
Connecticut Hartford 1875-01-01 17.3 122105 1204877 1470083 3
9+
Delaware Dover 1777-01-01 22.4 38079 180786 7209620 2
10+
Florida Tallahassee 1824-01-01 95.7 194500 387227 7
11+
Georgia Atlanta 1868-01-01 133.5 506811 6020364 6853392 1
12+
Hawaii Honolulu 1845-01-01 68.4 345064 974563 1
13+
Idaho Boise 1865-01-01 63.8 228959 749202 831235 1
14+
Illinois Springfield 1837-01-01 54.0 114230 206868 306399 6
15+
Indiana Indianapolis 1825-01-01 361.5 876384 2074537 2457286 1
16+
Iowa Des Moines 1857-01-01 75.8 214237 699292 877991 1
17+
Kansas Topeka 1856-01-01 56.0 125310 231969 4
18+
Kentucky Frankfort 1792-01-01 14.7 27679 73663 745033 15
19+
Louisiana Baton Rouge 1880-01-01 76.8 220236 854884 2
20+
Maine Augusta 1832-01-01 55.4 18681 122302 8
21+
Maryland Annapolis 1694-01-01 6.73 39174 2800053 9814928 7
22+
Massachusetts Boston 1630-01-01 89.6 692600 4873019 8287710 1
23+
Michigan Lansing 1847-01-01 35.0 118210 550391 5
24+
Minnesota Saint Paul 1849-01-01 52.8 308096 3654908 4027861 2
25+
Mississippi Jackson 1821-01-01 104.9 160628 594806 674340 1
26+
Missouri Jefferson City 1826-01-01 27.3 42838 151235 15
27+
Montana Helena 1875-01-01 14.0 32315 77414 6
28+
Nebraska Lincoln 1867-01-01 74.6 289102 336374 357887 2
29+
Nevada Carson City 1861-01-01 143.4 55916 55916 637973 6
30+
New Hampshire Concord 1808-01-01 64.3 43627 151391 8287710 3
31+
New Jersey Trenton 1784-01-01 7.66 83203 367430 22589036 10
32+
New Mexico Santa Fe 1610-01-01 37.3 84683 150358 1158464 4
33+
New York Albany 1797-01-01 21.4 96460 880381 1167594 6
34+
North Carolina Raleigh 1792-01-01 114.6 474069 1390785 2079687 2
35+
North Dakota Bismarck 1883-01-01 26.9 73529 128949 2
36+
Ohio Columbus 1816-01-01 210.3 898553 2122271 2525639 1
37+
Oklahoma Oklahoma City 1910-01-01 620.3 655057 1408950 1481542 1
38+
Oregon Salem 1855-01-01 45.7 174365 433903 3259710 3
39+
Pennsylvania Harrisburg 1812-01-01 8.11 49528 577941 1271801 9
40+
Rhode Island Providence 1900-01-01 18.5 179883 1624578 8287710 1
41+
South Carolina Columbia 1786-01-01 125.2 131674 838433 963048 2
42+
South Dakota Pierre 1889-01-01 13.0 13646 20672 8
43+
Tennessee Nashville 1826-01-01 525.9 670820 1934317 2062547 1
44+
Texas Austin 1839-01-01 305.1 978908 2227083 4
45+
Utah Salt Lake City 1858-01-01 109.1 200567 1232696 2641048 1
46+
Vermont Montpelier 1805-01-01 10.2 7855 6
47+
Virginia Richmond 1780-01-01 60.1 230436 1291900 4
48+
Washington Olympia 1853-01-01 16.7 46478 290536 4903675 24
49+
West Virginia Charleston 1885-01-01 31.6 46536 257074 776694 1
50+
Wisconsin Madison 1838-01-01 68.7 259680 664865 892661 2
51+
Wyoming Cheyenne 1869-01-01 21.1 64235 99500 1

0 commit comments

Comments
 (0)