This repository has been archived by the owner on Jan 3, 2018. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
tutorial.html
3386 lines (2938 loc) · 104 KB
/
tutorial.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
layout: lesson
root: ../..
title: Instructor's Guide for Web Programming
order: ["history"]
---
<section>
<h2>Opening</h2>
<div id="s:web:opening" class="opening">
<p>
Carla Climate is studying climate change in the Northern and Southern hemispheres.
As part of her work,
she wants to see whether the gap between annual temperatures in Canada and Australia
increased during the Twentieth Century.
The raw data she needs is available online;
her goal is to get it,
do her calculations,
and then post her results so that other scientists can use them.
</p>
<p>
This chapter is about how she can do that.
More specifically,
it's about how to fetch data from the web,
and how to create web pages that are useful to both human beings and computers.
What we will <em>not</em> cover is how to build interactive web applications;
making those secure is more work than we can cover in the time we have.
However,
everything in this chapter is a prerequisite for interactive apps,
and there are other good tutorials available
if you decide that's what you really need. Carla's goal is to share with everyone,
and that's the easiest kind of site to create.
</p>
</div>
</section>
<section>
<h2>Instructors</h2>
<div id="s:web:instructors" class="instructors">
<p>FIXME</p>
</div>
</section>
<section id="s:web:history">
<h2>How We Got Here</h2>
<h3>Objectives</h3>
<div id="s:web:history:objectives" class="objectives">
<ul>
<li>Distinguish between human-readable and machine-readable data.</li>
<li>Explain the relationship between HTML and XML.</li>
</ul>
</div>
<h3>Lesson</h3>
<div id="s:web:history:lesson" class="lesson">
<p>
To start,
let's have another look at the hearing tests from
<a href="python.html">our chapter on Python programming</a>.
Most people would probably store these results in a plain text file
with one row for each test:
</p>
<pre>
Date Experimenter Subject Test Score
---------- ------------ ------- ----- -----
2011-05-02 A. Binet H. Ebbinghaus DL-11 88%
2011-05-07 A. Binet H. Ebbinghaus DL-12 71%
2011-05-02 A. Binet W. Wundt DL-11 29%
2011-05-02 C. S. Pierce W. Wundt DL-11 45%
</pre>
<p>
This is pretty much what a conscientious researcher would write in a lab notebook,
and is easy for a human being to read.
It's a lot harder for a computer to understand, though.
Any program that wanted to load this data
would have to know that the first line of the file contains column titles,
that the second can be ignored,
that the first field of each row thereafter should be translated from text into a date,
that the fields after that start in particular columns
(since the number of spaces between them is variable,
and the number of spaces inside names can also vary—compare
"A. Binet" with "C. S. Pierce"),
and so on.
Such a program would not be hard to write,
but having to write, debug, and maintain a separate program for each data set
would be tedious.
</p>
<p>
Now consider something like this quotation
from Richard Feynman's 1965 Nobel Prize acceptance speech:
</p>
<blockquote>
As a by-product of this same view,
I received a telephone call one day at the graduate college at Princeton from Professor Wheeler,
in which he said,
"Feynman, I know why all electrons have the same charge and the same mass."
"Why?"
"Because, they are all the same electron!"
</blockquote>
<p>
A lot of information is implicit in these four sentences,
like the fact that "Wheeler" and "Feynman" are particular people,
that "Princeton" is a place,
that the speakers are alternating (with Wheeler speaking first),
and so on.
None of that is "visible" to a computer program,
so if we had a database containing millions of documents
and wanted to see which ones mentioned both John Wheeler
(the physicist, not the geologist)
and Princeton (the university, not the glacier),
we might have to wade through a lot of false matches.
What we need is some way to explicitly tell a computer
all the things that human beings are able to infer.
</p>
<p>
An early effort to tackle this problem dates back to 1969,
when Charles Goldfarb and others at IBM created
the <a href="glossary.html#sgml">Standard Generalized Markup Language</a>, or SGML.
It was designed as a way of adding extra data
to medical and legal documents
so that programs could search them more accurately.
SGML was very complex (the specification is over 500 pages long),
and unless you were a specialist,
you probably didn't even know it existed:
all you saw were the programs that used it.
</p>
<p>
But in 1989 Tim Berners-Lee borrowed the syntax of SGML
to create the <a href="glossary.html#html">HyperText Markup Language</a>, or HTML,
for his new "World Wide Web".
HTML looked superficially the same as SGML, but it was much (much) simpler:
almost anyone could write it, so almost everyone did.
</p>
<p>
However, HTML only had a small vocabulary,
which users could not change or extend.
They could say, "This is a paragraph," or, "This is a table,"
but not, "This is a chemical formula," or, "This is a person's name."
Instead of adding thousands of new terms for different application domains,
a new standard for <em>defining</em> terms was created in 1998.
This standard was called the
<a href="glossary.html#xml">Extensible Markup Language</a> (XML);
it was much more complex than HTML,
but hundreds of specialized vocabularies have now been defined in terms of it,
such as the <a href="http://www.xml-cml.org/">Chemical Markup Language</a>
for describing chemical compounds and related concepts.
</p>
<p>
More recently,
a new version of HTML called HTML5 has been created.
Web programmers are very excited about it,
primarily because its new features allow them to create
sophisticated user interfaces that run on smart phones and tablets as well as conventional computers.
In what follows,
though,
we'll focus on some basics that haven't changed (much) in 20 years.
</p>
</div>
<h3>Key Points</h3>
<div id="s:web:history:keypoints" class="keypoints">
<ul>
<li>Structured data is much easier for machines to process than unstructured data.</li>
<li>Markup languages like HTML and XML can be used to add semantic information to text.</li>
</ul>
</div>
<h3>Challenges</h3>
<div id="s:web:history:challenges" class="challenges">
<p>FIXME</p>
</div>
</section>
<section id="s:web:formatting">
<h2>Formatting Rules</h2>
<h3>Objectives</h3>
<div id="s:web:formatting:objectives" class="objectives">
<ul>
<li>Explain the difference between text, elements, and tags.</li>
<li>Explain the difference between a model and a view, and correctly identify instances of each.</li>
<li>Write correctly-formatted HTML (using escape sequences for special characters).</li>
<li>Identify and fix improperly-nested HTML.</li>
</ul>
</div>
<h3>Lesson</h3>
<div id="s:web:formatting:lesson" class="lesson">
<p>
A basic HTML <a href="glossary.html#document">document</a>
contains <a href="glossary.html#text">text</a>
and <a href="glossary.html#element">elements</a>.
(The full specification allows for many other things
with names like "external entity references" and "processing instructions",
but we'll ignore them.)
The text in a document is just characters,
and as far as HTML is concerned,
it has no intrinsic meaning:
"Feynman" is just seven characters,
not a person.
</p>
<p>
Elements are <a href="glossary.html#metadata">metadata</a>
that describe the meaning of the document's content.
For example,
one element might signal a heading,
while another might indicate that something is a cross-reference.
</p>
<p>
Elements are written using <a href="glossary.html#tag-xml">tags</a>,
which must be enclosed in angle brackets <code><…></code>.
For example, <code><cite></code> is used to mark the start of a citation,
and <code></cite></code> is used to mark its end.
Elements must be properly nested:
if an element called <code>inner</code> begins inside an element called <code>outer</code>,
<code>inner</code> must end before <code>outer</code> ends.
This means that <code><outer>…<inner>…</inner></outer></code> is legal HTML,
but <code><outer>…<inner>…</outer></inner></code> is not.
</p>
<p>
Here are some commonly-used HTML tags:
</p>
<table>
<tr>
<th>Tag</th>
<th>Usage</th>
</tr>
<tr>
<td><code>html</code></td>
<td>Root element of entire HTML document.</td>
</tr>
<tr>
<td><code>body</code></td>
<td>Body of page (i.e., visible content).</td>
</tr>
<tr>
<td><code>h1</code></td>
<td>Top-level heading. Use <code>h2</code>, <code>h3</code>, etc. for second- and third-level headings.</td>
</tr>
<tr>
<td><code>p</code></td>
<td>Paragraph.</td>
</tr>
<tr>
<td><code>em</code></td>
<td>Emphasized text.</td>
</tr>
</table>
<p>
Finally,
every well-formed document started with a <code>DOCTYPE</code> declaration,
which looks like:
</p>
<pre>
<!DOCTYPE html>
</pre>
<p class="continue">
This tells programs what kind of elements are allowed to appear in the document:
'html' (by far the most common case),
'math' for MathML,
and so on.
Here is a simple HTML document that uses everything we've seen so far:
</p>
<pre>
<!DOCTYPE html><html><body><h1>Dimorphism</h1><p>Occurring or existing in two different <em>forms</em>.</p></body></html>
</pre>
<p>
A web browser like Firefox might present this document
as shown in <a href="#f:very_simple">Figure XXX</a>.
Other devices will display it differently.
A phone,
for example,
might use a different background color for the heading,
while a screen reader for people with visual disabilities
would read the text aloud.
</p>
<figure id="f:very_simple">
<img src="web/very_simple.png" alt="A Very Simple Web Page" />
<figcaption>Figure XXX: A Very Simple Web Page</figcaption>
</figure>
<p>
These different presentations are possible because
HTML separates content from presentation,
or in computer science jargon,
separates <a href="glossary.html#model">models</a> from <a href="glossary.html#view">views</a>.
The model is the data itself;
the view is how that data is displayed,
such as a particular pattern of pixels on our screen
or a particular sequence of sounds on our headphones.
A given model may be viewed in many different ways,
just as what files are on your hard drive
can be viewed as a list,
as snapshots,
or as a hierarchical tree
(<a href="#f:filesystem_views">Figure XXX</a>).
</p>
<figure id="f:filesystem_views">
<img src="web/filesystem_views.png" alt="Different Views of a File System" />
<figcaption>Figure XXX: Different Views of a File System</figcaption>
</figure>
<p>
People can construct models from views almost effortlessly—if you are able to read,
it's almost impossible <em>not</em> to see the letters "HTML"
in the following block of text:
</p>
<pre>
* * ***** * * *
* * * ** ** *
***** * * * * *
* * * * * *
* * * * * ****
</pre>
<p class="continue">
Computers,
on the other hand,
are very bad at reconstructing models from views.
In fact,
many of the things we do without apparent effort,
like understanding sentences,
are still open research problems in computer science.
That's why markup languages were invented:
they are how we explicitly specify the "what" that we infer so easily
for computers' benefit.
</p>
<p>
There are a couple of other formatting rules we need to know
in order to create and understand documents.
If we are writing HTML by hand
instead of using a <a href="glossary.html#wysiwyg">WYSIWYG</a> editor
like LibreOffice or Microsoft Word,
we might lay it out like this to make it easier to read:
</p>
<pre>
<!DOCTYPE html>
<html>
<span class="highlight"> </span><body>
<span class="highlight"> </span><h1>Dimorphism</h1>
<span class="highlight"> </span><p>Occurring or existing in two different <em>forms</em>.</p>
<span class="highlight"> </span></body>
</html>
</pre>
<p class="continue">
Doing this doesn't change how most browsers render the document,
since they usually ignore "extra" whitespace
(highlighted above).
As we'll see when we start writing programs of our own, though,
that whitespace doesn't magically disappear when a program reads the document.
</p>
<p>
Second,
we must use <a href="glossary.html#escape-sequence">escape sequences</a>
to represent the special characters <code><</code> and <code>></code>
for the same reason that we have to use <code>\"</code>
inside a double-quoted string in a program.
<span class="fixme">where do we explain escape sequences?</span>
In HTML and XML,
an escape sequence is an ampersand '&'
followed by the abbreviated name of the character
(such as 'amp' for "ampersand")
and a semi-colon.
The four most common escape sequences are:
</p>
<table>
<tr>
<th>Sequence</th>
<th>Character</th>
</tr>
<tr>
<td><code>&lt;</code></td>
<td><code><</code></td>
</tr>
<tr>
<td><code>&gt;</code></td>
<td><code>></code></td>
</tr>
<tr>
<td><code>&quot;</code></td>
<td><code>"</code></td>
</tr>
<tr>
<td><code>&amp;</code></td>
<td><code>&</code></td>
</tr>
</table>
<p>
One final formatting rule is that
every document must have a single <a href="glossary.html#root-element">root element</a>,
i.e., a single element must enclose everything else.
When combined with the rule that elements must be properly nested,
this means that every document can be thought of as a <a href="glossary.html#tree">tree</a>.
For example,
we could draw the logical structure of our little document
as shown in <a href="#f:very_simple_tree">Figure XXX</a>.
</p>
<figure id="f:very_simple_tree">
<img src="web/very_simple_tree.png" alt="Tree View of a Very Simple Web Page" />
<figcaption>Figure XXX: Tree View of a Very Simple Web Page</figcaption>
</figure>
<p>
A document like this, on the other hand, is not strictly legal:
</p>
<pre>
<h1>Dimorphism</h1>
<p>Occurring or existing in two different <em>forms</em>.</p>
</pre>
<p class="continue">
because it has two top-level elements
(the <code>h1</code> and the <code>p</code>).
Most browsers will render it correctly,
since they're designed to accommodate improperly-formatted HTML,
but most programs won't,
because they're not.
</p>
<div class="box" id="a:beautiful-soup">
<h3>Beautiful Soup</h3>
<p>
There are a lot of incorrectly-formatted HTML pages out there.
To deal with them,
people have written libraries like <a href="http://www.crummy.com/software/BeautifulSoup/">Beautiful Soup</a>,
which does its best to turn real-world HTML into something that
a run-of-the-mill program can handle.
It almost always gets things right,
but sticking to the standard makes life a lot easier for everyone.
</p>
</div>
</div>
<h3>Key Points</h3>
<div id="s:web:formatting:keypoints" class="keypoints">
<ul>
<li>HTML documents contain elements and text.</li>
<li>Elements are represented using tags.</li>
<li>Different devices may display HTML differently.</li>
<li>Every document must have a single root element.</li>
<li>Tags must be properly nested to form a tree.</li>
<li>Special characters must be written using escape sequences beginning with &.</li>
</ul>
</div>
<h3>Challenges</h3>
<div id="s:web:formatting:challenges" class="challenges">
<p>FIXME</p>
</div>
</section>
<section id="s:web:attributes">
<h2>Attributes</h2>
<h3>Objectives</h3>
<div id="s:web:attributes:objectives" class="objectives">
<ul>
<li>Explain what element attributes are, and what they are for.</li>
<li>Write HTML that uses attributes to alter a document's appearance.</li>
<li>Explain when to use attributes rather than nested elements.</li>
</ul>
</div>
<h3>Lesson</h3>
<div id="s:web:attributes:lesson" class="lesson">
<p>
Elements can be customized by giving them <a href="glossary.html#attribute">attributes</a>.
These are name/value pairs enclosed in the opening tag like this:
</p>
<pre>
<h1 align="center">A Centered Heading</h1>
</pre>
<p class="continue">
or:
</p>
<pre>
<p class="disclaimer">This planet provided as-is.</p>
</pre>
<p>
Any particular attribute name may appear at most once in any element,
just like keys may be present at most once in a <a href="setdict.html#s:dict">dictionary</a>,
so <code><p align="left" align="right">…</p></code> is illegal.
Attributes' values <em>must</em> be in quotes in XML and older dialects of HTML;
HTML5 allows single-word values to be unquoted,
but quoting is still recommended.
</p>
<p>
Another similarity between attributes and dictionaries is that
attributes are unordered.
They have to be <em>written</em> in some order,
just as the keys and values in a dictionary have to be displayed in some order when they are printed,
but as far as the rules of HTML are concerned,
the elements:
</p>
<pre>
<p align="center" class="disclaimer">This web page is made from 100% recycled pixels.</p>
</pre>
<p class="continue">
and:
</p>
<pre>
<p class="disclaimer" align="center">This web page is made from 100% recycled pixels.</p>
</pre>
<p class="continue">
mean the same thing.
</p>
<div class="box">
<h3>HTML and Version Control</h3>
<p class="fixme">explain</p>
</div>
<p>
When should we use attributes, and when should we nest elements?
As a general rule,
we should use attributes when:
</p>
<ul>
<li>
each value can occur at most once for any element;
</li>
<li>
the order of the values doesn't matter; and
</li>
<li>
those values have no internal structure,
i.e.,
we will never need to parse an attribute's value
in order to understand it.
</li>
</ul>
<p class="continue">
In all other cases, we should use nested elements.
However, many widely-used XML formats break these rules
in order to make it easier for people to write XML by hand.
For example,
in the Scalable Vector Graphics (SVG) format used to describe images as XML,
we would define a rectangle as follows:
</p>
<pre>
<rect width="300" height="100" style="fill:rgb(0,0,255); stroke-width:1; stroke:rgb(0,0,0)"/>
</pre>
<p class="continue">
In order to understand the <code>style</code> attribute,
a program has to somehow know to split it on semicolons,
and then to split each piece on colons.
This means that a generic program for reading XML
can't extract all the information that's in SVG,
which partly defeats the purpose of using XML in the first place.
</p>
</div>
<h3>Key Points</h3>
<div id="s:web:attributes:keypoints" class="keypoints">
<ul>
<li>Elements can be customized by adding key-value pairs called attributes.</li>
<li>An element's attributes must be unique, and are unordered.</li>
<li>Attribute values should not have any internal structure.</li>
</ul>
</div>
<h3>Challenges</h3>
<div id="s:web:attributes:challenges" class="challenges">
<p>FIXME</p>
</div>
</section>
<section id="s:web:morehtml">
<h2>More HTML</h2>
<h3>Objectives</h3>
<div id="s:web:morehtml:objectives" class="objectives">
<ul>
<li>Write correctly-formatted HTML pages containing lists, tables, images, and links.</li>
<li>Add correctly-formatted metadata to the head of an HTML page.</li>
</ul>
</div>
<h3>Lesson</h3>
<div id="s:web:morehtml:lesson" class="lesson">
<p>
As anyone who has surfed the web has seen,
web pages can contain a lot more than just headings and paragraphs.
To start with,
HTML provides two kinds of lists:
<code>ul</code> to mark an unordered (bulleted) list,
and <code>ol</code> for an ordered (numbered) one
(<a href="#f:nested_lists">Figure XXX</a>).
Items inside either kind of list must be wrapped in <code>li</code> elements:
</p>
<pre>
<!DOCTYPE html>
<html>
<body>
<ul>
<li>A. Binet
<ol>
<li>H. Ebbinghaus</li>
<li>W. Wundt</li>
</ol>
</li>
<li>C. S. Pierce
<ol>
<li>W. Wundt</li>
</ol>
</li>
</body>
</html>
</pre>
<figure id="f:nested_lists">
<img src="web/nested_lists.png" alt="Nested Lists"/>
<figcaption>Figure XXX: Nested Lists</figcaption>
</figure>
<p class="continue">
Note how elements are nested:
since the ordered lists "belong" to the unordered list items above them,
they are inside those items' <code><li>…</li></code> tags.
And remember,
the indentation used to make this list easier for people to read
means nothing to the computer:
we could put the whole thing on one line,
or write it as:
</p>
<pre>
<!DOCTYPE html>
<html>
<body>
<ul>
<li>A. Binet
<ol>
<li>H. Ebbinghaus</li>
<li>W. Wundt</li>
</ol>
</li>
<li>C. S. Pierce
<ol>
<li>W. Wundt</li>
</ol>
</li>
</body>
</html>
</pre>
<p class="continue">
and the computer would interpret and display it the same way.
A human being,
on the other hand,
would find the inconsistent indentation of the second layout
much harder to follow.
</p>
<p>
HTML also provides tables, but they are awkward to use:
tables are naturally two-dimensional,
but text is one-dimensional.
This is exactly like the problem of representing a two-dimensional array in memory,
which we saw in the <a href="numpy.html#s:storage">NumPy</a>
and <a href="dev.html#s:storage">development</a> lessons.
We solve it in the same way:
by writing down the rows,
and the columns within each row,
in a fixed order.
The <code>table</code> element marks the table itself;
within that,
each row is wrapped in <code>tr</code> (for "table row"),
and within those,
column items are wrapped in <code>th</code> (for "table heading")
or <code>td</code> (for "table data"):
</p>
<pre>
<!DOCTYPE html>
<html>
<body>
<table>
<tr>
<th></th>
<th>A. Binet</th>
<th>C. S. Pierce</th>
</tr>
<tr>
<th>H. Ebbinghaus</th>
<td>88%</td>
<td>NA</td>
</tr>
<tr>
<th>W. Wundt</th>
<td>29%</td>
<td>45%</td>
</tr>
</table>
</body>
</html>
</pre>
<figure id="f:simple_table">
<img src="web/simple_table.png" alt="A Simple Table" />
<figcaption>A Simple Table</figcaption>
</figure>
<div class="box">
<h3>Tables, Layout, and CSS</h3>
<p>
Tables are sometimes used to do multi-column layout,
as well as for tabular data,
but this is a bad idea.
To understand why,
consider two other HTML tags:
<code>i</code>, meaning "italics",
and <code>em</code>, meaning "emphasis".
The former directly controls how text is displayed,
but by doing so,
it breaks the separation between model and view that is the heart of markup's usefulness.
Without understanding the text that has been italicized,
a program cannot understand whether it is meant to indicate someone shouting,
the definition of a new term,
or the title of a book.
The <code>em</code> tag, on the other hand, has exactly one meaning,
and that meaning is different from the meaning of <code>dfn</code> (a definition)
or <code>cite</code> (a citation).
</p>
<p>
Conscientious authors use <a href="glossary.html#css">Cascading Style Sheets</a> (or CSS)
to describe how they want pages to appear,
and only use <code>table</code> elements for actual tables.
CSS is beyond the scope of this lesson,
but is described briefly in <a href="extras.html#s:web:css">the appendix</a>.
</p>
</div>
<p>
HTML pages can also contain images.
(In fact,
the World Wide Web didn't really take off until
the Mosaic browser allowed people to mix images with text.)
The word "contain" is misleading, though:
HTML documents can only contain text,
so we cannot store an image "in" a page.
Instead,
we must put it in some other file,
and insert a reference to that file in the HTML using the <code>img</code> tag.
Its <code>src</code> attribute specifies where to find the image file;
this can be a path to a file on the same host as the web page,
or a URL for something stored elsewhere.
For example,
when a browser displays this:
</p>
<pre>
<!DOCTYPE html>
<html>
<body>
<p>My daughter's first online chat:</p>
<img src="madeleine.jpg"/>
<p>but probably not her last.</p>
</body>
</html>
</pre>
<p class="continue">
it looks for the file <code>madeleine.jpg</code>
in the same directory as the HTML file:
</p>
<figure id="f:simple_image">
<img src="web/simple_image.png" alt="Simple Images" />
<figcaption>Figure XXX: Simple Images</figcaption>
</figure>
<p>
Notice,
by the way,
that the <code>img</code> element is written as
<code><img…/></code>,
i.e.,
with a trailing slash inside the <code><></code>
rather than with a separate closing tag.
This makes sense because the element doesn't contain any text:
the content is referred to by its <code>src</code> attribute.
Any element that doesn't contain anything
can be written using this short form.
</p>
<p>
Images don't have to be in the same directory as the pages that refer to them.
When the browser displays this:
</p>
<pre>
<!DOCTYPE html>
<html>
<body>
<p>Yes, she knows she's cute:</p>
<img src="img/cute-smile.jpg"/>
</body>
</html>
</pre>
<p class="continue">
it looks in the directory containing the page
for a sub-directory called <code>img</code>,
and loads the image file from there,
while if it's given:
</p>
<pre>
<!DOCTYPE html>
<html>
<body>
<img src="http://software-carpentry.org/img/software-carpentry-logo.png"/>
</body>
</html>
</pre>
<p class="continue">
it downloads the image from the URL
<code>http://software-carpentry.org/img/software-carpentry-logo.png</code>
and displays that.
</p>
<div class="box">
<h3>It's Always Interpreted</h3>
<p class="fixme">The path is <em>always</em> interpreted (web browser config)</p>
</div>
<p>
Whenever we refer to an image,
we should use the <code>img</code> tag's <code>alt</code> attribute
to provide a title or description of the image.
This is what screen readers for people with visual handicaps will say aloud to "display" the image;
it's also what search engines rely on,
since they can't "see" the image either.
Adding this to our previous example gives:
</p>
<pre>
<!DOCTYPE html>
<html>
<body>
<p>My daughter's first online chat:</p>
<img src="madeleine.jpg" <span class="highlight">alt="Madeleine's first online chat"</span>/>
<p>but probably not her last.</p>
</body>
</html>
</pre>
<p>
We can use URLs for images,
but their most important use is
to create the links within and between pages that make HTML "hypertext".
This is done using the <code>a</code> element.
Whatever is inside the element is displayed and highlighted for clicking;
this is usually a few words of text,
but it can be an entire paragraph or an image.
</p>
<p>
The <code>a</code> element's <code>href</code> attribute
specifies what the link is pointing at;
as with images,
this can be either a local filename or a URL.
For example,
we can create a listing of the examples we've written so far like this
(<a href="#f:simple_listing">Figure XXX</a>):
</p>
<pre>
<!DOCTYPE html>
<html>
<body>
<p>
Simple HTML examples for
<a href="http://software-carpentry.org">Software Carpentry</a>.
</p>
<ol>
<li><a href="very-simple.html">a very simple page</a></li>
<li><a href="hide-paragraph.html">hiding paragraphs</a></li>
<li><a href="nested-lists.html">nested lists</a></li>
<li><a href="simple-table.html">a simple table</a></li>
<li><a href="simple-image.html">a simple image</a></li>
</ol>
</body>
</html>
</pre>
<figure id="f:simple_listing">
<img src="web/simple_listing.png" alt="Using Hyperlinks" />
<figcaption>Figure XXX: Using Hyperlinks</figcaption>
</figure>
<p>
The hyperlink element is called <code>a</code> because
it can also used to create <a href="glossary.html#anchor">anchors</a> in documents
by giving them a <code>name</code> attribute instead of an <code>href</code>.
An anchor is simply a location in a document that can be linked to.
For example,
suppose we formatted the Feynman quotation given earlier like this:
</p>
<pre>
<blockquote>
As a by-product of this same view, I received a telephone call one day
at the graduate college at <a name="pu">Princeton</a>
from Professor Wheeler, in which he said,
"Feynman, I know why all electrons have the same charge and the same mass."
"Why?"
"Because, they are all the same electron!"
</blockquote>
</pre>
<p class="continue">
If this quotation was in a file called <code>quote.html</code>,
we could then create a hyperlink directly to the mention of Princeton
using <code><a href="quote.html#pu"></code>.
The <code>#</code> in the <code>href</code>'s value separates the path to the document
from the anchor we're linking to.
Inside <code>quote.html</code> itself,
we could link to that same location simply using
<code><a href="#pu"></code>.