Coder Social home page Coder Social logo

djangoplotly's Introduction

Integrating PlotLy into Django

Notes

  • Updated to use Django 3.0

Intructions

Very simple introduction of using PlotLy in a Django webapp.

Create your plot using the Plotly Figure with data and layout :

fig = go.Figure(data=data, layout=layout)

Import the function from plotly.offline import plot which creates a <div>(...)</div> to be rendered in the template:

plot_div = plot(fig, output_type='div', include_plotlyjs=False)

In the view append the div above to the context_data:

context['plot'] = plot_div

Then in the respective template:

  1. Add to the HTML head:
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
  1. Somewhere in the HTML body, call the context variable and render it as safe:
<div class="col-md-12">{{plot|safe}}</div>

Note that I used include_plotlyjs=False in the plotly.offline.plot, and in the top of my template, I explcity include the PlotLy JavaScript. In the case you want to display several plots in same webapp, the browser should be smart enough to cache the plotly-latest.min.js thus speeding up the the graphic display process.

TLTR;

Tests using Plot.ly inside Django.

Testing

  • 1D
  • 2D
  • 3D surface plot
  • 1D generated from Ajax (to test plotly speed)
  • 1D from CSV files
  • Live 2D Plot
  • 3D scatter plot to test PlotLy speed with multiple 3D points

Installation

Requirements are in the requirements.txt file.

Use virtualenv:

virtualenv -p python3 venv
source venv/bin/activate
pip install -U -r requirements.txt

Run it:

cd site1/
./manage.py makemigrations
./manage.py migrate
./manage.py runserver 0.0.0.0:8000

Go to: http://127.0.0.1:8000

Code:

The main code is in:

site1/app1

Views:

site1/app1/views.py

Code for plots is here:

site1/app1/plots.py

djangoplotly's People

Contributors

ricleal avatar ricleal-fugue avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.