question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add loading of Plugin Models to nbshell command

See original GitHub issue

Environment

  • Python version: N/A
  • Nautobot version: 1.0.0b4

Proposed Functionality

Calling nautobot-server nbshell loads all of the builtin models for easy access to start debugging. It would be nice to add loading of the models for registered plugins.

Use Case

I am developing a new plugin, and need to debug an issue that comes up. It would be nice to have access to that model upon entering nbshell

Database Changes

N/A

External Dependencies

N/A

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:3
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
jathanismcommented, Apr 30, 2021

Hmmm… I like this idea. There is a better way to do this automatically than re-inventing the wheel ourselves. There is a shell_plus command that comes bundled with the django-extensions plugin that already does this. This isn’t part of core, but it doesn’t matter right now. I’ll show you how to use it while we review whether to extend the core to do it.

Example

First, the example. I’ve added nautobot.extras.tests.dummy_plugin to my PLUGINS since that comes bundled with Nautobot and provides its own DummyModel.

$ nautobot-server shell_plus
# Shell Plus Model Imports
from django.contrib.admin.models import LogEntry
from django.contrib.auth.models import Group, Permission
from django.contrib.contenttypes.models import ContentType
from django.contrib.sessions.models import Session
from nautobot.circuits.models import Circuit, CircuitTermination, CircuitType, Provider
from nautobot.dcim.models.cables import Cable, CablePath
from nautobot.dcim.models.device_component_templates import ConsolePortTemplate, ConsoleServerPortTemplate, DeviceBayTemplate, FrontPortTemplate, InterfaceTemplate, PowerOutletTemplate, PowerPortTemplate, RearPortTemplate
from nautobot.dcim.models.device_components import ConsolePort, ConsoleServerPort, DeviceBay, FrontPort, Interface, InventoryItem, PowerOutlet, PowerPort, RearPort
from nautobot.dcim.models.devices import Device, DeviceRole, DeviceType, Manufacturer, Platform, VirtualChassis
from nautobot.dcim.models.power import PowerFeed, PowerPanel
from nautobot.dcim.models.racks import Rack, RackGroup, RackReservation, RackRole
from nautobot.dcim.models.sites import Region, Site
from nautobot.extras.models.change_logging import ObjectChange
from nautobot.extras.models.customfields import CustomField, CustomFieldChoice
from nautobot.extras.models.datasources import GitRepository
from nautobot.extras.models.models import ConfigContext, CustomLink, ExportTemplate, ImageAttachment, Job, JobResult, Webhook
from nautobot.extras.models.relationships import Relationship, RelationshipAssociation
from nautobot.extras.models.statuses import Status
from taggit.models import Tag, TaggedItem
from nautobot.extras.tests.dummy_plugin.models import AnotherDummyModel, DummyModel
from nautobot.ipam.models import Aggregate, IPAddress, Prefix, RIR, Role, RouteTarget, Service, VLAN, VLANGroup, VRF
from nautobot.tenancy.models import Tenant, TenantGroup
from nautobot.users.models import AdminGroup, ObjectPermission, Token, User
from nautobot.virtualization.models import Cluster, ClusterGroup, ClusterType, VMInterface, VirtualMachine
from social_django.models import Association, Code, Nonce, Partial, UserSocialAuth
# Shell Plus Django Imports
from django.core.cache import cache
from django.conf import settings
from django.contrib.auth import get_user_model
from django.db import transaction
from django.db.models import Avg, Case, Count, F, Max, Min, Prefetch, Q, Sum, When
from django.utils import timezone
from django.urls import reverse
from django.db.models import Exists, OuterRef, Subquery
Python 3.8.7 (default, Jan 19 2021, 09:28:54)
[Clang 12.0.0 (clang-1200.0.32.28)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>>
>>> DummyModel.objects.all()
<QuerySet []>

Observe above this line:

from nautobot.extras.tests.dummy_plugin.models import AnotherDummyModel, DummyModel

Neat! So how to use it?

How to use shell_plus

Install django-extensions into your Nautobot environment:

pip3 install django-extensions

Add django_extensions to your EXTRA_INSTALLED_APPS in your nautobot_config.py:

EXTRA_INSTALLED_APPS = ["django_extensions"]

Run nautobot-server shell_plus:

$ nautobot-server shell_plus

Hint: If you also install iPython into your environment, shell_plus will automatically pick it up and use it instead:

$ nautobot-server shell_plus
# ... imports omitted for brevity
Python 3.8.7 (default, Jan 19 2021, 09:28:54)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.22.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]:

It will also allow you to customize your imports by adding SHELL_PLUS_IMPORTS to your nautobot_config.py!

Cheers!

1reaction
jmcgill298commented, Apr 30, 2021

The code also indicates that name conflicts will be handled automatically by Django as well (the concern from above Issue)

Read more comments on GitHub >

github_iconTop Results From Across the Web

nbshell plugin model support · Issue #5008 - GitHub
When entering nbshell load plugin models like core models are. Use Case. Loading plugin models would help with debugging plugin development.
Read more >
ntc-netbox-plugin-metrics-ext - PyPI
RQ Queues stats; Reports stats; Models count (configurable via configuration.py). Add your own metrics. This plugin supports some options to ...
Read more >
Getting Started - Nautobot Documentation
The Nautobot project follows a branching model based on Git-flow. ... Run Nautobot integration tests. loaddata Load data from file. makemigrations Perform ...
Read more >
Nautobot Solution Guide: Source of Truth for Network ...
The “nbshell” Management Command. Implemented as a Django management command, this tool provides access to an interactive Python shell with the Nautobot data....
Read more >
Posts by Year - NetBox.dev
#9971 - Enable ordering of nested group models by name ... #10716 - Add left/right page plugin content embeds for tag view; #10719...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found