Creating installer wizards with RKD¶
Wizard is a component designed to create comfortable installers, where user has to answer a few questions to get the task done.
- User answers questions invoked by
- At the end the
finish()is called, which acts as a commit, saves answers into
.rkd/tmp-wizard.jsonby default and into the
.envfile (depends on if to_env=true was specified)
- Next RKD task executed can read
.rkd/tmp-wizard.jsonlooking for answers, the answers placed in .env are already loaded automatically as part of standard mechanism of environment variables support
from rkd.core.api.inputoutput import Wizard # self is the TaskInterface instance, in Makefile.yaml it would be "this", in Python code it is "self" Wizard(self)\ .ask('Service name', attribute='service_name', regexp='([A-Za-z0-9_]+)', default='redis')\ .finish()
Service name [([A-Za-z0-9_]+)] [default: redis]: -> redis
Example of application that is using Wizard to ask interactive questions
Using Wizard results internally¶
Wizard is designed to keep the data on the disk, so you can access it in any other task executed, but this is not mandatory.
You can skip committing changes to disk by not using
finish() which is flushing data to json and to .env files.
wizard.answers to see all answers that would be put into json file, and
wizard.to_env to browse all environment variables that would be set in .env if
finish() would be used.
Example of loading stored values by other task¶
Wizard stores values into file and into .env file, so it can read it from file after it was stored there. This allows you to separate Wizard questions into one RKD task, and the rest of logic/steps into other RKD tasks.
from rkd.core.api.inputoutput import Wizard # ... assuming that previously the Wizard was completed by user and the finish() method was called ... wizard = Wizard(self) wizard.load_previously_stored_values() print(wizard.answers, wizard.to_env)