index.html
This file
Path
examples/celery/src/task_app/templates/index.html
108 lines · 2.8 KiB · HTML
Last changed by
Not known.
History of this fileTake it
The walk stopped after 200 commits: lines older than 73739a2 point at it.
Merge branch '2.2.x'David Lordbefore adoption73739a2
lines 1–108why, one step back1<!doctype html>2<html>3<head>4 <meta charset=UTF-8>5 <title>Celery Example</title>6</head>7<body>8<h2>Celery Example</h2>9Execute background tasks with Celery. Submits tasks and shows results using JavaScript.1011<hr>12<h4>Add</h4>13<p>Start a task to add two numbers, then poll for the result.14<form id=add method=post action="{{ url_for("tasks.add") }}">15 <label>A <input type=number name=a value=4></label><br>16 <label>B <input type=number name=b value=2></label><br>17 <input type=submit>18</form>19<p>Result: <span id=add-result></span></p>2021<hr>22<h4>Block</h4>23<p>Start a task that takes 5 seconds. However, the response will return immediately.24<form id=block method=post action="{{ url_for("tasks.block") }}">25 <input type=submit>26</form>27<p id=block-result></p>2829<hr>30<h4>Process</h4>31<p>Start a task that counts, waiting one second each time, showing progress.32<form id=process method=post action="{{ url_for("tasks.process") }}">33 <label>Total <input type=number name=total value="10"></label><br>34 <input type=submit>35</form>36<p id=process-result></p>3738<script>39 const taskForm = (formName, doPoll, report) => {40 document.forms[formName].addEventListener("submit", (event) => {41 event.preventDefault()42 fetch(event.target.action, {43 method: "POST",44 body: new FormData(event.target)45 })46 .then(response => response.json())47 .then(data => {48 report(null)4950 const poll = () => {51 fetch(`/tasks/result/${data["result_id"]}`)52 .then(response => response.json())53 .then(data => {54 report(data)5556 if (!data["ready"]) {57 setTimeout(poll, 500)58 } else if (!data["successful"]) {59 console.error(formName, data)60 }61 })62 }6364 if (doPoll) {65 poll()66 }67 })68 })69 }7071 taskForm("add", true, data => {72 const el = document.getElementById("add-result")7374 if (data === null) {75 el.innerText = "submitted"76 } else if (!data["ready"]) {77 el.innerText = "waiting"78 } else if (!data["successful"]) {79 el.innerText = "error, check console"80 } else {81 el.innerText = data["value"]82 }83 })8485 taskForm("block", false, data => {86 document.getElementById("block-result").innerText = (87 "request finished, check celery log to see task finish in 5 seconds"88 )89 })9091 taskForm("process", true, data => {92 const el = document.getElementById("process-result")9394 if (data === null) {95 el.innerText = "submitted"96 } else if (!data["ready"]) {97 el.innerText = `${data["value"]["current"]} / ${data["value"]["total"]}`98 } else if (!data["successful"]) {99 el.innerText = "error, check console"100 } else {101 el.innerText = "✅ done"102 }103 console.log(data)104 })105106</script>107</body>108</html>