Skip to content

Instantly share code, notes, and snippets.

@anandtripathi5
Last active June 5, 2021 08:42
Show Gist options
  • Save anandtripathi5/4241c228ce970c4662b66b1b0bb9517e to your computer and use it in GitHub Desktop.
Save anandtripathi5/4241c228ce970c4662b66b1b0bb9517e to your computer and use it in GitHub Desktop.
Render random xkcd comic in flask application
import time
from random import randint
import requests as requests
from flask import Flask
app = Flask(__name__)
def get_xkcd_image():
random = randint(0, 300)
response = requests.get(f'http://xkcd.com/{random}/info.0.json')
return response.json()['img']
@app.get('/comic')
def hello():
start = time.perf_counter()
url = get_xkcd_image()
end = time.perf_counter()
return f"""
Time taken: {end-start}<br><br>
<img src="{url}"></img>
"""
if __name__ == '__main__':
app.run(debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment