我将 Flask 用于我的后端,并将 brython 用于我的客户端,因为我需要使用不适用于 javascript 的 python 模块。目前,我在从不同文件夹导入模块时遇到问题。例如,假设我想在 brython 中导入 test.py。我的文件目录如下:
根
——app.py
- 模板
——index.html——test.py
_
我也尝试将 test.py 与 __init__.py 文件一起放入根目录,但没有运气。示例代码如下。
索引.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://cdnjs.cloudflare.com/ajax/libs/brython/3.8.8/brython.js" integrity="sha256-rA89wPrTJJQFWJaZveKW8jpdmC3t5F9rRkPyBjz8G04=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/brython/3.8.8/brython_stdlib.js" integrity="sha256-Gnrw9tIjrsXcZSCh/wos5Jrpn0bNVNFJuNJI9d71TDs=" crossorigin="anonymous"></script>
<title>{{ title }}</title>
</head>
<body onload="brython()">
<h3>Testing Brython with Flask</h3>
<div id="test">
</div>
<script type="text/python">
from browser import document
import test
value = test.do_math(4,6)
output = 'my number is '+(str(value))
document['test'] <= output
</script>
</body>
</html>
测试.py
def do_math(m,n):
return m+n
应用程序.py
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def home():
title="Home"
return render_template("index.html", title=title)
if __name__=='__main__':
app.run(debug=True)