Allow ViewTab to be rendered on a condition is met #7540

Closed
opened 2025-12-29 20:24:55 +01:00 by adam · 11 comments
Owner

Originally created by @abhi1693 on GitHub (Jan 20, 2023).

NetBox version

v3.4.2

Feature type

Change to existing functionality

Proposed functionality

Add a new condition parameter that takes a lambda function which evaluates to either true or false based on which the tab is displayed.

I overrode the ViewTab in my plugin as displayed below and something similar can also be done on the parent class.

from utilities.views import ViewTab as _ViewTab

class ViewTab(_ViewTab):
    """
    This ViewTab allows conditional rendering of the tabs when a condition is met.
    """

    def __init__(self, condition=None, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.condition = condition

    def render(self, instance):
        condition_value = self._get_condition_value(instance)
        if self.condition and not condition_value:
            return {}
        return super().render(instance)

    def _get_condition_value(self, instance):
        if not self.condition:
            return None
        if callable(self.condition):
            return self.condition(instance)
        return self.condition

Use case

In my plugin, I want to conditionally display the tab only when a specific set of tags are set on the device otherwise it should stay hidden. Adding a filter query makes the page crash with No matching query which is not ideal.

Database changes

None

External dependencies

None

Originally created by @abhi1693 on GitHub (Jan 20, 2023). ### NetBox version v3.4.2 ### Feature type Change to existing functionality ### Proposed functionality Add a new `condition` parameter that takes a `lambda` function which evaluates to either `true` or `false` based on which the tab is displayed. I overrode the `ViewTab` in my plugin as displayed below and something similar can also be done on the parent class. ```python from utilities.views import ViewTab as _ViewTab class ViewTab(_ViewTab): """ This ViewTab allows conditional rendering of the tabs when a condition is met. """ def __init__(self, condition=None, *args, **kwargs): super().__init__(*args, **kwargs) self.condition = condition def render(self, instance): condition_value = self._get_condition_value(instance) if self.condition and not condition_value: return {} return super().render(instance) def _get_condition_value(self, instance): if not self.condition: return None if callable(self.condition): return self.condition(instance) return self.condition ``` ### Use case In my plugin, I want to conditionally display the `tab` only when a specific set of tags are set on the device otherwise it should stay hidden. Adding a filter query makes the page crash with `No matching query` which is not ideal. ### Database changes None ### External dependencies None
adam added the type: featurepending closurestatus: under review labels 2025-12-29 20:24:55 +01:00
adam closed this issue 2025-12-29 20:24:55 +01:00
Author
Owner

@jeremystretch commented on GitHub (Jan 26, 2023):

I believe this is already possible using the badge and hide_if_empty parameters. You can set badge to a callable that returns None when the tab shouldn't be displayed, and set hide_if_empty to True.

@jeremystretch commented on GitHub (Jan 26, 2023): I believe this is already possible using the `badge` and `hide_if_empty` parameters. You can set `badge` to a callable that returns None when the tab shouldn't be displayed, and set `hide_if_empty` to True.
Author
Owner

@abhi1693 commented on GitHub (Jan 26, 2023):

In my case, I still want to show the tab when the condition meets, so setting true on hide_if_empty will not solve that problem.

@abhi1693 commented on GitHub (Jan 26, 2023): In my case, I still want to show the tab when the condition meets, so setting true on `hide_if_empty` will not solve that problem.
Author
Owner

@jeremystretch commented on GitHub (Jan 26, 2023):

I'm not sure I follow. Can you provide an example?

@jeremystretch commented on GitHub (Jan 26, 2023): I'm not sure I follow. Can you provide an example?
Author
Owner

@abhi1693 commented on GitHub (Jan 26, 2023):

From my use case sited above, I have built a plugin to take backups of network devices documented in NetBox. However, we also use the Device model for other physical hardware like baremetal servers. Now, in this, I would want to show the Backups tab when 1 or more tags match from the object. This tab may start out with no child objects associated with the parent. But, the tab should still be visible as the condition is valid (due to matching tags).

Wherever, the tags do not match the condition on tab, it is simply not displayed.

In this particular use-case, hide_if_empty=true does not help as I still want the tab to visible when no child objects are associated.

@abhi1693 commented on GitHub (Jan 26, 2023): From my use case sited above, I have built a plugin to take backups of network devices documented in NetBox. However, we also use the `Device` model for other physical hardware like baremetal servers. Now, in this, I would want to show the `Backups` tab when 1 or more tags match from the object. This tab may start out with no child objects associated with the parent. But, the tab should still be visible as the condition is valid (due to matching tags). Wherever, the tags do not match the condition on tab, it is simply not displayed. In this particular use-case, `hide_if_empty=true` does not help as I still want the tab to visible when no child objects are associated.
Author
Owner

@PieterL75 commented on GitHub (Feb 10, 2023):

I'm also looking for a way to hide/display a tab, based on conditions (a certain customfield has data)
with the hide_if_empty=true, you need to set a badge value to display the tab.
But in my case, there is no relevant info that I can set to the badge. no counter or important things

I also would like to see an alternative for hiding/displaying the tab, without the need of a badge
an extra lamba enabled field like 'badge' would help indeed

@PieterL75 commented on GitHub (Feb 10, 2023): I'm also looking for a way to hide/display a tab, based on conditions (a certain customfield has data) with the hide_if_empty=true, you need to set a badge value to display the tab. But in my case, there is no relevant info that I can set to the badge. no counter or important things I also would like to see an alternative for hiding/displaying the tab, without the need of a badge an extra lamba enabled field like 'badge' would help indeed
Author
Owner

@github-actions[bot] commented on GitHub (May 12, 2023):

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. NetBox is governed by a small group of core maintainers which means not all opened issues may receive direct feedback. Do not attempt to circumvent this process by "bumping" the issue; doing so will result in its immediate closure and you may be barred from participating in any future discussions. Please see our contributing guide.

@github-actions[bot] commented on GitHub (May 12, 2023): This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. NetBox is governed by a small group of core maintainers which means not all opened issues may receive direct feedback. **Do not** attempt to circumvent this process by "bumping" the issue; doing so will result in its immediate closure and you may be barred from participating in any future discussions. Please see our [contributing guide](https://github.com/netbox-community/netbox/blob/develop/CONTRIBUTING.md).
Author
Owner

@DanSheps commented on GitHub (May 15, 2023):

From my use case sited above, I have built a plugin to take backups of network devices documented in NetBox.

Aren't you duplicating this?

@DanSheps commented on GitHub (May 15, 2023): > From my use case sited above, I have built a plugin to take backups of network devices documented in NetBox. Aren't you duplicating [this](https://github.com/dansheps/netbox-config-backup)?
Author
Owner

@abhi1693 commented on GitHub (May 30, 2023):

Aren't you duplicating this?

Not really, mine does not do any automated tasks. It provides a DB model to store the configurations right in NetBox and provides a way to run diffs. So, a user can utlize what they prefer to gather the configs. There is no git integration either in mine as I managed to provide a similar functionality right with the UI.

@abhi1693 commented on GitHub (May 30, 2023): > Aren't you duplicating [this](https://github.com/dansheps/netbox-config-backup)? Not really, mine does not do any automated tasks. It provides a DB model to store the configurations right in NetBox and provides a way to run diffs. So, a user can utlize what they prefer to gather the configs. There is no git integration either in mine as I managed to provide a similar functionality right with the UI.
Author
Owner

@jeremystretch commented on GitHub (Aug 4, 2023):

Given that ViewTab is exposed as part of the plugins API, I'd like to keep hide_if_empty working as-is.

Add a new condition parameter that takes a lambda function

What we can do is introduce the condition kwarg as you have above, with a default pointing to a class method (let's call it display_tab()). Then we can move the current badge logic under display_tab(). This will retain the existing behavior while allowing the user to pass a lambda via condition to override the entire default logic.

@jeremystretch commented on GitHub (Aug 4, 2023): Given that `ViewTab` is exposed as part of the plugins API, I'd like to keep `hide_if_empty` working as-is. > Add a new condition parameter that takes a lambda function What we can do is introduce the `condition` kwarg as you have above, with a default pointing to a class method (let's call it `display_tab()`). Then we can move the current badge logic under `display_tab()`. This will retain the existing behavior while allowing the user to pass a lambda via `condition` to override the entire default logic.
Author
Owner

@github-actions[bot] commented on GitHub (Nov 3, 2023):

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. NetBox is governed by a small group of core maintainers which means not all opened issues may receive direct feedback. Do not attempt to circumvent this process by "bumping" the issue; doing so will result in its immediate closure and you may be barred from participating in any future discussions. Please see our contributing guide.

@github-actions[bot] commented on GitHub (Nov 3, 2023): This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. NetBox is governed by a small group of core maintainers which means not all opened issues may receive direct feedback. **Do not** attempt to circumvent this process by "bumping" the issue; doing so will result in its immediate closure and you may be barred from participating in any future discussions. Please see our [contributing guide](https://github.com/netbox-community/netbox/blob/develop/CONTRIBUTING.md).
Author
Owner

@github-actions[bot] commented on GitHub (Dec 4, 2023):

This issue has been automatically closed due to lack of activity. In an effort to reduce noise, please do not comment any further. Note that the core maintainers may elect to reopen this issue at a later date if deemed necessary.

@github-actions[bot] commented on GitHub (Dec 4, 2023): This issue has been automatically closed due to lack of activity. In an effort to reduce noise, please do not comment any further. Note that the core maintainers may elect to reopen this issue at a later date if deemed necessary.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#7540