In this guest post Dante tells us about his Wisdom of the Ages app he built for PyBites code challenge 16 (reviewed here). The best way to learn Python is to build something and we are proud of our community achieving just that.
Query Your Favorite API Challenge
Only recently I started following PyBites, but I really liked the idea of the code challenges, so when I saw the opportunity to put some of my knowledge in practice, I took it without thinking twice.
As I like to challenge myself and up the bets, I decided I was going to query one API and then query another one using somehow the results of the first. Since I had some experience querying the Wikipedia API, I chose it then googled up for free APIs on the internet to use. I came by an API to get random inspirational quotes, and the idea struck me:
A simple website that displays a random inspirational quote, with its author's biography as an addendum. Wikipedia could provide those biographies!
I quickly sketched the app using the well-known 'flask' and 'requests' modules, with a simple jinja2 template to display three variables:
- The quote,
- the quote's author, and
- the quote's author's biography.
After the core functionality was done, I started to iron out some bugs by adding try/except blocks for expected errors:
- the quotes API sometimes returned malformed JSON,
- I couldn't grab a biography for an anonymous author.
Making it pretty
Done with the 'insides' of my machine, I wanted to make it look pretty on the outside too, so I used my google-fu to search for bootstrap templates and themes, found one I liked (Amoeba) and after cutting down all the things I didn't need and downloading a favicon, my first iteration of Wisdom of the Ages was finished:
But I wanted more! Recently I had heard about a Python module called pywebview, which displays a barebones browser as an application to the user. I figured I could make the user of my script choose between the 'embedded' simple view of the website flask created and her own browser, but eventually dropped the functionality because it made everything more complicated (I even had to use threading to run code after starting the flask app website) with no real benefit.
Finally, after some back-and-forth with PyBites (thanks for the feedback, again!), I refactored the code for readability, made it more pythonic, added an "another quote" button that refreshes the website on click and last but not least, prepared the app to display an 'Internal server error' if the quotes API is down or the user has no internet connection.
I really enjoyed this experience and I'm looking forward to more challenges! Peer-reviewing code is a joy I don't usually get to experience. Thanks to the folks at PyBites and have fun with your coding!
Keep Calm and Code in Python!
-- Dante Septem
Dante describes himself as "a Pythonista in the making, digital magician and complete fool". You can follow him on Github