Orange Forum • View topic - dependent combo box

dependent combo box

A place to ask questions about methods in Orange and how they are used and other general support.

dependent combo box

Postby ragrawal » Thu Mar 21, 2013 16:54

Hi,

I am building a hive reader that will bring the data from hive table. I have three combo box: one for schema, second for database, and third for table name. The last two combobox items depends on the prior selections i.e. the content of the database will depend on the select made in the first combo box and similarly the table names will depend on the database selected. All this is working fine.

But I am not able to save and reload the schema. After I call loadSettings, although the first combobox is properly set, the second is not because its missing items. Is there any example that I can look at it. Below is some psuedo code I currently have

Code: Select all
"""
<name>Hive Reader</name>
<description>Load data from Hive</description>
<contact>ragrawal@gmail.com</contact>
<priority>10</priority>
"""

import sys
import uuid
import Orange
from OWWidget import *
import OWGUI
import OWGUIEx

SOURCES = ['prodhive', 'testhive']

class OWDseStorage(OWWidget):
    settingsList = ['source', 'db', 'tbl', 'alias']

    def __init__(self, parent=None, signalManager=None):
        OWWidget.__init__(self, parent, signalManager, "Dse Storage", wantMainArea=False)

        self.inputs = []
        self.outputs = [("meta", dict)]

        #initialize variables
        self.source = "prodhive"
        self.db = None
        self.tbl = None
        self.alias = None
        self.meta = None

        self.loadSettings()

        #GUI
        self.locationBox = OWGUI.widgetBox(self.controlArea, "Hive Table:")
        self.sourceCB = OWGUI.comboBox(self.locationBox, self, "source", label = "Source"+"  ",
                       items = SOURCES,
                       orientation=0, valueType = str, sendSelectedValue=True, emptyString=True,
                       callback=self.sourceChanged)

        self.dbCB = OWGUI.comboBox(self.locationBox, self, "db", label = "Database"+"  ",
                                    items = [],
                                    orientation=0, valueType = str, sendSelectedValue=True,
                                    callback=self.databaseChanged, emptyString=True)

        self.tblCB = OWGUI.comboBox(self.locationBox, self, "tbl", label = "Table:"+"  ",
                                    items = [],
                                    orientation=0, valueType = str, sendSelectedValue=True,
                                    callback=[self.tblChanged, self.setAlias], emptyString=True)

        OWGUI.lineEdit(self.controlArea, self, "alias", label="Alias"+"  ", callback=self.setAlias, orientation=0)
        OWGUI.separator(self.controlArea)

    def sourceChanged(self):
        if not self.source: return
        response = # get list of database
        if response and response['databases']:
            self.dbCB.clear()
            self.dbCB.addItems([str(name) for name in response['databases']])
            self.dbCB.setCurrentIndex(0)

    def databaseChanged(self):
        if not self.db: return
        response = # get list of tables
        if response and response['tables']:
            self.tblCB.clear()
            self.tblCB.addItems([str(name) for name in response['tables']])
            self.tblCB.setCurrentIndex(0)

    def tblChanged(self):
        if not self.tbl: return
        self.setSchema()


    def setAlias(self):
        if self.alias == None and self.tbl:
            self.alias = self.tbl
        self.meta['alias'] = self.alias

    def setSchema(self):
       # get schema of the selected table (source.db.tbl)


if __name__ == "__main__":
    appl = QApplication(sys.argv)
    ow = OWDseStorage()
    ow.show()
    appl.exec_()



Re: dependent combo box

Postby Ales » Fri Mar 22, 2013 18:18

Try calling 'loadSettings()' after initializing the GUI. The settings will be loaded in the order as they are listed in 'settingsList' (at least I think) and this should trigger a cascade of updates (i.e. first 'source' will be set calling 'sourceChanged()', updating the items in 'dbCB', then 'db' will be set etc.)

Re: dependent combo box

Postby ragrawal » Fri Mar 29, 2013 18:00

Hi Ales,

I tried that but its not loading dependent combobox. I guess I will need to overwrite loadSetting metting. Is there any example that I can look for. It seems the sourceChange() command is never fired at all.

Re: dependent combo box

Postby Ales » Mon Apr 08, 2013 13:58

ragrawal wrote:I guess I will need to overwrite loadSetting metting.

Don't do that.

Try initializing the combo box explicitly. Separate the combo box update code from the sourceChange, and call the update method in the __init__ method (as well as in the sourceChanged).


Return to Questions & Support