Skip to content

Instantly share code, notes, and snippets.

@kikocorreoso
Created July 24, 2019 06:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kikocorreoso/ecf0c992006ffb600bf89683254f68bd to your computer and use it in GitHub Desktop.
Save kikocorreoso/ecf0c992006ffb600bf89683254f68bd to your computer and use it in GitHub Desktop.
Brython "AJAX" comments
<!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