‹ Kinyanjui Wangonya

Tags / tutorial


Say you had an unknown number of <div> elements and (for some reason) you wanted to count them and number each other them automatically - how would you do it? If you’re like me, your first thought would be to use some kind of Javascript to play around with the DOM. What if I told you (insert Morpheus voice here) you can do it with CSS?

If you’re just creating a package for learning purposes, there’s no need (IMO) to pollute the official PyPi index with it. Also, you may want to release a package to a select number of users for testing before you actually release it officially. For these purposes, you’re better off using TestPyPi:

a separate instance of the Python Package Index that allows you to try distribution tools and processes without affecting the real index.

It’s good practice to, as much as possible, write tests for your code. If you’re working with Python, pytest makes the process of writing and running tests much smoother. I wrote a few posts some time back on getting started with testing with pytest, so if you’re completely new to it, you might want to take a look at them:

For testing CLI apps, Click provides a convenient module: click.testing which has some useful functions (notably CliRunner()) to help us invoke commands and check their behavior.

We’ll go ahead and test each part of our app - creating, reading, updating and deleting.

For the next phase in this series, we’ll learn how to make a CLI app to perform CRUD operations, i.e, Creating, Retrieving, Updating and Deleting data. We’ll use Firebase to store our data.

Getting user input is an important part of any kind of application. Since we’ve already learned about options, adding a user prompt to our hello world app should be a breeze. All we need to do is add prompt=True to the option decorator, so that it prompts the user for input if no option is passed in.

Arguments work very similarly to options. If you’re familiar with functional programming, then you’re familiar with arguments. The concept is the same in Click.

In the previous post, we used setuptools to package our app and enable us to run it using a single executable command hello. In this post, we look at how to make CLI apps more functional and interactive using options.

In our previous post, we made a Hello World! app to get us started with Click. You might have noticed though that we needed to do python hello-world.py to run it. Since we’re building CLI apps, that’s just not cool and it’s not how CLI apps work anyway. We need to be able to run a single command, something like:

(venv) $ hello
Hello World!

That’s where setuptools comes in. Setuptools helps us bundle our script so we can install it and run it like an app ($ hello), instead of like a script ($ python hello-world.py).

Setuptools is a collection of enhancements to the Python distutils that allow developers to more easily build and distribute Python packages, especially ones that have dependencies on other packages. - Docs

In this post, we’ll cover the very basics of working with Python and Click to create a CLI app. We’ll install Python 3, set up our development environment, install Click, and create a hello-world app.

Python 3.x

Getting started with Python is easy. Most *nix systems come with Python pre-installed. We’ll be using Python 3. To check which version of Python you have, open up your terminal and run python -V. You should get an output similar to this:

Python 2.7.10

This means the default Python version installed on my computer is 2.7.10. Most Macs ship with Python 2 by default. If you got a 3.x.x, then you’re good to go. After installing Python 3, running python3 -V outputs:

Python 3.7.3

If you don’t have Python installed, you can head over to python.org, download the appropriate installer for your system, and run it. This step is necessary if you’re on Windows.

In this tutorial, we’ll create a simple todo api with Python 3 and Django 2.

« Older posts Newer posts »