PyDev of the Week: Miguel Grinberg

This week we welcome Miguel Grinberg (@miguelgrinberg) as our PyDev of the Week! Miguel is the author of Flask Web Development and the very popular Flask Mega-Tutorial. You can find out more about Miguel by checking out his blog or his Github profile. Let’s spend some time getting to know Miguel better!

Can you tell us a little about yourself (hobbies, education, etc):

I was born in Buenos Aires, Argentina. Shortly after graduating from college with a Masters degree in Computer Science I was lucky to be offered a job in the United States, so I relocated to Portland, Oregon with my wife. We raised a family there and lived happily for several years. In 2018 we relocated once again, this time to Ireland. We plan to spend a few years on this side of the pond to be closer to my wife’s family and to be able to travel through Europe, but Portland is still our home and I’m pretty sure we will eventually return to America.

In terms of hobbies I have to say that by all standards I’m a fairly boring person. Outside of coding (which I do professionally and also as a hobby), what I enjoy the most is playing the Ukulele. I have a small collection of them, and I have recently expanded it with a Mandolin, which seemed appropriate now that I’m in Ireland. Everyone here seems to be in a band of some sort, so maybe one day I’ll join one as well, who knows!

Why did you start using Python?

This was around 2008 or 2009, I think. I was working at a company in which my team maintained a large library written in C++ that was used by several products, both internal and from partners. This was a big company, with a proper Quality Assurance department, but the QA engineers complained that they did not have an easy way to test our library, since it was C++ code. We had a homegrown unit testing suite written in a combination of bash, make, C++ and diff that was painful to maintain, and that was it in terms of testing. So I came up with the idea of creating bindings for our library in a scripting language that our QA people felt comfortable using. After a survey, the two contenders were Python and Ruby. At the time I knew very little about Python, and I had some knowledge of Ruby, so funny enough my personal choice would have been Ruby. But as it happens, one of the engineers in my team was actually very experienced in Python from a previous job, so strategically we thought it would be to our advantage to go with Python because we had an expert in the team. So I have to thank my teammate for getting me into Python!

I always approach the learning of new things through personal projects, so as soon as the decision to go with Python was made I started to play with the language at home just for fun. A few years after my initial introduction to the language I was thinking in starting a software blog and was having trouble finding a blogging platform that I liked, so I’ve got the idea of writing my own blog. By then the Python bindings we created at work were a success and Python had won me over 100%. So I naturally decided to use a Python web framework to make my blog, and looking through the available options I finally selected this minimalistic framework that at the time wasn’t that popular, called Flask. That turned out to be one of the best decisions I’ve made in my life.

What other programming languages do you know and which is your favorite?

I have a fair amount of experience with C and C++. I often work with JavaScript, HTML and CSS, since they are unavoidable if you do any sort of web development work. I learned to code back when I was a teenager hacking away on my TI-99/4A Home Computer, and in those days Basic was pretty much the only option to write code in. I remember I was frustrated back then because I was writing video games in Basic and they were pretty slow, so I taught myself Assembly to write faster games, as this was the only other available programming language on the TI computer. I have also done some projects here and there in other languages such as Ruby, PHP, Smalltalk and Java. So it’s a little bit of everything for me.

The language that allows me to get things done faster and without creating an unmaintainable code mess is hands down Python, so given a new project this is always my first choice. Python is also the language that is the most fun to use for me.

What projects are you working on now?

The open source project that takes most of my time these days is my Socket.IO client and server, which is split in two repositories, python-socketio and python-engineio. Socket.IO is a standard communication protocol built on top of HTTP and WebSocket that allows clients and servers to establish long lived bi-directional connections, avoiding some of the limitations of HTTP, and also with very low latency. My project allows Python to act as a client or as a server and connect to Socket.IO applications built in other languages, or of course, you can also use Python on both ends if that’s what you like. My Socket.IO server is commonly paired with the JavaScript client running on web browsers to build highly dynamic applications that are an alternative to the more traditional option of building the server as a REST or HTTP API.

I’ve mentioned above that at some point was trying to get into blogging. It’s now been more than seven years since I started to blog and I still find it very rewarding, so the blog is always high on the list of current projects. I recently finished a six part tutorial for MicroPython, and I’m about to publish the second part of an in-depth series on Python decorators.

Which Python libraries are your favorite (core or 3rd party)?

Two libraries that I wish were more popular because they are fantastic are gevent and eventlet. These are small frameworks that allow you to write asynchronous applications, pretty much like you would with Python’s own asyncio. The difference is that gevent and eventlet do not require a new syntax for asynchronous code, so you can get code that was written for regular Python to magically work asynchronously. Packages such as Requests or even Flask become asynchronous without any changes. I gave a talk about using these frameworks alongside Flask at PyCon UK 2019.

