Getting started with Django

The Django library provides powerful data visualization capabilities for Django applications. It creates a local web server that can receive and visualize data, making it perfect for data analysis, debugging, and monitoring applications. It also includes built-in handlers that make it easy to pipe all logs into Shellviz, and supports visualizations of ORM queries and model instances.

Installation

Install the Python package:

pip install shellviz

Then add the shellviz widget to your templates. You should now see a widget - just like the one on this page - on the bottom right of your pages.

<script src="https://unpkg.com/shellviz"></script>

Logging Data

You can now send logs and other data visualizations from Python to the browser:

from shellviz import log
log('this will show up in the shellviz widget!')

Logs can accept multiple arguments. It can visualize strings, numbers, lists, dictionaries, and even Djagno model instances and queries:

from shellviz import log
log('some text', 200, {'status': 200, 'num_responses': 20, 'execution_time': 540})

Visualizations

You can use any of shellviz' /visualizations in your Django codebase. These calls run alongside logging calls

import logging
from shellviz import table, json, progress

logging.info('Migration started')
progress(0)

# ...
logging.info('Migration complete')
progress(1)

Logging Handler

Shellviz includes the shellviz.django.logging.ShellvizHandler, which allows it to be configured as an automated logging handler for Django apps:

# settings.py
LOGGING = {
    'handlers': {
        'shellviz': {
            'class': 'shellviz.django.logging.ShellvizHandler',
            'level': 'INFO',
        },
    },
    'loggers': {
        'myapp': {
            'handlers': ['shellviz'],
            'level': 'INFO',
            'propagate': True,
        },
    },
}

Now, logging calls will automatically be shown in shellviz:

import logging
logging.info('This will appear in Shellviz')
# .. you can also specify a custom logger
logger = logging.getLogger('myapp')
logger.info('So will this!')

You can also use Shellviz's built-in log visualization method, which adds support for several additional features such as passing several arguments, running multiple logging streams, and richer data visualization.

from shellviz import log
log('response', {'status': 200}, id='web_requests')
log(order_instance, id='migration')

QuerySets and Models

Shellviz can automatically encode Django QuerySet and Model instances:

from shellviz import json, card
from myapp.models import User

# Display a model instance
json(request.user, id='current_user')

# Display a queryset
card(User.objects.all(), id='all_users')
table(User.objects.filter(is_active=True), id='active_users')

Generic Timing Mixin

Shellviz includes a TimingMixin that automatically logs timing information for ALL method calls on any class. Simply inherit from TimingMixin and all your methods will be automatically timed:

# For Django CBVs
from django.views.generic import ListView
from shellviz.django import TimingMixin

class ProjectListView(TimingMixin, ListView):
    model = Project
    # ... other view configuration

# For any custom class
class MyCustomClass(TimingMixin):
    def method_one(self):
        # This will be automatically timed
        pass
    
    def method_two(self):
        # This will also be automatically timed
        pass

The mixin automatically intercepts and times ALL method calls, with timestamps relative to the start of the first method call.

Example output in Shellviz:

timing_ProjectListView: dispatch: 0.000s
timing_ProjectListView: get_queryset: 0.101s
timing_ProjectListView: get_context_data: 0.102s
timing_ProjectListView: render_to_response: 0.150s

timing_MyCustomClass: method_one: 0.100s
timing_MyCustomClass: method_two: 0.150s

Configuration

If you're using Django, you can configure Shellviz by adding SHELLVIZ_* settings to your settings.py file. This takes precedence over environment variables.

# settings.py
SHELLVIZ_PORT = 8080
SHELLVIZ_SHOW_URL = False
SHELLVIZ_URL = "https://my-custom-domain.com"