{"id":353,"date":"2022-07-30T00:00:00","date_gmt":"2022-07-30T00:00:00","guid":{"rendered":"https:\/\/tac.debuzzify.com\/?p=353"},"modified":"2023-06-20T02:51:28","modified_gmt":"2023-06-20T02:51:28","slug":"toml-config-files","status":"publish","type":"post","link":"https:\/\/www.the-analytics.club\/toml-config-files\/","title":{"rendered":"A Brief Guide To Manage Configurations Using TOML Files"},"content":{"rendered":"\n

How’d you change the behavior of a software project based on a set of parameters?<\/p>\n\n\n\n

You could use environment variables. But what if you want complex structures in the parameters?<\/p>\n\n\n\n

Can you use JSON? Yes, you can. JSON files don’t allow comments. How’d you describe your parameters to readers?<\/p>\n\n\n\n

The answer is to use a TOML configuration file.<\/p>\n\n\n\n\n\n

Why should we use a config file, and why TOML?<\/h2>\n\n\n\n

Config files are great ways to extract project parameters. When you share your code with others, they know exactly where to make changes to tweak the behavior of the software.<\/p>\n\n\n\n

For instance, say yours is a website codebase that supports several themes. Others who set up your code can go to the config file and change the theme variable to a different one. It’s more convenient than going to the codebase and editing the theme in various files.<\/p>\n\n\n\n

But to let your users know what themes are available, you need to have comments on the config files. Your website supports ‘dark,’ ‘light,’ ‘pop,’ and ‘grayscale’ themes. JSON files fall short, as they don’t allow comments.<\/p>\n\n\n\n

It’s a misconception that environment files are config files<\/b>. They are not.<\/p>\n\n\n\n

While it’s also possible to store values in the environment file<\/a>, its purpose is to hide secrets from other developers.<\/p>\n\n\n\n

Say you have database credentials and API keys. Publishing your code with these values to a public cloud like GitHub can be harmful. Hence we use env variables to separate them.<\/p>\n\n\n\n

Related: <\/b>11 Advantages of Cloud Databases Over On-Premise Databases.<\/i><\/b><\/a><\/p>\n\n\n\n

Config files are to store values that you can happily share with other developers. The themes option we discussed is a good example.<\/p>\n\n\n\n

Why TOML?<\/p>\n\n\n\n

TOML is an excellent option because it’s both super straightforward and the most popular platforms accept it.\u00a0<\/p>\n\n\n\n

For instance, the popular headless CMS platform Netlify uses TOML files<\/a> to upload build configurations. We can configure the node environment, the build command, the output directory, etc., in this config file. <\/p>\n\n\n\n

Another good example is the GitLab Runner. Gitlab runner<\/a> is part of the Gitlab CI\/CD pipeline. It allows you to run jobs in a pipeline during continuous deployment. How do you configure it? You use a TOML file. <\/p>\n\n\n\n

In a TOML file, you can tell the runner how many concurrent jobs to execute, the log level, the listening port, and many other options. <\/p>\n\n\n\n

TOML also files also have syntax highlighting support from many code editors. For instance, on VSCode, I’ve installed the “Even Better TOML” extension.<\/p>\n\n\n\n

\u00a0<\/p>\n\n\n

\n
\"TOML
TOML syntax highlighting in VSCode.<\/figcaption><\/figure><\/div>\n\n\n

TOML file is a great feature of a modern Python project structure<\/a>.<\/p>\n\n\n\n

What is a TOML file?<\/h2>\n\n\n\n

TOML (Tom’s Obvious Minimal Language) is a configuration file format that is easy to read and write. It’s minimal, and even people with no programming experience can easily understand it.<\/p>\n\n\n\n

TOML supports many data structures, such as key-value pairs, arrays, and tables. Parser libraries can convert them to native data structures in different programming languages.<\/p>\n\n\n\n

Most mainstream programming languages can parse it. As of this post, over 40 languages have TOML parsers<\/a>. This list includes JavaScript, Java, C#, PHP, and C++.<\/p>\n\n\n\n

This post focuses on TOML file usage in Python.<\/p>\n\n\n\n

A TOML file can live anywhere in a project. The ideal location is the project’s root folder because we want to edit the configuration for the project. If we only care about a module, we can move it to a subfolder.<\/p>\n\n\n\n

TOML files have an extension, .toml. Here’s an example config file.<\/p>\n\n\n\n