I want to parse file names from my app.py to graph.js. But I cannot get variable name to js file. I am using flask
Here is the file structure
- app/
- flask_app.py
- templates/
-> index.html
-> data/
-> file1.csv
- static/
-> js/
-> create-graph.js
My app looks like this (related part):
data_files = ["file1.xlsx", "file2.xlsx", "file3.xlsx"]
cur = 0
df1 = pd.read_excel("data/" + data_files[cur])
df1.to_csv('templates/data/' + data_files[cur][:10] + '.csv', encoding='utf-8', index=False)
So I have files that I convert from xlsx to csv, and then I want to pull the csv file with its name. I need to pass the data_files and cur to my .js file... Here is the related .js:
function parseData(createGraph) {
var name = '{{data_files[cur][:10]}}';
console.log(name);
Papa.parse("static/data/" + name + ".csv", {
download: true,
complete: function(results) {
createGraph(results.data);
}
});
}
But it seems that I cannot pass the file name to here: var name = '{{data_files[cur][:10]}}';
On the console it gives me the exact same string
So what I am doing wrong ?
I also tried adding this to app:
@app.route('/create-graph.js')
def script():
return render_template('create-graph.js', fileName="data_files[cur][:10]")
Still no luck...
If anyone asks, here is how I call .js file:
...
<script src="{{ url_for('static', filename='js/create-graph.js') }}"></script>
...