Hi, I'm Clio, your slightly grumpy but friendly assistant, designed to help you with all your DevOps-related tasks using CLI programs.
There are thousands of habit tracker apps, websites, etc, out there. But being a developer, what's fun about using something that you can build your self? And especially when you can do it with a new technology like GPTScript :-)
In this tutorial, we are going to begin by setting up everything we need, and then continue to building a simple habit tracker.
With this habit tracker, you will be able to tell a chat bot to create categories that fits your need, you can instruct it to track what you have done today, yesterday, etc. You can ask it to count how many times you have tracked a certain category, give you the current streak count, and even export the data and create an HTML file with a beautiful chart.
The data will be stored in a sqlite3 database between the sessions.
We can begin with setting up GPTScript on your computer. The version I used for this tutorial is version 0.9.2. If you have an older version, you can just reinstall GPTScript and you should be ready to go.
A short guide:
MacOS / Linux: brew install gptscript-ai/tap/gptscript Windows: winget install gptscript-ai.gptscript
Get your OpenAI API key by going here (https://platform.openai.com/api-keys)
Add the API key to your environment by running this command:
MacOS / Linux: export OPENAI_API_KEY="your-api-key” Windows: $env:OPENAI_API_KEY = 'your-api-key'
And now you should be ready to jump to the next step. If you need more details for the installation, you can go to the official getting started guide for GPTScript by clicking here (https://docs.gptscript.ai/getting-started).
Before you start running the commands for running GPTScript, it might be smart to take a look at the price lists here: https://openai.com/pricing
Using OpenAI’s API is not free, but you do not pay for more than you actually use.
The script we are building in this tutorial will use a language model called gpt-4-o. This price can vary a little bit based on what you actually are sending in to the GPTscript, how long the texts are, etc.
Take a good look at the price lists to familiarize your self with them.
Creating a GPTScript is really simple. You begin by creating a new file called "habittracker.gpt". All files ending with ".gpt" indicates that it's a GPTScript.
Add the following contents to it:
tools: sys.read, sys.write, sys.exec description: A chat bot for a personal habit tracker. chat: true You are an expert on creating good and lasting habits. If there isn't a sqlite database called 'habits.sqlite3' in this folder, you have to create one. The database should contain a table with a field for the name of the habit, the streak count, a tag or category, and a way to make sure you can only track a habit once per day. When the chat is started, please ask what habit you should track this time, or if you should create a new category or export the data. You should also be able to create a new habit category and remember this.
Let's go through this line by line before we continue.
Now that we have the first version of the script, let's go to the command line and run it. You run it using this command:
gptscript habittracker.gpt
When you have done so, you will see something similar to this:
You can then either type 1 or 2, or just write exactly what you want with your own words. The export functionality will come later in this tutorial.
Let's try adding a category:
Can you add a category for dringing water? You can call it "Daily water"
When you run the first command, you will get a question if you want to allow the script to run the commands. You can just type "a" and hit enter. After this command is finished executing, you can ask the script to list your categories. You can add as many categories as you like. Try to be a little bit specific when it comes to the names. Since this is AI, it might think that you want to call it something a little bit different than what you typed unless you specifically tell it to.
Can you show me all the categories
I have added one more category, and my screen now looks like this:
Since we now have a category or two, let's add some habit entries as well so we have data we can export later.
Can you track an entry for yesterday for drinking water?
Can you track an entry for today for drinking water?
This should now have added two entries to the database. Note that you can only track one category per day (At least that's what we told the script). If you try to add more than once per day, the script will give you a warning.
Open up the script in your editor again, and add the following content to the bottom of it:
I also want you to be able to create an html file where you show the categories and streaks in a chart, remember to pull the latest data from the database.
Before we test this, you need to exit the running script and start it again. When you have do that, you can start asking it to export the data.
When the script has started, you can either just type "3" or ask it in plain English to do what you want.
This takes a few seconds. First it will pull a fresh list of the data from the database, then it will create an HTML file.
If you open up this in your browser, you will see something similar to this:
The first time I did this, both of the columns was the same color. But now, the color has changed on one of them. This is a little bit of "magic" that can suddenly happen because the script understands that it makes it easier to separate from each other.
As you can see, I have one habit for drinking water and one for jobbing. Those are two habits I want to have long streaks for. There are probably a few more categories I should add though!
And that was it. You now have a simple habit tracker that can help you on your way to build good habits.
There are probably many more cool things you can add to this. For example: -Export the data to json -Hourly habits, weekly habits, etc -Ask the bot to give you compliments when you have tracked a habit
Stein Helset is a developer who creates tutorial videos to teach coding, with a focus on Django, Python, Vue, Htmx and much much more!