Pass data from Flask into a seperate Javascript file


I’m running a flask application where I get information from a user’s uploaded CSV and I’m trying to take that data and pass it into a separate javascript file I have.

@app.route(‘/’) is the route that gets the users csv file and sends it to the upload,html

from flask import Flask, render_template, request, url_for, redirect
import pandas as pd

app = Flask(__name__)

@app.route('/', methods=['GET', 'POST'])
def upload():
    if request.method == 'POST':
        df = pd.read_csv(request.files.get('file'))
        return render_template('upload.html', shape=df.shape)
    return render_template('upload.html')

def graph():
    return render_template("graph.html")

if __name__ == "__main__":

This javascript builds a sankey graph, it’s connected to a css that formats it.


Highcharts.chart('container', {

    title: {
        text: 'Highcharts Sankey Diagram'
    accessibility: {
        point: {
            descriptionFormatter: function (point) {
                var index = point.index + 1,
                    from = point.from,
                    to =,
                    weight = point.weight;
                return index + '. ' + from + ' to ' + to + ', ' + weight + '.';
    series: [{
        keys: ['from', 'to', 'weight'],
        data: [
            ['Brazil', 'Portugal', 5],
            ['Brazil', 'France', 1],
            ['Brazil', 'Spain', 1],
            ['Brazil', 'England', 1],
            ['Canada', 'Portugal', 1],
            ['Canada', 'France', 5],
            ['Canada', 'England', 1],
            ['Mexico', 'Portugal', 1],
            ['Mexico', 'France', 1],
            ['Mexico', 'Spain', 5],
            ['Mexico', 'England', 1],
            ['USA', 'Portugal', 1],
            ['USA', 'France', 1],
            ['USA', 'Spain', 1],
            ['USA', 'England', 5],
            ['Portugal', 'Angola', 2],
            ['Portugal', 'Senegal', 1],
            ['Portugal', 'Morocco', 1],
            ['Portugal', 'South Africa', 3],
            ['France', 'Angola', 1],
            ['France', 'Senegal', 3],
            ['France', 'Mali', 3],
            ['France', 'Morocco', 3],
            ['France', 'South Africa', 1],
            ['Spain', 'Senegal', 1],
            ['Spain', 'Morocco', 3],
            ['Spain', 'South Africa', 1],
            ['England', 'Angola', 1],
            ['England', 'Senegal', 1],
            ['England', 'Morocco', 2],
            ['England', 'South Africa', 7],
            ['South Africa', 'China', 5],
            ['South Africa', 'India', 1],
            ['South Africa', 'Japan', 3],
            ['Angola', 'China', 5],
            ['Angola', 'India', 1],
            ['Angola', 'Japan', 3],
            ['Senegal', 'China', 5],
            ['Senegal', 'India', 1],
            ['Senegal', 'Japan', 3],
            ['Mali', 'China', 5],
            ['Mali', 'India', 1],
            ['Mali', 'Japan', 3],
            ['Morocco', 'China', 5],
            ['Morocco', 'India', 1],
            ['Morocco', 'Japan', 3]
        type: 'sankey',
        name: 'Sankey demo series'


This is the html file that currently displays the shape variable from


<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
<form method=post enctype=multipart/form-data>
    <input type=file name=file>
    <input type=submit value=Upload>
Shape is: {{ shape }}


<!DOCTYPE html>

<html lang="en" dir="ltr">
    <meta charset="utf-8">
    <title>Flask Tutorial</title>
    <figure class="highcharts-figure">
        <div id="container"></div>
        <p class="highcharts-description">
            Sankey charts are used to visualize data flow and volume
            between nodes. The wider lines indicate larger volumes.


  <script src=""></script>
  <script src=""></script>
  <script src=""></script>
  <script src=""></script>
  <script src=""></script>
  <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/template.css') }}">
  <script src="{{ url_for('static', filename='function.js') }}"></script>  




Update: The way I solved this was by simply embedding the javascript into the html, I couldn’t find a better way to do this.

Answered By – Jawsh

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published