Created
June 5, 2021 08:55
-
-
Save anandtripathi5/e28b1f80a910acf941fd54aa6422e84d to your computer and use it in GitHub Desktop.
Render multiple 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'] | |
def get_multiple_images(number): | |
return [get_xkcd_image() for _ in range(number)] | |
@app.get('/comic') | |
def hello(): | |
start = time.perf_counter() | |
urls = get_multiple_images(5) | |
end = time.perf_counter() | |
markup = f"Time taken: {end-start}<br><br>" | |
for url in urls: | |
markup += f'<img src="{url}"></img><br><br>' | |
return markup | |
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