-
Notifications
You must be signed in to change notification settings - Fork 1
/
demo.html
1272 lines (1011 loc) · 42 KB
/
demo.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
<html>
<head>
<meta charset="utf-8">
<title>Brython demo</title>
<style>
body{
font-size: 14px;
font-family: Helvetica;
}
a{
color: #333;
}
a.menu{
text-decoration: none;
}
a.menu:hover{
background-color: #ccc;
}
a.selected{
font-weight: bold;
}
a:visited{
color: #333;
}
li{
list-style-type: none;
}
#panel{
display:table-cell;
width:65%;
padding-left:2%;
padding-right:2%;
text-align: left;
}
button{
background-color: #dadada;
color: #113;
border-style: solid;
border-color: #000;
border-width: 1px;
border-radius: 3px;
font-size: 1.2em;
}
button.btn-ls{
font-size: 1em;
}
div.example{
margin-top: 2em;
display: none;
}
pre{
display: inline-block;
padding: 1em;
font-family: Consolas, Courier New;
color: #ff9;
background-color: #114;
border-radius: 10px;
margin-top: 2em;
width: 66%;
}
pre.html{
padding: 0.5em;
}
.zone{
margin-left: 5em;
padding: 1em;
border-width: 1px;
border-color: #333;
border-style: solid;
width: 20em;
}
code{
font-family: Consolas, Courier New;
color: #113;
padding-left: 0.2em;
padding-right: 0.2em;
}
code.file{
background-color: #fff;
font-weigth: bold;
font-style: italic;
}
#moving{
position: relative;
left:140px;
top:10px;
font-size:16px;
padding: 3px;
background-color: var(--clear-2);
border-radius: 4px;
}
.up{
font-size: 1.2em;
}
.down{
font-size:0.8em;
}
/* colors for highlighted Python code */
span.python-string{
color: #6aa;
}
span.python-comment{
color: #6a6;
}
span.python-keyword{
color: #42ebf4;
}
span.python-builtin{
color: #0f0;
}
</style>
</head>
<body onload="brython({debug: 1, indexedDB: false})">
<h1 style="text-align: center">Introduction to <a href="http://brython.info" target="_blank" class="external">Brython</a></h1>
<div style="display:table; width:100%;">
<div style="display: table-row">
<div style="display: table-cell; width:20%; vertical-align: top;">
<ul id="menu">
</ul>
</div>
<div style="" id="panel">
<div id="home">
<p>This page shows a few examples of how you can interact with a web
page using Python as the scripting language : create new elements,
access and modify existing elements, create graphics, animations, send
Ajax requests, etc.
<p>The scripts use objects - elements shown in the page, styles
applied to these elements, events, etc. - that are described in the
Document Object Model (DOM), defined by the W3C. The reference used in
this page is the
<a href="https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model" target="_blank" class="external">Mozilla Developer Network</a>
(MDN) documentation.
<p>Although the DOM is by nature language-independant, most examples
in the MDN documentation are written with Javascript, not
(yet ;-) in Python. The translation is usually straightforward,
since Brython supports all the DOM types and their attributes and methods.
<p>In some cases, Brython adds a more Pythonic syntax : you will discover
them by browsing the examples.
<p>More documentation and examples can be found on the
<a href="http://brython.info" target="_blank" class="external">Brython site</a>.
</div>
</div>
</div>
</div>
<div class="example" id="ex0">
<button id="button_alert">display an alert box</button>
<script type="text/python" id="script0">
from browser import document, alert
def hello(ev):
alert("Hello !")
document["button_alert"].bind("click", hello)
</script>
<p>
<pre class="python"></pre>
<p><span class="comment">
<code>browser</code> is a Brython-specific module that defines the objects
used to interact with the page.
<p><code>document</code> is an object representing the HTML document ;
<code>document[element_id]</code> is the element with attribute id equal
to element_id. In this example, <code>document["button0"]</code> is a
reference to the button you click on.
<p><code>bind(<i>event, function</i>)</code> is a method of elements that
takes two arguments, the name of an event and the function to call when
the event occurs on the element. When the user clicks on the element, this
event is called "click". The code means : when the user clicks on the
element (here, the button with the text "display an alert box"), call the
function <code>hello</code>.
The function takes one argument, an object representing the event.
<p><code>alert</code> is used to display small popup windows.
</span>
</div>
<div class="example" id="ex_dialog">
<button id="button_dialog">display a popup window</button>
<script type="text/python" id="script_dailog">
from browser import document
from browser.widgets.dialog import InfoDialog
def hello(ev):
InfoDialog("Demo", "Hello world !", left=ev.x, top=ev.y)
document["button_dialog"].bind("click", hello)
</script>
<p>
<pre class="python"></pre>
<p><span class="comment">
Instead of the browser alert box, you can use a dialog box provided
by the built-in module <code class="module">browser.widgets.dialog</code>.
<p>Here, the box is positioned at the mouse position (given by the attributes
<code>x</code> and <code>y</code> of the event object) by keyword arguments
<code>left</code> and <code>top</code>.
</span>
</div>
<div class="example" id="ex1">
<button id="button1">change the text of an element</button>
<span id="zone1" class="zone">Initial content</span>
<script type="text/python" id="script1">
from browser import document
def change(event):
document["zone1"].textContent = "New content"
document["button1"].bind("click", change)
</script>
<p>
<pre class="python"></pre>
<p>
<span class="comment">
The attributes and methods of the elements are described in the Document
Object Model (DOM), which is abundantly documented (by
<a href="https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model" class="external">Mozilla</a>
for instance).
<p><code>textContent</code> is an attribute of
<a href="https://developer.mozilla.org/en-US/docs/Web/API/Node" target="_blank" class="external">Node</a> instances ; it is used to get or set an element's text content.
<p>Brython supports all the DOM methods and attributes ; for text content
it provides a shortcut, <code>text</code>, so that the function body can
also be written :
<pre class="python">
def change(event):
document["zone1"].text = "New Content"
</pre>
</code>
</span>
</div>
<div class="example" id="ex2">
<button id="button2">change the style of an element</button>
<span id="zone2" style="color:blue;background-color:#aad;" class="zone">coloured content</span>
<script type="text/python" id="script2">
from browser import document
element = document["zone2"]
def change(event):
style = element.style
color = style.color
style.color = "#cc8" if color == "blue" else "blue"
style.backgroundColor = "gray" if color == "blue" else "#aad"
style.fontWeight = "bold" if color == "blue" else "normal"
style.fontSize = "18px" if color == "blue" else "14px"
document["button2"].bind("click", change)
</script>
<p>
<pre class="python"></pre>
<p>
<span class="comment">
Elements have an attribute <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/style" target="_blank"><code>style</code></a>, which itself has (among many others) the attributes <code>color</code> (text color), <code>backgroundColor</code> (background color), <code>fontWeight</code> ("bold" or "normal"), <code>fontSize</code> (size of the letters, in pixels), etc.
<p>Look <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Reference" target="_blank" class="external">here</a> for a reference on CSS.
<p>Note that the attribute names that have a hyphen is CSS are written in camelCase in Python code : for instance, to set the CSS attribute <code>background-color</code>, the syntax is <code>style.backgroundColor</code>
</span>
</div>
<div class="example" id="ex_class">
<button id="button_class">change the class of an element</button>
<span id="zone_class" style="color:blue;background-color:#aad;" class="zone">a random text</span>
<script type="text/python" id="script_class">
from browser import document
element = document["zone_class"]
element.classList.add("down")
def change_class(event):
if "up" in element.classList:
element.classList.remove("up")
element.classList.add("down")
else:
element.classList.remove("down")
element.classList.add("up")
document["button_class"].bind("click", change_class)
</script>
<p>
<pre class="python"></pre>
<p>
<span class="comment">
Elements have an attribute <a href="https://developer.mozilla.org/en-US/docs/Web/API/Element/classList" target="_blank" class="external"><code>classList</code></a> with the values set in the attribute <code>class</code> of the element.
<p>It is an instance of the class <a href="https://developer.mozilla.org/en-US/docs/Web/API/DOMTokenList" target="_blank" class="external"><code>DOMTokenList</code></a>, which supports the methods <code>add()</code> and <code>remove()</code>.
</span>
</div>
<div class="example" id="ex3">
<button id="button3">hide or show an element</button>
<span id="zone3" class="zone">on / off</span>
<script type="text/python" id="script3">
from browser import bind, document
@bind("#button3", "click")
def change(event):
display = document["zone3"].style.display
document["zone3"].style.display = "inline" if display == "none" else "none"
</script>
<p>
<pre class="python"></pre>
<p>
<span class="comment">
<code>display</code> is another attribute of <code>style</code>. Setting it to <code>"none"</code> hides the element.
<p>Note that in this example, we use a variant of the syntax to bind
events to functions. Instead of the form we had seen before, which uses
the <i>method</i> <code>bind</code> of DOM elements, here we use the
<i>function</i> bind defined in module <code>browser</code>. It is used
as a decorator for the callback function, and takes two parameters:
<ul>
<li>- an identifier of the element(s) to bind : either a reference to the
element, or a <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors"
class="external">CSS selector</a> (here, the selector "#button3" refers to the
button)
<li>- the event name
</ul>
</span>
</div>
<div class="example" id="ex6_std">
<button id="button6_std">insert an element (standard DOM methods)</button>
<span id="zone6_std" class="zone">initial content</span>
<script type="text/python" id="script6_std">
from browser import document, html
element = document.getElementById("zone6_std")
nb = 0
def change(event):
global nb
elt = document.createElement("B")
txt = document.createTextNode(f" {nb}")
elt.appendChild(txt)
element.appendChild(elt)
nb += 1
document["button6_std"].addEventListener("click", change)
</script>
<p>
<pre class="python"></pre>
<p>
<span class="comment">This example uses the standard DOM methods to create elements (<a href="https://developer.mozilla.org/en-US/docs/Web/API/Document/createElement" target="_blank" class="external"><code>document.createElement()</code></a>, <a href="https://developer.mozilla.org/en-US/docs/Web/API/Document/createTextNode" target="_blank" class="external"><code>createTextNode()</code></a>) and append them to other elements or to the document (<a href="https://developer.mozilla.org/en-US/docs/Web/API/Node/appendChild" target="_blank"><code>Node.appendChild()</code></a>).
<p>Note that on the last line, we use the standard DOM method <a href="https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener" target="_blank" class="external"><code>EventTarget.addEventListener()</code></a> : the method <code>bind()</code> used in the other examples is Brython-specific.
<p>Also note the use of f-string for string formatting (<a href="https://www.python.org/dev/peps/pep-0498/" target="_blank" class="external">PEP 448</a>, introduced in Python 3.6) in the line
<pre class="python">
txt = document.createTextNode(f" {nb}")
</pre>
</span>
</div>
<div class="example" id="ex6">
<button id="button6">insert an element (Brython style)</button>
<span id="zone6" class="zone">initial content</span>
<script type="text/python" id="script6">
from browser import document, html
element = document["zone6"]
nb = 0
def change(event):
global nb
element <= html.B(f" {nb}")
nb += 1
document["button6"].bind("click", change)
</script>
<p>
<pre class="python"></pre>
<p>
<span class="comment">
To create DOM elements, Brython provides the module <code class="module">browser.html</code>. It defines classes with the name of all the valid HTML tags in uppercase ; <code>html.B("message")</code> creates the element <code class="html"><B>message</B></code>
<p>To include an element inside another one, Brython uses the operator <code><=</code> : think of it as a left arrow, not as "less than or equal". The use of an operator is more concise and avoids having to use a function call.
</code>
</span>
</div>
<div class="example" id="ex61">
<button id="button61">insert before an element</button>
<div id="zone61" class="zone"><ul><li>element</ul></div>
<script type="text/python" id="script61">
from browser import document, html
ul = document["zone61"].get(selector="ul")[0]
element = ul.get(selector="li")[0]
nb = 0
def change(event):
global nb
nb += 1
ul.insertBefore(html.LI(f"element before {nb}"), element)
document["button61"].bind("click", change)
</script>
<p>
<pre class="python"></pre>
<p>
<span class="comment">
We use the standard DOM method <a href="https://developer.mozilla.org/en-US/docs/Web/API/Node/insertBefore" class="external">insertBefore()</a>
</code>
</span>
</div>
<div class="example" id="ex62">
<button id="button62">insert after an element</button>
<div id="zone62" class="zone"><ul><li>element</ul></div>
<script type="text/python" id="script62">
from browser import document, html
ul = document["zone62"].get(selector="ul")[0]
element = ul.get(selector="li")[0]
nb = 0
def change(event):
global nb
nb += 1
ul.insertBefore(html.LI(f"element after {nb}"), element.nextSibling)
document["button62"].bind("click", change)
</script>
<p>
<pre class="python"></pre>
<p>
<span class="comment">
We use the standard DOM method <a href="https://developer.mozilla.org/en-US/docs/Web/API/Node/insertBefore" class="external">insertBefore()</a>
and insert the element after <a href="https://developer.mozilla.org/en-US/docs/Web/API/Node/nextSibling" class="external">element.nextSibling</a>.
If the element is the last in its parent's children, <code>nextSibling</code> is <code>null</code> so the new element is inserted at
the end.
</code>
</span>
</div>
<div class="example" id="ex_elt_attr">
<button id="button_elt_attr">insert an element with attributes</button>
<span id="zone_elt_attr" class="zone">initial content</span>
<script type="text/python" id="script_elt_attr">
from browser import document, html
element = document["zone_elt_attr"]
def insert(event):
element <= html.SPAN("new text",
style=dict(color="red", paddingLeft="1em"),
Class="down")
document["button_elt_attr"].bind("click", insert)
</script>
<p>
<pre class="python"></pre>
<p>
<span class="comment">
The attributes of a tag are defined as keyword arguments of the html classes constructor.
<p>Note that the style attribute is passed as a dictionary ; also note that, since <code>class</code> is a Python keyword, it can't be used as a key, so we use <code>Class</code> instead (tag attributes are case-insensitive).
</code>
</span>
</div>
<div class="example" id="ex7">
<button id="button7">insert an HTML table</button>
<p>
<div id="zone7" class="zone" style="width:60%"></div>
<script type="text/python" id="script7">
from browser import document
from browser.html import TABLE, TR, TH, TD
def insert_table(event):
table = TABLE()
# header row
table <= TR(TH(f"Column {i}") for i in range(5))
# table rows
for row in range(3):
table <= TR(TD(f"Cell {row}-{i}") for i in range(5))
document["zone7"].clear()
document["zone7"] <= table
document["button7"].bind("click", insert_table)
</script>
<p>
<pre class="python"></pre>
<p>
<span class="comment">
To build a table, we use the HTML tags <code>TABLE, TR, TH</code> and <code>TD</code>.
<p>The first attribute passed to the classes defined in <code class="module">browser.html</code> is either a string, another instance of a class, <i>or an iterator on such elements</i>.
<p>Here we use a Python generator expression to include several headers (<code>TH</code>) in the first row (<code>TR</code>) of the table, and the same for the table cells (<code>TD</code>) in the next rows.
<p><code>clear()</code> is a method that removes all the element contents.
</code>
</span>
</div>
<div class="example" id="ex12">
<button id="button12">insert a dropdown menu</button>
<span id="zone12" class="zone"></span>
<script type="text/python" id="script12">
from browser import document, html
from browser.widgets.dialog import InfoDialog
def show(event):
dropdown = event.target
num = dropdown.selectedIndex
InfoDialog("Demo", "Selected: {}".format(dropdown.options[num].value))
def insert_dropdown(event):
document["zone12"] <= "Your choice : "
dropdown = html.SELECT(html.OPTION(f"Choice {i}") for i in range(5))
dropdown.bind("change", show)
document["zone12"] <= dropdown
document["button12"].bind("click", insert_dropdown)
</script>
<p>
<pre class="python"></pre>
<p>
<span class="comment">
We build the dropdown menu by a SELECT element, using a generator expression with the OPTION elements displayed in the menu.
<p>When the user changes the selected option in the menu, the event "change" is triggered on the SELECT box. This is done in the line
<p>
<pre class="python">
dropdown.bind("change", show)
</pre>
<p>
<p>In function <code>show(event)</code>, <code>event.target</code> is the element itself.
<p>A <a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement" target="_blank">SELECT element</a> has an attribute <a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement/selectedIndex" target="_blank"><code>selectedIndex</code></a>, the rank of the selected item in the list of options, available by its attribute <code>options</code>. The OPTION element itself has an attribute <code>value</code>, here the option text.
</span>
</div>
<div class="example" id="ex8">
<button id="button8">draw in a canvas</button>
<canvas id="zone8" width="200" height="50"
style="border-color:#000;border-style:solid;border-width:1px;margin-left:5em;"></canvas>
<script type="text/python" id="script8">
from browser import document, html
import math
canvas = document["zone8"]
ctx = canvas.getContext("2d")
x = 20
def draw(event):
global x
ctx.beginPath()
ctx.arc(x, 25, 15, 0, 2 * math.pi)
x += 15
ctx.stroke()
document["button8"].bind("click", draw)
</script>
<p>
<pre class="python"></pre>
<p>
<span class="comment">
We start by importing the module <code class="module">math</code>, the same as in the Python standard distribution.
<p><a href="https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API" target="_blank">Canvas</a> is an API for drawing geometric forms in the page. The examples on the MDN pages are written in Javascript but can be easily translated for Brython.
</span>
</div>
<div class="example" id="ex9">
<button id="button9">insert an image</button>
<p><div id="zone9" class="zone" style="height:60px;"></div>
<script type="text/python" id="script9">
from browser import document, html
logo = "python-logo.png"
def insert_image(event):
document["zone9"].clear()
document["zone9"] <= html.IMG(src=logo, height=50)
document["button9"].bind("click", insert_image)
</script>
</div>
<div class="example" id="ex16">
<button id="button16">get the value of form fields</button>
<p><div id="zone16" class="zone"></div>
<p><div style="width:30%; padding-left:3em; background-color:#ddd;">
<p><input id="input16" autocomplete="off" value="test">
<br><select id="select16" autocomplete="off">
<option value="one">one
<option value="two">two
<option value="three">three
</select>
<br>Checkbox <input type="checkbox" id="check16">
<br>
<textarea id="textarea16" rows=3 cols=30 autocomplete="off">your content here !...</textarea>
</div>
<script type="text/python" id="script16">
from browser import document, html
def show_values(event):
input = document["input16"].value
select = document["select16"]
option = select.options[select.selectedIndex].value
checked = document["check16"].checked
text = document["textarea16"].value
document["zone16"].clear()
document["zone16"] <= (f"Value in INPUT field: {input}",
html.BR(), f"Selected option: {option}",
html.BR(), f"Checkbox checked ? {checked}",
html.BR(), f"Value in TEXTAREA field: {text}"
)
document["button16"].bind("click", show_values)
</script>
<p>
<pre class="python"></pre>
<p>
</div>
<div class="example" id="ex13">
<button id="button13">select elements by CSS selectors</button>
<span id="zone13" class="zone"></span>
<script type="text/python" id="script13">
from browser import document, html
def change(event):
# clear the zone
document["zone13"].clear()
# get a list of all BUTTON elements
buttons = document.select("button")
document["zone13"] <= "On this page there are {} buttons ".format(len(buttons))
# get a list of all tags with class "zone"
zones = document.select(".zone")
document["zone13"] <= "and {} elements with class 'zone'".format(len(zones))
document["button13"].bind("click", change)
</script>
<p>
<pre class="python"></pre>
<p>
<span class="comment">
We have seen how to get an element by its id : <code>document[element_id]</code>. Here, <code>document["zone13"]</code> is the bordered box at the right of the button.
<p>The method <code>select(css_selector)</code> provides a way to select elements based on the <a href="http://www.w3schools.com/cssref/css_selectors.asp" target="_blank">CSS selector syntax</a>. Passing the name of a tag returns a list of all the elements with this tag ; passing <code>.zone</code> returns a list of all the elements whose attribute <code>class</code> is set to <code>"zone"</code>.
</span>
</div>
<div class="example" id="ex14">
<button id="button14">rotate an element</button>
<span id="zone14" class="zone">
<button style="background-color: red; width: 1.5em;" id="rot14"> </button>
</span>
<script type="text/python" id="script14">
from browser import document, html
moving = document["rot14"]
angle = 10
def change(event):
global angle
moving.style.transform = f"rotate({angle}deg)"
angle += 10
document["button14"].bind("click", change)
</script>
<p>
<pre class="python"></pre>
<p>
<span class="comment">
<code>transform</code> is another attribute of <code>style</code> that can make changes to the element, including a rotation by a specified angle.
</span>
</div>
<div class="example" id="ex15">
<button id="button15">animate an element</button>
<p>
<div id="zone15" style="padding:0;" class="zone">
<button style="background-color: #fff; border-width: 0px; color:#000;padding:0;font-size:24px;height:auto;" id="rot15">►</button>
</div>
<script type="text/python" id="script15">
from browser import document, window
moving = document["rot15"]
x = 0
dx = 3
run = None
def change(event):
global run
if run is None:
# start animation
animloop(1)
else:
# stop animation
window.cancelAnimationFrame(run)
run = None
document["button15"].bind("click", change)
def render():
global x, dx
moving.style.transform = "translate({}px,0)".format(x)
x += dx
if x > document["zone15"].offsetWidth-moving.offsetWidth:
dx = -dx
moving.html = "◄" # left triangle
elif x <= 0:
dx = -dx
moving.html = "►" # right triangle
def animloop(t):
global run
run = window.requestAnimationFrame(animloop)
render()
</script>
<p>
<pre class="python"></pre>
<p>
<span class="comment">
The browser window provides 2 functions to start and stop an animation : <code>requestAnimationFrame</code> and <code>cancelAnimationFrame</code>.
<p>When the user clicks on the button, the animation is started by calling the function <code>animloop</code> with an arbitrary argument. <code>window.requestAnimationFrame</code>, called with the function itself as argument, returns a reference to the animation. The drawing function, <code>render()</code> is called ; the loop is then executed repeatedly under the control of the animation loop.
<p>In <code>render()</code>, to put the element at a specific location, we use another <code>transform</code> function, <code>translate(x, y)</code>. The moving element bounces, and its shape changes, when its position reaches the box borders.
<p>If the user clicks the button again, calling <code>window.cancelAnimationFrame</code> on the animation object stops the animation.
</span>
</div>
<div class="example" id="ex_mousemove">
<button id="button_mousemove" style="border-width:0;background-color:inherit;">
move an element with the mouse
</button>
<b id="moving" style="top:10px; left: 140px;">drag me
</b>
<script type="text/python" id="script_mousemove">
from browser import document
class ElementMove:
def __init__(self, moving):
"""Make "moving" element movable with the mouse"""
self.moving = moving
self.is_moving = False
self.moving.bind("mousedown", self.start)
self.moving.bind("mouseup", self.stop)
moving.style.cursor = "move"
def start(self, event):
"""When user clicks on the moving element, set boolean is_moving
to True and store mouse and moving element positions"""
self.is_moving = True
self.mouse_pos = [event.x, event.y]
self.elt_pos = [self.moving.left, self.moving.top]
document.bind("mousemove", self.move)
# prevent default behaviour to avoid selecting the moving element
event.preventDefault()
def move(self, event):
"""User moves the mouse"""
if not self.is_moving:
return
# set new moving element coordinates
self.moving.left = self.elt_pos[0] + event.x - self.mouse_pos[0]
self.moving.top = self.elt_pos[1] + event.y - self.mouse_pos[1]
def stop(self, event):
"""When user releases the mouse button, stop moving the element"""
self.is_moving = False
document.unbind("mousemove")
ElementMove(document["moving"])
</script>
<p>
<pre class="python"></pre>
<p>
<span class="comment">
This example uses other mouse events than "click" : "mousedown", "mousemove" and "mouseup".
<p>The mouse position is available by the attributes <code>x</code>, <code>y</code> of the event object.
<p>The position of an element can be changed by defining its attributes <code>left</code> and <code>top</code>.
</span>
</div>
<div class="example" id="ex_ls">
<button id="button_ls">use local storage</button>
<p><div id="zone_ls" style="padding-left:1em;"></div>
<script type="text/python" id="script_ls">
from browser import document, window
from browser.html import TABLE, TR, TD, INPUT, BUTTON
zone = document["zone_ls"]
"""Test if the browser supports local storage"""
try:
storage = window.localStorage
storage.setItem("x", "x")
storage.removeItem("x")
except:
storage = None
def action(ev):
"""User clicked on "add" or "remove" button"""
button = ev.target
row = button.closest("TR")
if button.text == "remove":
key = row.get(selector="TD")[0].text
storage.removeItem(key)
else:
key, value = [x.value for x in row.get(selector="INPUT")]
if key.strip():
storage.setItem(key, value)
# refresh table
show()
def update_value(ev):
"""If a value field has been modified, update storage"""
row = ev.target.closest("TR")
key = row.get(selector="TD")[0].text
value = row.get(selector="INPUT")[0].value
storage.setItem(key, value)
def show(*args):
"""Shows the data stored locally, add buttons to add / remove items"""
zone.clear()
if storage is None:
zone <= "No local storage for this browser"
return
table = TABLE()
for i in range(storage.length):
key = storage.key(i)
value = storage.getItem(key)
value_field = INPUT(value=value)
value_field.bind("change", update_value)
table <= TR(TD(key) + TD(value_field)+
TD(BUTTON("remove", Class="btn-ls")))
table <= TR(TD(INPUT()) + TD(INPUT()) +
TD(BUTTON("add", Class="btn-ls")))
zone <= table
for button in document["zone_ls"].get(selector="button"):
button.bind("click", action)
document["button_ls"].bind("click", show)
</script>
<p>
<pre class="python"></pre>
<p>
<span class="comment">
The <a href="https://developer.mozilla.org/en-US/docs/Web/API/Storage" target="_blank">Storage</a> API allows local storage of key/value pairs.
<p>Notice the use of method <code>elt.closest(tagName)</code> to get the first DOM element with the specified tag name above <code>elt</code>.
<p>The event "change" on the INPUT field is triggered when the field loses focus, if the value has been modified.
</span>
</div>
<div class="example" id="ex10">
<button id="button10">send an Ajax request</button>
<p><div id="zone10" class="zone" style="padding-left:1em;"></div>
<script type="text/python" id="script10">
from browser import document, ajax
url = "http://api.open-notify.org/iss-now.json"
msg = "Position of the International Space Station at {}: {}"
def complete(request):
import datetime
data = request.json
position = data["iss_position"]
ts = data["timestamp"]
now = datetime.datetime.fromtimestamp(ts)
document["zone10"].text = msg.format(now, position)
def click(event):
ajax.get(url, oncomplete=complete, mode="json")
document["zone10"].text = "waiting..."
document["button10"].bind("click", click)
</script>
<p>
<i>Warning: this demo only works if this page (demo.html) is served in
http, not https.</i>
<pre class="python"></pre>
<p>
<span class="comment">
The module <code class="module">browser.ajax</code> allows sending Ajax calls, ie sending HTTP requests to a URL and handle the reply without having to reload the page.
<p>Here we use a public API that gives the current position of the International Space Station. The callback function <code>complete()</code> is called when the Ajax call has completed ; its argument has an attribute <code>responseText</code>, the response sent by the server. In this case, the API tells us that it's a JSON string. We decode it with the module <code class="module">json</code> of the standard distribution.
</span>
</div>
<div class="example" id="ex50">
<button id="button50">select, read and save a local file</button>
<script type="text/python" id="script50">
from browser import bind, window, document
load_btn = document["rtfile1"]
save_btn = document["save_file"]
@bind(load_btn, "input")
def file_read(ev):
def onload(event):