Last active
June 5, 2021 08:42
-
-
Save anandtripathi5/4241c228ce970c4662b66b1b0bb9517e to your computer and use it in GitHub Desktop.
Render random xkcd comic in flask application
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
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