Skip to content

Instantly share code, notes, and snippets.

@kaustubhgupta
Created November 15, 2020 12:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaustubhgupta/ee36b0fb856b1536ded893ca9a10e539 to your computer and use it in GitHub Desktop.
Save kaustubhgupta/ee36b0fb856b1536ded893ca9a10e539 to your computer and use it in GitHub Desktop.
<body onload="brython()">
<h1>Get a joke</h1>
<button id="joke-btn">Get Joke</button>
<div id="joke" class="card">Click the "get joke" button</div>
<!-- Ajax call -->
<script type="text/python" id="script3">
from browser import document, ajax
url = 'https://api.chucknorris.io/jokes/random'
def on_complete(req):
import json
data = json.loads(req.responseText)
joke = data['value']
document['joke'].text = joke
def get_joke(e):
req = ajax.ajax()
req.open('GET', url, True)
req.bind('complete', on_complete)
document['joke'].text = 'Loading...'
req.send()
document['joke-btn'].bind('click', get_joke)
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment