Skip to content

Commit bf87076

Browse files
committed
feat: modify render preview
1 parent 78bf770 commit bf87076

File tree

5 files changed

+29
-23
lines changed

5 files changed

+29
-23
lines changed

app/src/index.tsx

+8-5
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { Preview, ChartPreview } from './components/preview';
2222
import UploadSpecModal from "./components/uploadSpecModal"
2323
import UploadChartModal from './components/uploadChartModal';
2424
import InitModal from './components/initModal';
25-
import { getSaveTool, hidePreview } from './tools/saveTool';
25+
import { getSaveTool } from './tools/saveTool';
2626
import { getExportTool } from './tools/exportTool';
2727
import { getExportDataframeTool } from './tools/exportDataframe';
2828
import { formatExportedChartDatas } from "./utils/save";
@@ -64,7 +64,6 @@ const initChart = async (gwRef: React.MutableRefObject<IGWHandler | null>, total
6464
curIndex: chart.index + 1,
6565
total: chart.total,
6666
});
67-
hidePreview(props.id);
6867
}
6968
}
7069
commonStore.setInitModalOpen(false);
@@ -308,7 +307,6 @@ const initOnJupyter = async(props: IAppProps) => {
308307
comm.sendMsgAsync("request_data", {}, null);
309308
}
310309
await initDslParser();
311-
hidePreview(props.id);
312310
}
313311

314312
const initOnHttpCommunication = async(props: IAppProps) => {
@@ -382,14 +380,19 @@ function GWalker(props: IAppProps, id: string) {
382380
})
383381
}
384382

385-
function PreviewApp(props: IPreviewProps, id: string) {
383+
function PreviewApp(props: IPreviewProps, containerId: string) {
386384
props.charts = FormatSpec(props.charts.map(chart => chart.visSpec), [])
387385
.map((visSpec, index) => { return {...props.charts[index], visSpec} });
386+
387+
if (window.document.getElementById(`gwalker-${props.gid}`)) {
388+
window.document.getElementById(containerId)?.remove();
389+
}
390+
388391
ReactDOM.render(
389392
<MainApp darkMode={props.dark} hideToolBar>
390393
<Preview {...props} />
391394
</MainApp>,
392-
document.getElementById(id)
395+
document.getElementById(containerId)
393396
);
394397
}
395398

app/src/tools/saveTool.tsx

-14
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,6 @@ import type { IGWHandler } from '@kanaries/graphic-walker/interfaces';
1515
import type { ToolbarButtonItem } from "@kanaries/graphic-walker/components/toolbar/toolbar-button"
1616
import type { VizSpecStore } from '@kanaries/graphic-walker/store/visualSpecStore'
1717

18-
function saveJupyterNotebook() {
19-
const rootDocument = window.parent.document;
20-
rootDocument.body.dispatchEvent(new KeyboardEvent('keydown', {key:'s', keyCode: 83, metaKey: true}));
21-
rootDocument.body.dispatchEvent(new KeyboardEvent('keydown', {key:'s', keyCode: 83, ctrlKey: true}));
22-
}
23-
2418
function DocumentTextIconWithRedPoint(iconProps) {
2519
return (
2620
<div style={{position: "relative"}} >
@@ -30,12 +24,6 @@ function DocumentTextIconWithRedPoint(iconProps) {
3024
)
3125
}
3226

33-
export function hidePreview(id: string) {
34-
setTimeout(() => {
35-
window.parent.document.getElementById(`pygwalker-preview-${id}`)?.remove();
36-
}, 500)
37-
}
38-
3927
export function getSaveTool(
4028
props: IAppProps,
4129
gwRef: React.MutableRefObject<IGWHandler | null>,
@@ -89,10 +77,8 @@ export function getSaveTool(
8977
"chartData": await formatExportedChartDatas(chartData),
9078
"workflowList": visSpec.map((spec) => chartToWorkflow(spec))
9179
});
92-
saveJupyterNotebook();
9380
} finally {
9481
setSaving(false);
95-
hidePreview(props.id);
9682
}
9783

9884
if (["json_file", "json_ksf"].indexOf(props.specType) === -1) {

pygwalker/api/pygwalker.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ def display_on_jupyter_use_widgets(self, iframe_width: Optional[str] = None, ifr
254254

255255
display_html(html_widgets)
256256
preview_tool.init_display()
257-
preview_tool.render_gw_review(self._get_gw_preview_html())
257+
preview_tool.async_render_gw_review(self._get_gw_preview_html())
258258

259259
def display_preview_on_jupyter(self):
260260
"""
@@ -356,7 +356,7 @@ def update_spec(data: Dict[str, Any]):
356356
self.workflow_list = data.get("workflowList", [])
357357

358358
if self.use_preview:
359-
preview_tool.render_gw_review(self._get_gw_preview_html())
359+
preview_tool.async_render_gw_review(self._get_gw_preview_html())
360360

361361
save_chart_endpoint(data["chartData"])
362362

pygwalker/services/preview_image.py

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
from typing import Optional, List, Dict, Any
2+
from concurrent.futures.thread import ThreadPoolExecutor
23
import base64
34
import zlib
45
import json
56

67
from pydantic import BaseModel, Field
8+
from ipylab import JupyterFrontEnd
79

810
from pygwalker.utils.encode import DataFrameEncoder
911
from pygwalker.utils.display import display_html
@@ -106,7 +108,7 @@ def render_gw_preview_html(
106108
"data": data_base64_str
107109
})
108110

109-
props = {"charts": charts, "themeKey": theme_key, "dark": appearance}
111+
props = {"charts": charts, "themeKey": theme_key, "dark": appearance, "gid": gid}
110112

111113
container_id = f"pygwalker-preview-{gid}"
112114
template = jinja_env.get_template("index.html")
@@ -161,6 +163,11 @@ class PreviewImageTool:
161163
def __init__(self, gid: str):
162164
self.gid = gid
163165
self.image_slot_id = f"pygwalker-preview-{gid}"
166+
self.t_pool = ThreadPoolExecutor(1)
167+
try:
168+
self.command_app = JupyterFrontEnd()
169+
except Exception:
170+
self.command_app = None
164171

165172
def init_display(self):
166173
display_html("", slot_id=self.image_slot_id)
@@ -171,3 +178,12 @@ def render(self, charts_map: Dict[str, ChartData]):
171178

172179
def render_gw_review(self, html: str):
173180
display_html(html, slot_id=self.image_slot_id)
181+
182+
if self.command_app:
183+
try:
184+
self.command_app.commands.execute('docmanager:save')
185+
except Exception:
186+
pass
187+
188+
def async_render_gw_review(self, html: str):
189+
self.t_pool.submit(self.render_gw_review, html)

pyproject.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ dependencies = [
3333
"kanaries_track==0.0.5",
3434
"cachetools",
3535
"packaging",
36-
"numpy<2.0.0"
36+
"numpy<2.0.0",
37+
"ipylab<=1.0.0"
3738
]
3839
[project.urls]
3940
homepage = "https://github.com/Kanaries/pygwalker"

0 commit comments

Comments
 (0)