I have a CSV file of readings from a sensor that I would like to graph with chart.js on a flask server, however I am getting 404 errors when trying to access the CSV file, since I am getting 404 errors I am assuming that I need to actually path the CSV file in flask, but I cant figure out how to do this, I'm coming across a lot of irrelevant information about using flask to make and fill a database but I need to pass the database to an html page to parse for graphing.
Could someone please help me learn how to do this?
Code that I'm testing and playing around with for reading CSV files in JavaScript. Has the route specified in flask code below!
<script>
//path is from html file location to db location is this right?
d3.csv("../db/test.csv", function(data) {
for (var i = 0; i < data.length; i++) {
console.log(data[i].Test1);
console.log(data[i].Test2);
console.log(data[i].Test3);
}
});
</script>
Test CSV file.
Test1, Test2, Test3
1, 2, 3
Any help to better understand how to do this is much appreciated!
forgot my flask code!
@app.route('/test')
def test():
#every thing that links the csv to flask would go here? Or would I need a more specific function for the csv file?
return render_template('test.html'), 201
I FOUND IT!!! Here is the article I used in case anyone ever has a similar issue!
https://medium.com/@rovai/from-data-to-graph-a-web-jorney-with-flask-and-sqlite-6c2ec9c0ad0
flask code
#Retrieve data from db (logs/log_db/sht.#.db ; # = run_id = SHT30_#.log, #)
def getData():
conn = sqlite3.connect('logs/log_db/sht.%s.db' %run_id)
cur = conn.cursor()
for row in cur.execute('''SELECT Itn, Temp, Humi FROM READING'''):
Itn = str(row[0])
Temp = str(row[1])
Humi = str(row[2])
conn.close()
return Itn, Temp, Humi
#main route
@app.route("/")
def index():
Itn, Temp, Humi = getData()
templateData = {
'Itn': Itn,
'Temp': Temp,
'Humi': Humi
}
return(render_template('index.html', **templateData))
In my index.html i used {{ variables }} to display the data on the page.