Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5

How to use robodk.roboapps.AppSettings

#1
Clearly, I don't understand how to use the AppSettings object.  The documentation says that it can load and save parameters to the station.  My hope was that any script in the station could use this to read/write parameters.  This sounds really useful.  I made up this little script - assuming that the values would not exist when it was run the first time, but the values would be there the second time it was run:

Code:
from robodk import robolink
from robodk import robomath
from robodk import roboapps


# RoboDK reference
RDK = robolink.Robolink()

print('start')

settings = roboapps.AppSettings('MySettings')

# No idea why this does not print anything...
print(type(settings))

# Load settings from the active station
settings.Load()

print(settings.getAttribs())

# Change/Add some settings with their values
settings.forecast = 'cloudy'
settings.color = 'blue'
settings.temperature = 70

print(settings.getAttribs())

# Save the settings back to the station
settings.Save()

print('end')

When this script runs, the first list of attributes (after loading) is always empty and the saving doesn't seem to do what I expect it to.  No matter how many times it runs the list of attributes is always empty after loading.

Settings Second Run.png   


Yes, I did look at the examples on github - the AppTemplate add-in.  I don't understand why a script like OptionCheckable.py has to import from AppSettings.py if the settings are stored in the station.  If I run the AppSetting.py script from the top menu bar and change the values like this it seems to remember then the second time is it run.

   

Hoping to learn something, I changed the OptionCheckable.py script so that it ~should~ display the value from the dialog box above.  It is no longer setting the parameters, just reading them.

Code:
def ActionChecked():
    """Action to perform when the action is checked in RoboDK."""
    RDK = robolink.Robolink()
    S = Settings()
    S.Load(RDK)
    #RDK.setParam(S.APP_OPTION_KEY, 1.0)
    #ShowMessage(RDK, ACTION_NAME, str(RDK.getParam(S.APP_OPTION_KEY)), False)
    ShowMessage(RDK, "Testing", str(RDK.getParam(S.APP_OPTION_KEY)), False)

def ActionUnchecked():
    """Action to perform when the action is unchecked in RoboDK."""

    RDK = robolink.Robolink()
    S = Settings()
    S.Load(RDK)

    #RDK.setParam(S.APP_OPTION_KEY, 0.0)
    ShowMessage(RDK, ACTION_NAME, str(RDK.getParam(S.APP_OPTION_KEY)), False)

But when I run OptionCheckable (from the top menu) it seems to be ignoring the values from the settings dialog and looks like this:
   

So I am even more confused.  Why is the OptionCheckable script not getting the values from the AppSettings script?  Is there a place where it is forcing that setting into a boolean?

Like I mentioned in the first sentence, I am missing some knowledge.

Thanks for your time.


Attached Files Thumbnail(s)
Settings First Run.png   
#2
You can find more information on the AppSettings class in our documentation:
https://robodk.com/doc/en/PythonAPI/robo...ppSettings

The roboapps.AppSettings class is a utility to save and retrieve custom parameters with a dynamic GUI for editing.
It is meant to be use in pair with RoboDK Apps, as an easy way to add parameters to your scripts.
The parameters are loaded from and saved to the currently active station associated with your Robolink instance.
Make sure this station was saved on disk at least once, as "New station" might not work.
The AppSettings class does not automatically load the data. When you instantiate it, you need to load from the station. Otherwise, you will get the default parameters.
The "settings_param" in the constructor is the entry used to store the data in the station. You need to consistently use the same name if you override it. You can subclass AppSettings instead.
Lastly, the settings are defined and loaded as class attributes. Meaning that after your load from station, the AppSettings attributes will be set with the stored data and can be accessed as-is 

The AppTemplate Add-in is an example of a RoboDK App, that includes an example of roboapps.AppSettings.
The OptionCheckable.py in it uses the AppSettings, but it is not mandatory. You can use checkable options without AppSettings. The example uses RDK.setParam/getParam to store a value in the station parameters. The key used to store them is defined in AppSettings, and can be changed.

Here's an example workflow:
  1. Define your AppSettings with default values (of the right type).
  2. Have the user open the settings, edit the values, and save them.
  3. In your script, instantiate the class (careful with settings_param) and Load the current settings.
  4. Use the AppSettings instance to access attributes directly.
  5. No need to save, unless you progamatically changed settings.

I think you are confusing RDK.getParam() / RDK.setParam() in the AppTemplate checkable example with the AppSettings: https://robodk.com/doc/en/PythonAPI/robo...k.setParam
Please read the Forum Guidelines before posting!
Find useful information about RoboDK by visiting our Online Documentation.
#3
Thank you for the explanation Sam.  

The RDK.setParam() and RDF.getParam() seems like the best solution for me.  It is easy to store values in the station, they are saved with the station, and all the the "Python things" seem to have access to them.

In the attached file, I created a simple example for the above (which works as expected) and also an example using the AppSettings approach (which does not work).  I just don't get it.   The Add_to_settings script creates a class instance and sets a couple attributes.  This should be the most minimal example that assigns values to settings, correct?  This has no errors.

The Get_from_settings script imports the class from the Add_to_settings script, creates an instance of it, loads the settings, but they are not there.  Shouldn't any class that inherits from roboapps.AppSettings have access to the same settings as long as the 'settings_param' is the same?  So it shouldn't have to import the Settings class from the first script, it should be able to define its own simple class.  Neither approach worked for me.

Could you please adjust the script so it is a minimal working example?

Getting


Attached Files
.rdk   Settings Test Project.rdk (Size: 1.13 KB / Downloads: 229)
  




Users browsing this thread:
1 Guest(s)