Setup pystatis

You don’t need to do much to use pystatis. Basically, the first time you import the package, it will create a config.ini file under ~/.pystatis. This file is used for storing settings, for example your credentials fpr the supported databases.

To set up your credentials, we need to ask you for your username and password. This is done by the setup_credentials() function. It will ask you interactivly for the credentials, or you can set the following environmental variables:

  • PYSTATIS_GENESIS_API_USERNAME

  • PYSTATIS_GENESIS_API_PASSWORD

  • PYSTATIS_ZENSUS_API_USERNAME

  • PYSTATIS_ZENSUS_API_PASSWORD

  • PYSTATIS_REGIO_API_USERNAME

  • PYSTATIS_REGIO_API_PASSWORD

Note: Zensus 2022 supports a new authentication method using an API token. This token will be set as your username and the password remains blank so you can use the new token authentication method right away, just use the token as username and leave the password empty.

dotenv is uses here to load a local .env file that contains the above mentioned environmental variables so we don’t have to input them.

[ ]:
from pathlib import Path

from dotenv import load_dotenv

import pystatis

print("pystatis version: ", pystatis.__version__)
load_dotenv()
[2]:
# only execute if you want to delete your config file for test purposes
# pystatis.config.delete_config()

init_config is called when loading pystatis, so a config with empty credentials will be created in your user home directory by default.

The only thing you have to do is to set up your user credentials.

You can do so either by:

  1. specifying the 6 environment variables PYSTATIS_GENESIS_API_USERNAME|PASSWORD, PYSTATIS_ZENSUS_API_USERNAME|PASSWORD, and PYSTATIS_REGIO_API_USERNAME|PASSWORD

  2. calling the function setup_credentials() which will guide you through the process

Even if you do 1. please call setup_credentials() once as it will read out the environment variables and write the credentials to the config.ini in your config dir.

[3]:
pystatis.setup_credentials()  # also part of config module

Once you have set up your credentials, they are stored in the config.ini and in the config object of the config.py module. You don’t have to know this as regular user, this is more internal knowledge.

Warning: The following code will print out the content of your config.ini file with the credentials set, so please do not share or push this notebook with outputs enabled.

[ ]:
with open(Path.home() / ".pystatis" / "config.ini") as f:
    print(f.read())

The profile module allows you to change your password (change_password) and use the (unavailable) remove_result functionality, listed in the documentation under 2.7.2.

[ ]:
# change your password
pystatis.profile.change_password(
    db_name="genesis", new_password="DoNotUseThisAccidentally"
)

# use remove_result functionality
destatis_name_code = "42131-0001"
pystatis.profile.remove_result(name=destatis_name_code)