Persistent configuration

Both static and interactive plots produced through DataMapPlots can be customized through many settings. When such customizations become more numerous and pervasive through one’s usage, copy-pasting the settings in every invocation of create_plot and friends can become cumbersome. The solution to this problem is to write these common custom settings up in the DataMapPlot configuration file.

When DataMapPlot is first imported in any Python code, it creates this configuration file in the “most appropriate place” given one’s computing platform.

[1]:
import datamapplot
[2]:
from datamapplot.config import ConfigManager
cfg = ConfigManager()
[3]:
from pathlib import Path
print(ConfigManager()._config_file.relative_to(Path.home()))
.config/datamapplot/config.json

The default configurationm file includes minimal settings that echo our users’ typical purposes for bending away from the default parameter values of the main DataMapPlot routines.

[4]:
print(cfg._config_file.read_text())
{
  "dpi": 100,
  "figsize": [
    10,
    10
  ],
  "cdn_url": "unpkg.com",
  "font_family": "Roboto"
}

Most parameters that carry a default value for create_plot, create_interactive_plot and other interface routines can be set by editing this JSON file. Yet, some of the most basic plot ingredients cannot be set through this file. An attempt to do so will raise a ConfigError exception when creating a plot.

Setting persistent configuration through Python code

ConfigManager is a singleton class whose indexing getting and setting work just like a dictionary. Thus, custom settings can be set through Python code.

[5]:
print(cfg["dpi"])
100
[6]:
cfg["font_family"] = "Roboto"

These configuration settings can be made to persist between sessions by saving to the configuration file.

[7]:
cfg.save()