Add Tabs for Custom Script List #11951

Open
opened 2025-12-29 21:51:50 +01:00 by adam · 0 comments
Owner

Originally created by @k01ek on GitHub (Dec 25, 2025).

NetBox version

v4.3.7

Feature type

New functionality

Proposed functionality

When there are a lot of scripts, navigation becomes inconvenient. It would be great to be able to add tabs to the list of scripts for easier navigation.

Plan

By default, all modules are placed in the Scripts tab. If the 'tab' variable is set in the module, the corresponding tab is added.

Example

from extras.scripts import Script

class Script2(Script):
    def run(self, data, commit):
        pass

name = 'Test2'
tab = 'Tab2'

Result:

Image

Code changes

Minor changes to the code will be required.
Template script_list.html

{% block tabs %}
  <ul class="nav nav-tabs" role="tablist">
    <li class="nav-item" role="presentation">
      <a class="nav-link active" id="scripts-tab" data-bs-toggle="tab" data-bs-target="#scripts" type="button" role="tab" aria-controls="scripts" aria-selected="true">
        {% trans "Scripts" %}
      </a>
    </li>
{% for tab in extratabs %}
    <li class="nav-item" role="presentation">
      <a class="nav-link" id="{{ tab }}-tab" data-bs-toggle="tab" data-bs-target="#{{ tab }}" type="button" role="tab" aria-controls="{{ tab }}" aria-selected="false">
        {% trans tab %}
      </a>
    </li>
{% endfor %}
  </ul>
{% endblock tabs %}
{% block content %}
  <div class="tab-pane show active" id="scripts" role="tabpanel" aria-labelledby="scripts-tab">
    {% include 'extras/inc/script_list_content.html' with embedded=False %}
  </div>
  {% for tab, script_modules in extratabs.items %}
    <div class="tab-pane show" id="{{ tab }}" role="tabpanel" aria-labelledby="{{ tab }}-tab">
      {% include 'extras/inc/script_list_content.html' with embedded=False %}
    </div>
  {% endfor %}
{% endblock content %}

ScriptListView:

class ScriptListView(ContentTypePermissionRequiredMixin, View):

    def get_required_permission(self):
        return 'extras.view_script'

    def get(self, request):
        script_modules = ScriptModule.objects.restrict(request.user).prefetch_related(
            'data_source', 'data_file', 'jobs'
        )
        extratabs = {}
        default_tab = []
        for script_module in script_modules:
            module = script_module.get_module()
            tab = getattr(module, 'tab', None)
            if tab:
                extratabs.setdefault(tab, []).append(script_module)
            else:
                default_tab.append(script_module)

        context = {
            'model': ScriptModule,
            'script_modules': default_tab,
            'extratabs': extratabs
        }

        # Use partial template for dashboard widgets
        template_name = 'extras/script_list.html'
        if request.GET.get('embedded'):
            template_name = 'extras/inc/script_list_content.html'
            context['embedded'] = True
            context['script_modules'] = script_modules

        return render(request, template_name, context)

Use case

Users will be able to add tabs to better organize scripts, for example, add separate tabs for reports, departments, functionality or various purposes, etc.

Database changes

No response

External dependencies

No response

Originally created by @k01ek on GitHub (Dec 25, 2025). ### NetBox version v4.3.7 ### Feature type New functionality ### Proposed functionality When there are a lot of scripts, navigation becomes inconvenient. It would be great to be able to add tabs to the list of scripts for easier navigation. ### Plan By default, all modules are placed in the Scripts tab. If the 'tab' variable is set in the module, the corresponding tab is added. ### Example ``` from extras.scripts import Script class Script2(Script): def run(self, data, commit): pass name = 'Test2' tab = 'Tab2' ``` Result: <img width="1352" height="259" alt="Image" src="https://github.com/user-attachments/assets/3169519f-4607-4003-951d-702c7f52ec26" /> ### Code changes Minor changes to the code will be required. Template script_list.html ``` {% block tabs %} <ul class="nav nav-tabs" role="tablist"> <li class="nav-item" role="presentation"> <a class="nav-link active" id="scripts-tab" data-bs-toggle="tab" data-bs-target="#scripts" type="button" role="tab" aria-controls="scripts" aria-selected="true"> {% trans "Scripts" %} </a> </li> {% for tab in extratabs %} <li class="nav-item" role="presentation"> <a class="nav-link" id="{{ tab }}-tab" data-bs-toggle="tab" data-bs-target="#{{ tab }}" type="button" role="tab" aria-controls="{{ tab }}" aria-selected="false"> {% trans tab %} </a> </li> {% endfor %} </ul> {% endblock tabs %} {% block content %} <div class="tab-pane show active" id="scripts" role="tabpanel" aria-labelledby="scripts-tab"> {% include 'extras/inc/script_list_content.html' with embedded=False %} </div> {% for tab, script_modules in extratabs.items %} <div class="tab-pane show" id="{{ tab }}" role="tabpanel" aria-labelledby="{{ tab }}-tab"> {% include 'extras/inc/script_list_content.html' with embedded=False %} </div> {% endfor %} {% endblock content %} ``` ScriptListView: ``` class ScriptListView(ContentTypePermissionRequiredMixin, View): def get_required_permission(self): return 'extras.view_script' def get(self, request): script_modules = ScriptModule.objects.restrict(request.user).prefetch_related( 'data_source', 'data_file', 'jobs' ) extratabs = {} default_tab = [] for script_module in script_modules: module = script_module.get_module() tab = getattr(module, 'tab', None) if tab: extratabs.setdefault(tab, []).append(script_module) else: default_tab.append(script_module) context = { 'model': ScriptModule, 'script_modules': default_tab, 'extratabs': extratabs } # Use partial template for dashboard widgets template_name = 'extras/script_list.html' if request.GET.get('embedded'): template_name = 'extras/inc/script_list_content.html' context['embedded'] = True context['script_modules'] = script_modules return render(request, template_name, context) ``` ### Use case Users will be able to add tabs to better organize scripts, for example, add separate tabs for reports, departments, functionality or various purposes, etc. ### Database changes _No response_ ### External dependencies _No response_
adam added the type: featurenetboxstatus: needs triage labels 2025-12-29 21:51:50 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#11951