Created
July 24, 2019 06:30
-
-
Save kikocorreoso/ecf0c992006ffb600bf89683254f68bd to your computer and use it in GitHub Desktop.
Brython "AJAX" comments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html> | |
<head> | |
<title>Brython ajax example for comments</title> | |
<meta charset="UTF-8"> | |
<noscript>Please enable Javascript to view this page correctly.</noscript> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/brython/3.7.4/brython.min.js"></script> | |
<script type="text/python"> | |
from browser import document as doc, alert, html | |
comments = [ | |
{ | |
"parent": True, | |
"comment": "This is a parent comment" | |
}, | |
{ | |
"parent": True, | |
"comment": "This is a parent comment with a child" | |
}, | |
{ | |
"parent": False, | |
"comment": "This is a child comment with a child" | |
}, | |
] | |
def load_comments(ev): | |
# Perform an AJAX request and receive the JSON. In this case the JSON is already in the frontend to simplify... | |
for comment in comments: | |
render_comment(comment) | |
def render_comment(comment): | |
if comment["parent"]: | |
doc["comments"] <= html.DIV(comment["comment"]) | |
else: | |
doc["comments"] <= html.DIV(comment["comment"], style={"margin-left": "50px"}) | |
doc["load_comments"].bind("click", load_comments) | |
</script> | |
</head> | |
<body onload="brython(1)"> | |
<div id="content"> | |
<div id="post"> | |
This is the post. | |
</div> | |
<div id="comments"> | |
</div> | |
<button id="load_comments" type="button">Load comments</button> | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment