Tabs in Dashboards

Hey everyone!! šŸ™‚

In one of my previous posts, I had explained some of the basics of working with dashboards in Horizon. In the past few weeks, I’ve learned a lot more about Horizon’s dashboards like creating tabs, workflows etc. In this post, I will share the basics of creating and working with Tabs.

When creating tabs, the most fundamental element is the TabGroup, which contains all your tabs. A TabGroup can contain one or more tabs, and can be defined for a panel in the file. A TabGroup is basically a container for individual Tabs. A basic TabGroup can be defined as follows:

class MyTabGroup(tabs.TabGroup):
    slug = "mypanel_tabs"
    tabs = (MyTab,)
    sticky = True

In the above code, MyTabGroup is the name for the TabGroup that you want to define (it can be named anything you want). The tabs attribute lists all the tabs that come under this TabGroup (in this case, just one tab called MyTab). There are a number of other attributes and methods that are available when working with TabGroups which you can find here.

Once you have defined a TabGroup, the next thing to be defined is the Tab itself. There are 2 types of Tabs that Horizon allows: Tab and TableTab. The Tab class allows you to create a simple tab, while the TableTab allows you to create a tab that knows how to handle any DataTables that are rendered inside it. The TableTab basically simplifies the complex handling of Tabs, their dynamic loading, Tables, their dynamic updating and the actions associated with them.

When creating an object of the Tab class, there are 3 attributes and methods must be given i.e. they are required. These are name, slug and get_context_data. The name attribute specifies the name that will be displayed in the HTML i.e. it is the display name for the tab. The slug attribute defines the URL slug and id for the Tab, and must thus be unique within a TabGroup. The get_context_data method returns the dictionary of the data that is to be used to render the Tab. A simple Tab can be defined as follows:

class MyTab(tabs.Tab):
    name = _("My Tab")
    slug = "my_tab"

    def get_context_data(self, request):
        app_id = self.tab_group.kwargs['application_id']
        solum = solumclient(request)
        app = solum.apps.find(name_or_id=app_id)
        return {"application": app}

In the above code, you can change the class name, the name attribute value and the slug attribute value to whatever you want. In the get_context_data method, you just need to ensure that you return a dictionary which contains all the data that you want to render using this Tab. The Tab class has a lot more attributes and methods that you can use to customize the Tab, more details about which you can find here.

There are also a few specific views that can be used to handle TabGroups like the TabView and the TabbedTableView. These classes have only one attribute that must always be defined, which is the tab_group_class attribute. The TabbedTableView simplifies handling both Tabs and Tables together. More details about the attributes and methods allowed for the TabView and the TabbedTableView can be found here.

In this post, I have covered just the basics of working with Tabs. But there is a lot more that can be done with Tabs. Horizon provides a lot of great functionalities and ways to customize Tabs.

Stay tuned for more updates regarding Horizon and some more cool features that it provides! šŸ˜€


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s