Skip to content

illagrenan/django-numpy-json-encoder

Repository files navigation

Django numpy JSON encoder

PyPi MIT TravisCI Coverage Supported Python implementations Supported Python versions

Introduction

Subclass of standard Django JSON encoder [1] that can encode some numpy types (integers, floats and arrays).

[1]https://docs.djangoproject.com/en/dev/topics/serialization/#djangojsonencoder

Installation

  • Supported Python versions are: 3.6, 3.7 and 3.7-dev.
  • Supported Django versions are: 2.0 and 2.1 (version 1.11 should also work).
pip install --upgrade django-numpy-json-encoder

Usage

# -*- encoding: utf-8 -*-
# ! python3

import numpy as np
from django.http import JsonResponse
from django.views import View

from django_numpy_json_encoder.numpy_encoder import NumpyJSONEncoder


class ExampleNumpyArrayJSONView(View):
    # noinspection PyMethodMayBeStatic
    def post(self, *args, **kwargs):
        numpy_array = np.random.rand(8, 42).astype(np.float32)

        return JsonResponse(data={'array': numpy_array},
                            encoder=NumpyJSONEncoder,
                            safe=True)

License

The MIT License (MIT)

About

Django JSON encoder with numpy support

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages