首先,我对javascript比较陌生。所以我很抱歉如果我的问题是愚蠢的。我想用这个工具https://github.com/rdkit/rdkit-js在我的网页上画一个分子。我还在这里找到了一个例子,https://iwatobipen.wordpress.com/2021/12/29/create-desktop-chemoinformatics-application-with-js-chemoinformatics-rdkit-js/comment-page-1/。这个例子在我的例子中是可行的,但是当我试图调用一个函数将一个分子画成一个.svg,而不使用示例代码时,我失败了。我在浏览器中得到以下错误消息:
Uncaught ReferenceError: RDKit is not defined
at drawmol (results:21:15)
at results:33:5在下面的代码示例中,您可以看到第一种情况,如果它不起作用,那么第二种情况。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://unpkg.com/@rdkit/rdkit/dist/RDKit_minimal.js"></script>
<title>Document</title>
<script>
window
.initRDKitModule()
.then(function (RDKit) {
console.log("RDKit version: " + RDKit.version());
window.RDKit = RDKit;
})
.catch(() => {
});
</script>
<script> var drawmol = function() {
var mol = RDKit.get_mol("C1=CC=C(C=C1)O"); // the string here is a string representation of chemical molecules, it could also be something like "CO" or "CCCCC", shouldnt be important
var svg = mol.get_svg();
document.getElementById("drawer").innerHTML = svg;
};
</script>
</head>
<body>
<button type='button' onclick='drawmol()'> <!-- this works -->
draw
</button><br>
<script>
// drawmol() //this doesnt work
</script>
<div id='drawer'></div>
</body>
</body>
</html>稍后,我想使用该模块动态生成这些图像。我使用django作为框架。在本例中,我试图给出一个不使用django的最小示例。
提前感谢您的努力!
发布于 2022-08-12 13:16:26
在drawmol()准备好之前,您正在调用RDKit。
要解决这个问题,请在加载RDKit之后放置它:
window
.initRDKitModule()
.then(function (RDKit) {
console.log("RDKit version: " + RDKit.version());
window.RDKit = RDKit;
//Now RDKit is loaded you can safely call drawmol()
drawmol();
})
.catch(() => {
});https://stackoverflow.com/questions/73334519
复制相似问题