Songyou commited on
Commit
05ae851
·
verified ·
1 Parent(s): b9bd342

Upload index.js

Browse files
Files changed (1) hide show
  1. index.js +35 -0
index.js ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { Ketcher } from 'ketcher-standalone';
2
+
3
+ // 导出一个初始化函数,以便在 HTML 中调用
4
+ export function initializeKetcher(containerId, binaryWasmPath) {
5
+ const ketcherInstance = new Ketcher(containerId, {
6
+ preset: 'default',
7
+ binaryWasmPath: binaryWasmPath,
8
+ });
9
+
10
+ // 设置初始 SMILES 为 "c1cccc1"
11
+ ketcherInstance.setMolecule("c1cccc1").then(() => {
12
+ console.log("初始 SMILES 加载完成");
13
+ }).catch(err => {
14
+ console.error("加载初始 SMILES 时出错:", err);
15
+ });
16
+
17
+ // 监听 Ketcher 的 'change' 事件,当分子结构改变时触发
18
+ ketcherInstance.on('change', () => {
19
+ ketcherInstance.getSmiles().then(smiles => {
20
+ console.log("生成的新 SMILES:", smiles);
21
+ // 在此处可以添加与 Gradio 后端的交互逻辑
22
+ }).catch(err => {
23
+ console.error('获取 SMILES 时出错:', err);
24
+ });
25
+ });
26
+
27
+ // 监听 Ketcher 的 'load' 事件,当分子结构加载时触发
28
+ ketcherInstance.on('load', () => {
29
+ ketcherInstance.getSmiles().then(smiles => {
30
+ console.log("加载的 SMILES:", smiles);
31
+ }).catch(err => {
32
+ console.error('获取 SMILES 时出错:', err);
33
+ });
34
+ });
35
+ }