Advanced configuration
Taipy provides three ways to configure your application:
- A Python code configuration
- An explicit file configuration using TOML format
- A file configuration provided through an environment variable
Example
Here is a typical workflow that shows how to adapt the configuration of a Taipy application at each stage of a software project (from initial development to production).
-
As a developer, I will design my application by configuring all the new entities (data nodes, tasks, scenarios) using the Python code configuration with just a few attributes configured. The default configuration is used for the other attributes.
-
Then, I test the application I just built. At this step, I need my application to have a more realistic behavior, like real data. For that, I need more configuration. I can specify for my specific input dataset what file to use. I am using the Python code configuration for that.
-
Once I am satisfied with my application running on my local dev environment, I will deploy it to a remote environment for testing. It is a dedicated environment made for testing deployment and integration testing. I can then use an explicit TOML file configuration. I can now easily update the file when needed to debug efficiently without changing the code directly.
-
Once step 3 is completed, I want to deploy a released and tagged version of my application across several remote environments (e.g., pre-production and production). I create one TOML file per remote environment with a few values that differ from step 3. For each environment, I set the environment variable TAIPY_CONFIG_PATH (see below) to point to the right TOML configuration file.
These methods are described below.
Python code configuration¶
The configuration (for all your data nodes, tasks, scenarios, etc.) can be defined directly using Python
code. This configuration can be done using methods from the Config
class. This Python configuration is meant to
be used during the application development phase. It overrides the default configuration:
the default configuration applies if some values are not provided.
Design of the application to configure
Let's imagine we want to configure an application corresponding to the design described in the picture. We use the following Python configuration code.
Below is the Python code corresponding to the design above.
my_config.py | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
|
The train
, predict
, and plan
methods used in lines 22, 24, and 28 are the user functions imported in line
2 from the module my_functions
that represents a user Python module.
The following module "my_function" is imported in the Python configuration.
my_functions.py | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
Studio
You can use Taipy Studio for your Python code configuration. Taipy Studio generates a TOML file that you can load using the following code:
The config.toml file contains the TOML generated by Studio equivalent to the previous Python code configuration.
1 2 3 |
|
The following file is the TOML version of the Python code configuration.
config.toml | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
|
Note that the type of the non-string configuration attributes is specified in the TOML file by adding at the end of the value (':bool', ':int', or ':float').
Security
Note that in line 15 of the Python code, and in line 37 of the TOML export, the password is not exposed. The property's value is a template referencing the environment variable PWD that contains the value. It must be exported as follows:
export PWD=my_pwd
Override using explicit TOML file¶
Taipy also provides file configuration. Indeed, a TOML file can be explicitly provided by the developer to the Taipy application using Python coding such as:
1 2 3 |
|
This file configuration overrides the attributes in the code configuration (and the default configuration). Here is an example of a TOML file overriding the code configuration provided above as an example:
folder/config.toml | |
---|---|
1 2 3 4 5 6 7 |
|
Two behaviors occur if the previous TOML file is used as file configuration. First, the Taipy application now has
five workers (By default, the number of workers is 2). Then, the sales_history data node now is a CSV data node
pointing to the file ./path/to/my/file.csv
. All other configuration fields remain unchanged.
Override with file in env variable¶
Finally, if the environment variable TAIPY_CONFIG_PATH
is defined with the path of a TOML config, Taipy will
automatically load the file and override the previous configurations (explicit file configuration, code
configuration, and default configuration).
This functionality can be handy for changing the configuration of a Taipy application without having to restart it.
Attribute in an env variable¶
Configuration can be set dynamically using environment variables with the syntax ENV[MY_VARIABLE]
. This syntax can
be used both in Python configuration or TOML configuration. At runtime, Taipy will search for MY_VARIABLE
among the
environment variables and then use it.
It can be used to set a different configuration field value depending on the application's environment. It is also especially useful if you want to use secret strings such as host names, usernames or passwords. For example, you can hide a password from the configuration file using an environment variable.
Let's take an example with two environment variables. One string password and one integer value. You can export the
PWD
and NB_USERS
variables with the following command lines
1 2 |
|
and refer to it with the following Taipy configuration:
1 2 3 |
|
1 2 3 |
|
Note that if the type of the configuration attribute is not a string, it must be specified in the TOML file (':bool', ':int', ':float').
Loading and exporting configuration¶
Taipy provides some APIs to export and load its configurations.
Load¶
Note
Changed in version 2.1. Now it only replaces the Python code configuration.
Load a configuration file and replace the Python code configuration.
1 2 3 |
|
Export¶
Note
Changed in version 2.1. Now it only exports the Python code configuration.
Export the Python code configuration:
1 2 3 |
|
Backup¶
Note
New in version 2.1.
Backup the applied configuration:
1 2 3 |
|
Restore¶
Note
New in version 2.1.
Load a configuration file and replace the applied configuration.
1 2 3 |
|
Override¶
Note
New in version 2.1.
Load a configuration file, replace the current file configuration and triggers a re-computation of the applied configuration.
1 2 3 |
|