Package: contributions-django
GitHub Contributions DjangoCreate a GitHub Contributions graph for Django!This repository serves a Django app that can be used to generate a GitHub
contributions graph. Anexampleapplication is provided in theexamplefolder that can be interacted with here.QuickstartInstall contributions django:pipinstallcontributions-djangoAdd it to yourINSTALLED_APPS:INSTALLED_APPS=(...'contributions_django',...)To generate the graph in a view, importgenerate_contributors_graph. Give the function
a list of dates, and optionally a title to generate a context for your view:# -*- coding: utf-8 -*-from.modelsimportEventfromdjango_contributions.graphsimportgenerate_contributors_graphfromdjango.shortcutsimportrenderdefevents_view(request):"""Generate a papers graph using all Events dates"""# Get a single list of timestamps for the viewdates=Event.objects.values_list('date',flat=True)context=generate_contributors_graph(dates,title="Contributions")returnrender(request,"events/index.html",context)And then in your template, include this snippet where you want the graph to show:{% include "contributions_django/graph.html" %}Note that by default, the template includes bootstrap style (css) and font awesome icons.
See the next section, customization, if you already have these in your template
and want to remove them.CustomizationIf you want to customize any colors or text in the graph, you can do that in your
settings in aCONTRIBUTIONS_DJANGOsection - the defaults are shown below.CONTRIBUTIONS_DJANGO={# Gradients (0 or weak to stronger)'GRADIENT0':"#eee",'GRADIENT1':"#d6e685",'GRADIENT2':"#8cc665",'GRADIENT3':"#44a340",'GRADIENT4':"#1e6823",# The name to appear in tooltips (e.g., 12 contributions)'ITEM_NAME':'contributions',# Include bootstrap style for outer part of template# set this to false if you already have it on your page'INCLUDE_BOOTSTRAP':True,'INCLUDE_FONTAWESOME':True,# The icon to show next to title. Set to None to remove.'ICON_CLASSES':"fa fa-calendar",# Instead of boxes, use circles.'CIRCLES':True}ShapeThe cells can be circles if you setCIRCLESto true:ColorsFor gradients, you can find a color palette generation site likethis oneand customize gradients 1 through 4. You likely don't want to customizeGRADIENT0since
that is the grey (empty) color.greenThe default (akin to GitHub) is a set of greens:blueHere is the settings for a blue:# Gradients (0 or weak to stronger)'GRADIENT0':"#eee",# this is empty gray, likely don't want to change this'GRADIENT1':"#90E0EF",'GRADIENT2':"#00B4D8",'GRADIENT3':"#0077B6",'GRADIENT4':"#03045E",# this should be the darkestpinksAnd pinks!# Gradients (0 or weak to stronger) - below is green
'GRADIENT0': "#eee", # this is empty gray, likely don't want to change this
'GRADIENT1': "#fbc3bc",
'GRADIENT2': "#f7a399",
'GRADIENT3': "#f38375",
'GRADIENT4': "#ef6351", # this should be the darkestPlease open a pull request to add examples of your favorite palettes!TemplateIf you want to customize the entire template, then you can create a copy of
thedefaultand include it in your view instead.{% include "example/mygraph.html" %}DatesImportantly, with Django it's recommended to not use naive datetimes. In fact,
you'll get an error if you try saving a naive datetime to a model field. This means
that contributions_django must be flexible to accept naive or aware times.
Here is how you can generate timestamps that are aware:fromdatetimeimporttimedeltafromdjango.utilsimporttimezoneimportpytz# get today and one year earliertoday=timezone.now()last_year=today-timedelta(days=365)# Create an event with a timezone aware timestampEvent.objects.create(date=last_year,name="My Event")And here is a naive timestamp:fromdatetimeimportdatetimetimestamp=datetime.now()Regardless of your choice, the contributions graph only cares about the day,
so it takes thetimestamp.date()that is available regardless of being naive
or not.Example ApplicationYou can develop or test interactively using the example (very simple) application
undertests. Themanage.pyis located in the root directory
so it's easy to update your install and then interact with your test interface.
First create a virtual environment in the root and install the dependencies,
mostly your application in development mode:python-mvenvenvsourceenv/bin/activatecdexample
pipinstall-rrequirements.txtThen make migrations - this will create a basic Event model:$pythonmanage.pymakemigrationsmain
Migrationsfor'main':example/apps/main/migrations/0001_initial.py-CreatemodelEventAnd migrate$pythonmanage.pymigrate
Operationstoperform:Applyallmigrations:admin,auth,contenttypes,main,sessions
Runningmigrations:Applyingcontenttypes.0001_initial...OKApplyingauth.0001_initial...OKApplyingadmin.0001_initial...OKApplyingadmin.0002_logentry_remove_auto_add...OKApplyingadmin.0003_logentry_add_action_flag_choices...OKApplyingcontenttypes.0002_remove_content_type_name...OKApplyingauth.0002_alter_permission_name_max_length...OKApplyingauth.0003_alter_user_email_max_length...OKApplyingauth.0004_alter_user_username_opts...OKApplyingauth.0005_alter_user_last_login_null...OKApplyingauth.0006_require_contenttypes_0002...OKApplyingauth.0007_alter_validators_add_error_messages...OKApplyingauth.0008_alter_user_username_max_length...OKApplyingauth.0009_alter_user_last_name_max_length...OKApplyingauth.0010_alter_group_name_max_length...OKApplyingauth.0011_update_proxy_permissions...OKApplyingmain.0001_initial...OKApplyingsessions.0001_initial...OKNote that the example app creates the Event model with the sole purpose of
populating something with dates. You can use contributions_django for any
kind of visualization that provides a list of dates.
At this point, if you run your server and browse to the index, you'll
see the empty graph, because we haven't created any events:pythonmanage.pyrunserverNext, let's run the management command to create a bunch of random events -
1000 between today and last year, specifically. This is done by the example
management commandhere:$pythonmanage.pycreate_events
Creating1000events:

CreatingEventjoyous-lemon-3372at2020-07-1200:56:15.675397+00:00
CreatingEventmisunderstood-leg-5849at2019-09-2323:23:01.675397+00:00
CreatingEventrainbow-leg-6361at2020-07-1619:03:03.675397+00:00
CreatingEventcowy-eagle-2288at2020-02-1921:55:53.675397+00:00
...You should then be able to run the application and see the events populated!pythonmanage.pyrunserverDevelopmentI suggest that you follow the same steps to set up the example app:python-mvenvenv
pipinstall-rrequirements.txtsourceenv/bin/activateAnd then you can change code undercontributions_djangoand test the live outcome with the example app.Many Thankscookiecutter-djangopackageThe library is resurrected from the archived repositorycontributions-graphand updated for Django. The original license can be seenhere.