@@ -7,14 +7,15 @@ import type { VizSpecStore } from '@kanaries/graphic-walker/store/visualSpecStor
7
7
import type { IGWHandler , IViewField , ISegmentKey , IDarkMode , IChatMessage , IRow } from '@kanaries/graphic-walker/interfaces' ;
8
8
import { Tabs , TabsContent , TabsList , TabsTrigger } from "@/components/ui/tabs" ;
9
9
import { Streamlit , withStreamlitConnection } from "streamlit-component-lib"
10
+ import { createRender , useModel } from "@anywidget/react" ;
10
11
11
12
import Options from './components/options' ;
12
13
import { IAppProps } from './interfaces' ;
13
14
14
15
import { loadDataSource , postDataService , finishDataService , getDatasFromKernelBySql , getDatasFromKernelByPayload } from './dataSource' ;
15
16
16
17
import commonStore from "./store/common" ;
17
- import { initJupyterCommunication , initHttpCommunication , streamlitComponentCallback } from "./utils/communication" ;
18
+ import { initJupyterCommunication , initHttpCommunication , streamlitComponentCallback , initAnywidgetCommunication } from "./utils/communication" ;
18
19
import communicationStore from "./store/communication"
19
20
import { setConfig } from './utils/userConfig' ;
20
21
import CodeExportModal from './components/codeExportModal' ;
@@ -209,7 +210,7 @@ const ExploreApp: React.FC<IAppProps & {initChartFlag: boolean}> = (props) => {
209
210
const exportTool = getExportTool ( setExportOpen ) ;
210
211
211
212
const tools = [ exportTool ] ;
212
- if ( ( props . env === "jupyter_widgets" || props . env === "streamlit" || props . env === "gradio" ) && props . useSaveTool ) {
213
+ if ( props . env && [ "jupyter_widgets" , "streamlit" , "gradio" , "marimo" ] . indexOf ( props . env ) !== - 1 && props . useSaveTool ) {
213
214
const saveTool = getSaveTool ( props , gwRef , storeRef , isChanged , setIsChanged ) ;
214
215
tools . push ( saveTool ) ;
215
216
}
@@ -368,6 +369,16 @@ const initOnHttpCommunication = async(props: IAppProps) => {
368
369
await initDslParser ( ) ;
369
370
}
370
371
372
+ const initOnAnywidgetCommunication = async ( props : IAppProps , model : import ( "@anywidget/types" ) . AnyModel ) => {
373
+ const comm = await initAnywidgetCommunication ( props . id , model ) ;
374
+ communicationStore . setComm ( comm ) ;
375
+ if ( ( props . gwMode === "explore" || props . gwMode === "filter_renderer" ) && props . needLoadLastSpec ) {
376
+ const visSpecResp = await comm . sendMsg ( "get_latest_vis_spec" , { } ) ;
377
+ props . visSpec = visSpecResp [ "data" ] [ "visSpec" ] ;
378
+ }
379
+ await initDslParser ( ) ;
380
+ }
381
+
371
382
const defaultInit = async ( props : IAppProps ) => { }
372
383
373
384
function GWalkerComponent ( props : IAppProps ) {
@@ -568,4 +579,29 @@ const StreamlitGWalker = () => {
568
579
)
569
580
}
570
581
571
- export default { GWalker, PreviewApp, ChartPreviewApp, StreamlitGWalker }
582
+ function AnywidgetGWalkerApp ( ) {
583
+ const [ inited , setInited ] = useState ( false ) ;
584
+ const model = useModel ( ) ;
585
+ const props = JSON . parse ( model . get ( "props" ) ) as IAppProps ;
586
+ props . visSpec = FormatSpec ( props . visSpec , props . rawFields ) ;
587
+
588
+ useEffect ( ( ) => {
589
+ initOnAnywidgetCommunication ( props , model ) . then ( ( ) => {
590
+ setInited ( true ) ;
591
+ } )
592
+ } , [ ] ) ;
593
+
594
+ return (
595
+ < React . StrictMode >
596
+ { ! inited && < div > Loading...</ div > }
597
+ { inited && (
598
+ < MainApp darkMode = { props . dark } >
599
+ < GWalkerComponent { ...props } />
600
+ </ MainApp >
601
+ ) }
602
+ </ React . StrictMode >
603
+ ) ;
604
+ }
605
+
606
+
607
+ export default { GWalker, PreviewApp, ChartPreviewApp, StreamlitGWalker, render : createRender ( AnywidgetGWalkerApp ) }
0 commit comments