|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -eux |
| 4 | + |
| 5 | +export OUTPUT_DIRECTORY=../temp/typescript/react_vite |
| 6 | + |
| 7 | +mkdir -p $OUTPUT_DIRECTORY |
| 8 | +cd $OUTPUT_DIRECTORY |
| 9 | +rm -rf * |
| 10 | + |
| 11 | +function merge-json() { |
| 12 | + # merge the second json file into the first. |
| 13 | + TEMP_FILE=$(mktemp) |
| 14 | + jq '. * input' $1 $2 > TEMP_FILE && mv TEMP_FILE $1 |
| 15 | +} |
| 16 | + |
| 17 | +# 1. Create base vite project |
| 18 | +npm create vite@latest . -- --template react-ts --yes |
| 19 | + |
| 20 | +# 2. Build and run initial basic project |
| 21 | +# npm install |
| 22 | +# # npm run dev |
| 23 | +# In a web browser navigate to http://localhost:5173/ |
| 24 | + |
| 25 | +# 3. Simplify by removing some unwanted files |
| 26 | +rm src/assets/react.svg src/App.css src/index.css public/vite.svg |
| 27 | + |
| 28 | +# 4. Replace src/App.tsx with a simple hello example |
| 29 | +cat > src/App.tsx << EOF |
| 30 | +function App() { |
| 31 | + return ( |
| 32 | + <> |
| 33 | + <div>Hello</div> |
| 34 | + </> |
| 35 | + ) |
| 36 | +} |
| 37 | +
|
| 38 | +export default App |
| 39 | +EOF |
| 40 | + |
| 41 | +# 5. Remove CSS lines from src/main.tsx by replacing it |
| 42 | +cat > src/main.tsx << EOF |
| 43 | +import { StrictMode } from 'react' |
| 44 | +import { createRoot } from 'react-dom/client' |
| 45 | +import App from './App.tsx' |
| 46 | +
|
| 47 | +createRoot(document.getElementById('root')!).render( |
| 48 | + <StrictMode> |
| 49 | + <App /> |
| 50 | + </StrictMode>, |
| 51 | +) |
| 52 | +EOF |
| 53 | + |
| 54 | +# 6. Build and run the minimal example |
| 55 | +# # npm run dev |
| 56 | +# In a web browser navigate to http://localhost:5173/ |
| 57 | + |
| 58 | +# 7. 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. |
| 59 | +npm install ../../../../bokeh-bokehjs-3.8.0-dev.1.tgz |
| 60 | + |
| 61 | +# 8. Create a new file src/components/BokehComponent.tsx containing a BokehJS plot component |
| 62 | +mkdir -p src |
| 63 | +cat > src/BokehComponent.tsx << EOF |
| 64 | +import { useEffect, useRef } from 'react' |
| 65 | +import * as Bokeh from "@bokeh/bokehjs"; |
| 66 | +
|
| 67 | +console.info("BokehJS version:", Bokeh.version); |
| 68 | +
|
| 69 | +function create_bokehjs_plot(): Bokeh.Column { |
| 70 | + const source = new Bokeh.ColumnDataSource({data: { x: [0.1, 0.9], y: [0.1, 0.9], size: [40, 10] }}); |
| 71 | +
|
| 72 | + const plot = Bokeh.Plotting.figure({ |
| 73 | + title: "Example BokehJS plot", height: 500, width: 500, |
| 74 | + x_range: [0, 1], y_range: [0, 1], sizing_mode: "stretch_width", |
| 75 | + }); |
| 76 | +
|
| 77 | + plot.scatter({ field: "x" }, { field: "y" }, {source, size: { field: "size" }}); |
| 78 | +
|
| 79 | + const button = new Bokeh.Widgets.Button({label: "Click me to add a point", button_type: "primary"}); |
| 80 | + function button_callback() { |
| 81 | + const data = source.data as any; |
| 82 | + data.x.push(Math.random()); |
| 83 | + data.y.push(Math.random()); |
| 84 | + data.size.push(10 + Math.random()*30); |
| 85 | + source.change.emit(); |
| 86 | + } |
| 87 | + button.on_click(button_callback); |
| 88 | +
|
| 89 | + return new Bokeh.Column({children: [plot, button], sizing_mode: "stretch_width"}); |
| 90 | +} |
| 91 | +
|
| 92 | +export function BokehComponent() { |
| 93 | + const shown = useRef(false); |
| 94 | + useEffect(() => { |
| 95 | + if (!shown.current) { |
| 96 | + Bokeh.Plotting.show(create_bokehjs_plot(), "#target"); |
| 97 | + shown.current = true; |
| 98 | + } |
| 99 | + }, []) |
| 100 | +
|
| 101 | + return ( |
| 102 | + <> |
| 103 | + <div id="target"></div> |
| 104 | + </> |
| 105 | + ) |
| 106 | +} |
| 107 | +EOF |
| 108 | + |
| 109 | +# 9. Replace src/App.tsx so that it uses the BokehComponent |
| 110 | +cat > src/App.tsx << EOF |
| 111 | +import { BokehComponent } from './BokehComponent.tsx' |
| 112 | +
|
| 113 | +function App() { |
| 114 | + return ( |
| 115 | + <> |
| 116 | + <BokehComponent /> |
| 117 | + </> |
| 118 | + ) |
| 119 | +} |
| 120 | +
|
| 121 | +export default App |
| 122 | +EOF |
| 123 | + |
| 124 | +# 10. Rebuild and serve |
| 125 | +# npm run dev |
| 126 | +# In a web browser navigate to http://localhost:5173/ |
0 commit comments