Another package that I like a lot is pudb, a console based GUI debugger that you can run on a remote server through an SSH connection. I also like arrow as a nicer date and time library than Python’s datetime package. Last but not least, httpie is a great HTTP client that runs from the CLI with a much cleaner syntax than curl or wget.

How did you get started with MicroPython?

When I moved to Ireland last year I rented a house that had a last generation heating controller, one of those that you can control from a smartphone app or the browser. My issue with this controller was that the thermostat could not be programmed, the only thing that was programmable was to start and stop the heating at the given times. Since the weather outside is constantly changing, the schedule required constant adjusting, and that was a tedious task. What I would have preferred is to program a target temperature for the day and another one for the night, and then have the controller figure out when to start and stop the heating so that my desired temperatures are reached, but this device, and most others I’ve seen here in Ireland, do not have that capability for some reason. So the idea came to write a little thermostat app and load it into a microcontroller connected to a thermometer running MicroPython. I’ve used a ESP8266 board for this project, a nice and cheap microcontroller board that comes with Wi-Fi and has a vast array of supported sensors, including a few for temperature. I built two of these devices, one for downstairs and one for upstairs. The MicroPython boards would then read the temperature every 5 minutes and compare it against my chosen temperature for the current time of day. The script would then decide on the best course of action to reduce the difference between the current and target temperatures. The action could be one of three options: start the heating, stop the heating, or keep the heating in its current state. Controlling the heating from MicroPython was actually the easiest part, it just required sending a couple of HTTP requests. It was a super fun project to work on!

What was the motivation behind the Flask Mega-Tutorial?

The Flask Mega-Tutorial was actually my first writing task for the blog. Once I completed my blog project using Python and Flask and had the blog deployed, I had to find a first topic to write about. At the time Flask was starting to be noticed, but wasn’t in most people’s radars yet. For example, there were no books about it at all. So the most logical thing seemed to start blogging about my experience writing the blog itself with Flask. I called it the “Mega” tutorial because I knew I wanted to cover a lot of different topics, all sourced from my experience in getting the blog project going. I started writing articles without doing so much planning, to be honest. Every 2-3 weeks I would decide on a topic and write an article for the series on it, plus fully working code to go with it. I finally decided to end the tutorial after 18 chapters, about a year after I started it.

I never liked that improvised nature of the original tutorial, but regardless, a lot of people liked it and it became very popular, enough for an O’Reilly editor to notice it and eventually give me the chance to write an Animal book on the Flask framework, now in its second edition. The years passed, and I kept feeling that the original Flask Mega-Tutorial could have been better organized. About five years after I started it, I decided to give it a major makeover, sponsored by a Kickstarter campaign. I feel much better about the new version of the tutorial, now expanded to 23 chapters and with a more thought out structure. I have also shot video versions of all the chapters, where I go through the tutorial step by step, and this was a lot of fun.

Do you have any words of wisdom for other aspiring bloggers / content creators?

This sounds like a cheesy bit of advice, but I think the most important thing you can do is to start writing right away! It’s easy enough to create a free blog, so there is no reason to delay it. What should you write about? You should write about what you know. I’ve found that for me, the easiest way to get going was to write step by step tutorials. You may find that this works for you as well, or maybe it comes easier to you to write in a different style, or maybe to record a video instead. The important thing is that you get used to produce content a little bit every day, because then without even realizing it you will be getting better at it over time. If you think you are not at expert at anything so there’s no topic you can talk about, then you should know that most bloggers (me included) are not experts at what they write about. It actually works the other way around, you become more experienced on a topic after you go through the effort of explaining it in detail to others. If you are concerned about making mistakes in your writing due to inexperience, you should know that blog posts can be edited after published, so as soon as you learn of a mistake you’ve made you can correct it easily. I constantly revise, edit and improve my old posts, often as a result of someone pointing out mistakes or inconsistencies I’ve made. No shame in that. So if you want to become a blogger or content creator you really have no excuse. You should start now!

Is there anything else you’d like to say?

I’d like to encourage everyone to work on personal projects. The things that I do on the side of my day job have a special place in my heart, because unlike work, where there are a lot of things outside of my control, when I embark on a personal project I can control every aspect of it and make it 100% to my liking. I have heard often the argument that if you are working 40+ hours a week the last thing you want is to give yourself more work for your nights and weekends. I think the people who think this way are choosing the wrong side projects! Your personal projects should be all about enjoyment and fun, if they feel too much like work then you are doing it wrong.

If I convinced you that spending some time on personal projects can be a good thing, then I hope you’ll agree that showing those projects off is also important. The interaction with the community will give you feedback and new ideas. You will likely become a role model for others who will decide to also embark on their own projects after seeing yours. You may also notice that over time, what you do at your day job will be positively affected by what you do on your own time, often in little ways, but potentially in big ways as well. A software project that I built on my own time while I was a college student in Argentina was key in getting that job offer to relocate to the United States, for example.

Thanks for doing the interview, Miguel!