(Bokeh3.4.1) BokehServer is unable to load /save files to disk folder -saveData located at the servers root .
BrowserError : POST http://localhost:5006/saveData 404 (Not Found)
How to get the server to recognize and read from files stored in folders on disk.
CODE:
# bokeh serve --show C:\Users\User_0\Documents\Code\Python\NseScraping\Others\bokeh\dataTable_save_2a.py --port 5006
import os
import pandas as pd
from bokeh.io import curdoc
from bokeh.layouts import column
from bokeh.models import ColumnDataSource, DataTable, TableColumn, Button, CustomJS
from bokeh.server.server import Server
from tornado.ioloop import IOLoop
from tornado.web import RequestHandler
import json
csv_file_path = 'my_data.csv'
if os.path.exists(csv_file_path):
df = pd.read_csv(csv_file_path)
else:
# Sample data if the file doesn't exist
df = pd.DataFrame({
'names': ['Csv failed to load from disk'],
})
source = ColumnDataSource(df)
columns = [
TableColumn(field="names", title="Name"),
TableColumn(field="ages", title="Age"),
]
data_table = DataTable(source=source, columns=columns, editable=True, width=400, height=280)
button = Button(label="Save as CSV", button_type="success")
button_callback = CustomJS(args=dict(source=source), code="""
const data = source.data;
const json = JSON.stringify(data);
const xhr = new XMLHttpRequest();
xhr.open("POST", "saveData", true);
xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhr.onload = function () {
if (xhr.status === 200) {
console.log("Save path:", JSON.parse(xhr.responseText).path);
} else {
console.error("Error:", JSON.parse(xhr.responseText).message);
}
};
xhr.send(json);
""")
button.js_on_click(button_callback)
layout = column(data_table, button)
curdoc().add_root(layout)
# Tornado RequestHandler to save data as CSV
class SaveCSVHandler(RequestHandler):
def post(self):
try:
data = json.loads(self.request.body.decode('utf-8'))
df = pd.DataFrame(data)
df.to_csv(csv_file_path, index=False)
self.write({'status': 'success', 'path': csv_file_path})
print(f"Data saved to {csv_file_path}")
except Exception as e:
self.write({'status': 'error', 'message': str(e)})
print(f"Error saving data: {str(e)}")
def modify_doc(doc):
doc.add_root(layout)
def start_server():
extra_patterns = [('saveData', SaveCSVHandler)]
server = Server({'/': modify_doc}, io_loop=IOLoop.current(), extra_patterns=extra_patterns, allow_websocket_origin=["localhost:5006"])
server.start()
server.io_loop.add_callback(server.show, "/")
server.io_loop.start()
if __name__ == '__main__':
start_server()