-
Create initial
package.json
(npm
project settings)npm init --yes
-
Install dev dependencies
npm install --save-dev typescript webpack webpack-cli webpack-dev-server ts-node ts-loader
-
Create typescript configuration
tsconfig.json
containing{ "compilerOptions": { "baseUrl": ".", "esModuleInterop": true, "moduleResolution": "node", "outDir": "./dist", "rootDir": "./src", "target": "ES2022" }, "include": ["src"] }
-
Create webpack configuration
webpack.config.ts
containingimport path from 'path'; import webpack from 'webpack'; import 'webpack-dev-server'; const config: webpack.Configuration = { entry: './src/index.ts', mode: 'development', module: { rules: [ { test: /\.ts/, use: "ts-loader", exclude: /node_modules/ } ], }, output: { filename: 'bundle.js' }, devServer: { static: { directory: path.join(__dirname, 'assets'), }, port: 4500, }, }; export default config;
-
Create HTML file
assets/index.html
containing<!DOCTYPE html> <html> <head> <title>BokehJS example: typescript vanilla webpack</title> <script src="bundle.js"></script> </head> <body> <div id="target"></div> </body> </html>
-
Create source typescript file
src/index.ts
containingconsole.log("Successfully loaded")
-
Add
build
andserve
commands to thescripts
section ofpackage.json
{ "scripts": { "build": "webpack build", "serve": "webpack serve" } }
-
Build and run basic example without any BokehJS
npm install npm run build npm run serve
In a web browser navigate to http://localhost:4500/
-
Add BokehJS dependency to this project. This assumes the package has been built and copied to the root directory of this repository as outlined in the top-level
README.md
.npm install ../../../../bokeh-bokehjs-3.8.0-dev.1.tgz
-
Replace contents of
src/index.ts
with code to create BokehJS plot containingimport * as Bokeh from "@bokeh/bokehjs"; console.info("BokehJS version:", Bokeh.version); function create_bokehjs_plot(): Bokeh.Column { const source = new Bokeh.ColumnDataSource({data: { x: [0.1, 0.9], y: [0.1, 0.9], size: [40, 10] }}); const plot = Bokeh.Plotting.figure({ title: "Example BokehJS plot", height: 500, width: 500, x_range: [0, 1], y_range: [0, 1], sizing_mode: "stretch_width", }); plot.scatter({ field: "x" }, { field: "y" }, {source, size: { field: "size" }}); const button = new Bokeh.Widgets.Button({label: "Click me to add a point", button_type: "primary"}); function button_callback() { const data = source.data as any; data.x.push(Math.random()); data.y.push(Math.random()); data.size.push(10 + Math.random()*30); source.change.emit(); } button.on_click(button_callback); return new Bokeh.Column({children: [plot, button], sizing_mode: "stretch_width"}); } Bokeh.Plotting.show(create_bokehjs_plot(), "#target");
-
Rebuild and serve
npm install npm run build npm run serve
In a web browser navigate to http://localhost:4500/