Skip to main content

No project description provided

Project description

Flask-ChartJS-Manager

Flask-ChartJS-Manager (from now on FCM) provides a simple interface to use ChartJS javascript library with Flask.

🚧 This package is under heavy development..

Installation

Install the extension with pip:

pip install flask-chartjs-manager

Install with poetry:

poetry add flask-chartjs-manager

Usage

Once installed the FCM is easy to use. Let's walk through setting up a basic application. Also please note that this is a very basic guide: we will be taking shortcuts here that you should never take in a real application.

To begin we'll set up a Flask app:

from flask import Flask

app = Flask(__name__)

Setting up extension

FCM works via a ChartJSManager object. To kick things off, we'll set up the chartjs_manager by instantiating it and telling it about our Flask app:

from flask_chartjs import ChartJSManager

chartjs_manager = ChartJSManager()
chartjs_manager.init_app(app)

This will make available the chartjs object into the templates context so you could load the javascript package easily, like this. you can configure a CHARTJS_LOCAL_PATH to add a custom location for the package

<head>
  {{ chartjs.load() }}
</head>

Creating a chart

Now we will construct a basic chart. For this you have to import Chart and DataSet objects in order to create a new chart.

from flask_chartjs import Chart, DataSet
from flask import render_template

@app.get('/chart-example')
def chart_example():

    chart = Chart('income-outcome', 'bar') # Requires at least an ID and a chart type.

    dataset_income = DataSet('Income', [100,200,300])
    dataset_outcome = DataSet('OutCome', [50,100,150])

    chart.data.add_labels('jan', 'feb', 'mar')
    chart.data.add_dataset(dataset_income)
    chart.data.add_dataset(dataset_outcome)

    return render_template('path/to/template.html', my_chart=chart)

Rendering the chart

Once created you can pass the Chart object to render_template and use it likewise.

<!-- chartjs.load() must be called before this line -->
<div class="my-classes">{{ chartjs.render(my_chart) }}</div>

Changelog 0.1.11

Added new options to personalize using the full power of the ChartJS library. Now you can limit the python code to add the dataset itself and let the configuration and further customization to the actual template level. See the next example. If you add a % in front of a value its assumed to be a javascript variable. You have the especial kwarg datasets to access directly to the datasets options, as you can observe in the next example. The key is the dataset index.

<!-- load_chartjs() must be called before this line -->
<script>
  function addDollarSign(value, index, ticks) {
    return "$" + value.toLocaleString();
  }
</script>
<div class="my-classes">
  {{ chartjs.render(chart, options={ 'datasets': { 'line': { 'tension': 0.4,
  'fill': true, } }, 'elements': { 'point': { 'pointStyle': 'circle', 'radius':
  5, 'hitRadius': 5, 'hoverRadius': 5, 'borderWidth': 5, } }, 'scales': { 'y': {
  'ticks': { 'callback': '%addDollarSign' } } } }, datasets={ 0: {
  'borderColor': 'rgba(20, 184, 166, 0.8)', 'backgroundColor': 'rgba(20, 184,
  166, 0.4)', }, 1: { 'borderColor': 'rgba(239, 68, 68, 0.8)',
  'backgroundColor': 'rgba(239, 68, 68, 0.4)', }, }) }}
</div>

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flask_chartjs_manager-0.1.11.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

flask_chartjs_manager-0.1.11-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file flask_chartjs_manager-0.1.11.tar.gz.

File metadata

  • Download URL: flask_chartjs_manager-0.1.11.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.2 CPython/3.8.10 Linux/5.10.16.3-microsoft-standard-WSL2

File hashes

Hashes for flask_chartjs_manager-0.1.11.tar.gz
Algorithm Hash digest
SHA256 6c1f48e0d86202398e8fed9e21905cad497a58b452f48431476eb902c2ccc8e3
MD5 89edf97cdb8133467ab3cb53f18faf85
BLAKE2b-256 81db48e72de72e5db704444f9e2ebac6f3ce1ff51db75b7099db4fb0bdfc2f75

See more details on using hashes here.

File details

Details for the file flask_chartjs_manager-0.1.11-py3-none-any.whl.

File metadata

  • Download URL: flask_chartjs_manager-0.1.11-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.2 CPython/3.8.10 Linux/5.10.16.3-microsoft-standard-WSL2

File hashes

Hashes for flask_chartjs_manager-0.1.11-py3-none-any.whl
Algorithm Hash digest
SHA256 2bc82844de58b201972cc07d63767fda0a3d8eacff394a8b78e4a8c8f1725ec2
MD5 a56bf1c121c0c9b4760bf71a7cd55d28
BLAKE2b-256 698596f4b8af0e4cb658d5475d31d50dbb496203331e88186d92a7980c8947c4

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page