• Stars
    star
    382
  • Rank 112,241 (Top 3 %)
  • Language
    Python
  • License
    Apache License 2.0
  • Created over 4 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

gRPC for Django.

Django gRPC Framework

https://readthedocs.org/projects/djangogrpcframework/badge/?version=latest https://travis-ci.org/fengsp/django-grpc-framework.svg?branch=master https://img.shields.io/pypi/pyversions/djangogrpcframework https://img.shields.io/pypi/l/djangogrpcframework

Django gRPC framework is a toolkit for building gRPC services, inspired by djangorestframework.

Requirements

  • Python (3.6, 3.7, 3.8)
  • Django (2.2, 3.0), Django REST Framework (3.10.x, 3.11.x)
  • gRPC, gRPC tools, proto3

Installation

$ pip install djangogrpcframework

Add django_grpc_framework to INSTALLED_APPS setting:

INSTALLED_APPS = [
    ...
    'django_grpc_framework',
]

Demo

Here is a quick example of using gRPC framework to build a simple model-backed service for accessing users, startup a new project:

$ django-admin startproject demo
$ python manage.py migrate

Generate .proto file demo.proto:

python manage.py generateproto --model django.contrib.auth.models.User --fields id,username,email --file demo.proto

Generate gRPC code:

python -m grpc_tools.protoc --proto_path=./ --python_out=./ --grpc_python_out=./ ./demo.proto

Now edit the demo/urls.py module:

from django.contrib.auth.models import User
from django_grpc_framework import generics, proto_serializers
import demo_pb2
import demo_pb2_grpc


class UserProtoSerializer(proto_serializers.ModelProtoSerializer):
    class Meta:
        model = User
        proto_class = demo_pb2.User
        fields = ['id', 'username', 'email']


class UserService(generics.ModelService):
    queryset = User.objects.all()
    serializer_class = UserProtoSerializer


urlpatterns = []
def grpc_handlers(server):
    demo_pb2_grpc.add_UserControllerServicer_to_server(UserService.as_servicer(), server)

That's it, we're done!

$ python manage.py grpcrunserver --dev

You can now run a gRPC client to access the service:

with grpc.insecure_channel('localhost:50051') as channel:
    stub = demo_pb2_grpc.UserControllerStub(channel)
    for user in stub.List(demo_pb2.UserListRequest()):
        print(user, end='')

More Repositories

1

plan

Crontab jobs management in Python
Python
1,171
star
2

color-thief-py

Grabs the dominant color or a representative color palette from an image. Uses Python and Pillow.
Python
1,017
star
3

pencil

A web application microframework for Rust
Rust
871
star
4

python-snippets

A basket of python snippets
222
star
5

sender

One easy to use Python SMTP client
Python
197
star
6

easy-python

Libraries you didn't know you would need
192
star
7

rc

Redis cache cluster system in Python
Python
121
star
8

knight

One HTTP development server with reloader for Go
Go
69
star
9

flask-snippets

Flask Snippets
Python
68
star
10

shortly

A URL shortener
Python
50
star
11

flask-profile

Flask Application Profiler
JavaScript
37
star
12

lookup

Look up words via the command line
Python
36
star
13

batpod

A really tiny web framework
Python
23
star
14

python

Python Style Guide
22
star
15

flask-application-wizard

Helper script to create Flask Applications
Python
16
star
16

cli

Rust command line utility
Rust
13
star
17

clock

A minimalist datetime library for Python
Python
10
star
18

pypages

Simple Python Pagination
Python
9
star
19

golang-tour

Sample Go code from the Tour of Go
4
star
20

slim

SimpleHTTPServer serving files relative to the current directory
Go
2
star
21

fengsp.github.io

Shipeng Feng's Writings
HTML
1
star
22

fork

Doing subprocess in Python should be easy
Python
1
star
23

douban-photoalbum-downloader

Download douban photo album
Python
1
star
24

blog

My personal website
HTML
1
star
25

faster-python

Write Faster Python Programs
Python
1
star
26

ninja

The ninja template engine for Go
Go
1
star
27

markdown-online

Put your local markdowns online
JavaScript
1
star
28

pyalgorithms

Algorithms in Python
Python
1
star
29

PyObject

Python 2.5 Source
Python
1
star