|
| 1 | +<!DOCTYPE html > |
| 2 | +<html> |
| 3 | + <head> |
| 4 | + <meta charset="utf-8"/> |
| 5 | + <link |
| 6 | + rel="stylesheet" |
| 7 | + href="https://cdnjs.cloudflare.com/ajax/libs/pure/2.0.3/pure-min.min.css" |
| 8 | + integrity="sha256-jYujp4Kf07YDuUF9i1MHo4AnpXUKuHxIUXH7CrHxdKw=" |
| 9 | + crossorigin="anonymous" /> |
| 10 | + <script |
| 11 | + src="https://cdnjs.cloudflare.com/ajax/libs/brython/3.8.9/brython.min.js" defer> |
| 12 | + </script> |
| 13 | + <script |
| 14 | + src="https://cdnjs.cloudflare.com/ajax/libs/brython/3.8.9/brython_stdlib.min.js" defer> |
| 15 | + </script> |
| 16 | + <style> |
| 17 | + body { padding: 30px; } |
| 18 | + aside { |
| 19 | + background: lightyellow; |
| 20 | + margin: 1em 0; |
| 21 | + padding: .3em 1em; |
| 22 | + border-radius: 3px; |
| 23 | + border: 1px solid gray; |
| 24 | + color: gray; |
| 25 | + } |
| 26 | + </style> |
| 27 | + </head> |
| 28 | + <body onload="brython(1)"> |
| 29 | + <h2>Ajax Requests</h2> |
| 30 | + <aside><p>Demonstrate the usage of GET using <tt>browser.ajax</tt>. |
| 31 | + You need to start a server for this example to work. |
| 32 | + You can start the Python development web server with the following command: |
| 33 | + <tt>python3 -m http.server</tt>. |
| 34 | + </p></aside> |
| 35 | + <form class="pure-form" onsubmit="return false;"> |
| 36 | + <legend>Actions</legend> |
| 37 | + <button id="get-btn" class="pure-button pure-button-primary">Async Get</button> |
| 38 | + <button id="get-blocking-btn" class="pure-button pure-button-primary">Blocking Get</button> |
| 39 | + <button id="clear-logs-btn" class="pure-button">Clear Logs</button> |
| 40 | + <legend>Logs</legend> |
| 41 | + <textarea id="log" name="log" rows="20" cols="60"></textarea> |
| 42 | + </form> |
| 43 | + <script type="text/python"> |
| 44 | +from browser import ajax, document |
| 45 | +import javascript |
| 46 | + |
| 47 | +def counter(): |
| 48 | + x = 1 |
| 49 | + while True: |
| 50 | + yield x |
| 51 | + x += 1 |
| 52 | + |
| 53 | +cnt = counter() |
| 54 | + |
| 55 | +def on_complete(req): |
| 56 | + if req.status == 200: |
| 57 | + log(f"Text received: '{req.text}'") |
| 58 | + elif req.status == 0: |
| 59 | + error = f"Error: Did you start a web server (ex: 'python3 -m http.server')?" |
| 60 | + log(error) |
| 61 | + else: |
| 62 | + error = f"Error: {req.status} - {req.text}" |
| 63 | + |
| 64 | +def log(message): |
| 65 | + document["log"].value += f"{next(cnt):03}: {message} \n" |
| 66 | + |
| 67 | +def add_log_sep(): |
| 68 | + log_text = document["log"].value |
| 69 | + if not log_text: |
| 70 | + return |
| 71 | + else: |
| 72 | + document["log"].value += "======================================\n" |
| 73 | + |
| 74 | +def clear_logs(evt): |
| 75 | + global cnt |
| 76 | + cnt = counter() # Reset counter |
| 77 | + document["log"].value = "" |
| 78 | + |
| 79 | +def ajax_get(evt): |
| 80 | + add_log_sep() |
| 81 | + log("Before async get") |
| 82 | + ajax.get("/api.txt", oncomplete=on_complete) |
| 83 | + log("After async get") |
| 84 | + |
| 85 | +def ajax_get_blocking(evt): |
| 86 | + add_log_sep() |
| 87 | + log("Before blocking get") |
| 88 | + try: |
| 89 | + ajax.get("/api.txt", blocking=True, oncomplete=on_complete) |
| 90 | + except Exception as exc: |
| 91 | + error = f"Error: {exc.__name__} - Did you start a web server (ex: 'python3 -m http.server')?" |
| 92 | + log(error) |
| 93 | + else: |
| 94 | + log("After blocking get") |
| 95 | + |
| 96 | +document["get-btn"].bind("click", ajax_get) |
| 97 | +document["get-blocking-btn"].bind("click", ajax_get_blocking) |
| 98 | +document["clear-logs-btn"].bind("click", clear_logs) |
| 99 | + </script> |
| 100 | + </body> |
| 101 | + |
| 102 | +</html> |
0 commit comments