此页面无法工作127.0.0.1未在Flask中发送任何数据错误

2024-05-17 15:03:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在构建一个Flask webapp,它使用OpenBabel API(化学工具包)为我生成一些文件。当我调用这个特定函数时,它似乎工作正常,并在我想要的目录中生成文件。但是,一旦它返回到Flask,它就会崩溃,Flask不会呈现html模板,而是将我重定向到This page isn’t working 127.0.0.1 didn’t send any data。我发现当删除函数中的代码时,它工作正常。所以OpenBabel可能有问题。Openbabel函数本身不输出任何错误,甚至在调试结束时返回

我从其他SO答案中尝试了很多东西,包括将主机更改为0.0.0.0、添加threaded=True和其他一些解决方案。都没有用。我试着调试了很长时间,但现在我迷路了,因为我已经尝试了我所知道的一切。我只能从Flask得到一个SystemExit异常。有时它能够在它之后运行print语句,但更多的时候它会立即崩溃。我不知道问题出在哪里。谢谢你给我的任何帮助。代码示例(略为缩短):

@app.route("/", methods=["POST", "GET"])
def form_handler():
    if request.method == "POST":
        smiles = request.form["smiles_molecule"]
        pdb_file = request.files["pdb_molecule"]
        no_conformers = int(request.form["no_conformers"])
        scoring = request.form["score"]
        if smiles:
            pattern = re.compile('[^A-Za-z0-9]+')
            smiles_no_special_chars = re.sub(pattern, "", smiles)
            mol_path = os.path.join(app.config["MOLECULE_UPLOADS"], smiles_no_special_chars[0:10])
            if os.path.exists(mol_path):
                shutil.rmtree(mol_path)
            os.mkdir(mol_path)
            os.chdir(mol_path)
            x = conf_gen.gen_and_write_confs(smiles, no_conformers, scoring) #<- breaks down here
            print(x)
            return render_template("index.html", mole=smiles_no_special_chars[0:10])

调用的函数:

def gen_and_write_confs(molecule, no_confs, scoring):
    
    """Generate and write the conformers to PDB. Takes mol, number of conformers and
    scoring method: RCS, MECS and ECS: OBRMSDConformerScore,
    OBMinimizingEnergyConformerScore and OBEnergyConformerScore. See OpenBabel docs."""

    mole = pybel.readstring("can", molecule)
    mole.addh()
    mole.make3D()
    mole = mole.OBMol
    mole.SetChainsPerceived(True)

    cs_obj = ob.OBConformerSearch()
    cs_obj.Setup(mole, no_confs, 5, 5, 25)
    if scoring == "RCS":
        score = ob.OBRMSDConformerScore()
    elif scoring == "MECS":
        score = ob.OBMinimizingEnergyConformerScore() 
    else:
        score = ob.OBEnergyConformerScore()
    cs_obj.SetScore(score)
    cs_obj.Search()
    cs_obj.GetConformers(mole)
    mole.DeleteNonPolarHydrogens()

    return "Test"

如果需要,我可以在Github上上传项目。它确实需要安装一些依赖项,我现在正在使用conda,但我也可以通过pip使其可用,因为OpenBabel在pip中可用

Ps:崩溃后不会显示任何错误消息


Tags: andpath函数noobjflaskrequestcs