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:

uv 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 ChartJS object. To kick things off, we'll set up the chartjs by instantiating it and telling it about our Flask app:

from flask_chartjs import ChartJS

chartjs = ChartJSManager()
chartjs.init_app(app)

This will make available the chartjs object into the templates context so you could load the javascript package easily. 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.16.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flask_chartjs_manager-0.1.16-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for flask_chartjs_manager-0.1.16.tar.gz
Algorithm Hash digest
SHA256 bf29f74cdeb4c74f0d5eb3946bce2a23a632940a64d9cbf78573c66df58da535
MD5 f04451eac3a86a12757e2430438a40d0
BLAKE2b-256 1b659b60f55195bece6d7d3dfa62d9b288d1138a2dd6334c24ba77217d7e5ea0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flask_chartjs_manager-0.1.16-py3-none-any.whl
Algorithm Hash digest
SHA256 bc89a55dbc14c55a602a0bdb49b55a00171fa6eb2b8d176ddc45edb01d468105
MD5 52c02dc6d9665a98ee3c4093e57c9e20
BLAKE2b-256 aac1aabb371c701412be2f787d827f2f6534f8226ce62c3086ff5b61327dfcdd

See more details on using hashes here.

Supported by

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