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.
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:
After the core functionality was done, I started to iron out some bugs by adding try/except blocks for expected errors:
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!
Do you want to get 250+ concise and applicable Python tips in an ebook that will cost you less than 10 bucks (future updates included), check it out here.
"The discussions are succinct yet thorough enough to give you a solid grasp of the particular problem. I just wish I would have had this book when I started learning Python." - Daniel H
"Bob and Julian are the masters at aggregating these small snippets of code that can really make certain aspects of coding easier." - Jesse B
"This is now my favourite first Python go-to reference." - Anthony L
"Do you ever go on one of those cooking websites for a recipe and have to scroll for what feels like an eternity to get to the ingredients and the 4 steps the recipe actually takes? This is the opposite of that." - Sergio S