是否可以拥有烧瓶应用程序和EJS网站

发布于 2025-01-25 07:02:39 字数 906 浏览 1 评论 0原文

我正在制作烧瓶应用程序,通常我会执行以下代码:

from flask import (
    Flask, 
    render_template, 
    request
)

app = Flask(__name__)

@app.route('/', methods=['GET', 'POST'])
def index():
    if request.method == 'POST':
        # print(request.form.get('hallo'))
        pass
    return render_template('index.html')


if __name__ == '__main__':
    app.run(debug=True)

但是我现在需要对EJS做同样的事情,这是可能的,我可以做:

from flask import (
    Flask, 
    render_template, 
    request
)

app = Flask(__name__)

@app.route('/', methods=['GET', 'POST'])
def index():
    if request.method == 'POST':
        # print(request.form.get('hallo'))
        pass
    return render_template('home.ejs')


if __name__ == '__main__':
    app.run(debug=True)

因为那对我不起作用。如果我不能使用烧瓶和EJS,请让我知道与EJ一起使用的是什么。

谢谢

i am making a flask app and usually i would do the following code:

from flask import (
    Flask, 
    render_template, 
    request
)

app = Flask(__name__)

@app.route('/', methods=['GET', 'POST'])
def index():
    if request.method == 'POST':
        # print(request.form.get('hallo'))
        pass
    return render_template('index.html')


if __name__ == '__main__':
    app.run(debug=True)

but i now need to do the same thing with EJS, is this possible and could i just do:

from flask import (
    Flask, 
    render_template, 
    request
)

app = Flask(__name__)

@app.route('/', methods=['GET', 'POST'])
def index():
    if request.method == 'POST':
        # print(request.form.get('hallo'))
        pass
    return render_template('home.ejs')


if __name__ == '__main__':
    app.run(debug=True)

because that doesnt work for me. IF i can't use flask and ejs please do let me know what would work with ejs.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾其所爱 2025-02-01 07:02:39

render_template直接从磁盘读取文件。

您必须将其运行为subprocess并从此过程中获取结果,然后随后将其与render_template_string(而不是render_template)或直接与返回

import subprocess

process = subprocess.run('ejs home.ejs', shell=True, capture_output=True)

html  = process.stdout.decode()
error = process.stderr.decode()

print(html)
print(error)

和类似flask

import subprocess

from flask import (
    Flask, 
    render_template, 
    render_template_string, 
    request
)

app = Flask(__name__)

@app.route('/', methods=['GET', 'POST'])
def index():

    # ...code ...
    
    process = subprocess.run('ejs home.ejs', shell=True, capture_output=True)

    html  = process.stdout.decode()
    #error = process.stderr.decode()

    return html

    # or
    
    return render_template_string(html)

if __name__ == '__main__':
    app.run(debug=True)

编辑:

更有用,可以将代码放在分离的功能中,并使用完整的路径到达ejs < /code>和使用模板文件夹,

# --- constants ---

EJS_EXE = '/full/path/to/ejs.exe'
EJS_TEMPLATES = '/full/path/to/folder/with/templates'

# --- code ---

def render_ejs(filename, params="", folder=EJS_TEMPLATES, debug=False):

    path = os.path.join(folder, filename)
    
    # to make sure I put `ejs` and `path` in `' '` because they may have spaces in full path
    process = subprocess.run(f"'{EJS_EXE}' '{path}' {params}", 
                             shell=True, capture_output=True)

    html  = process.stdout.decode()

    if debug:
        error = process.stderr.decode()
        print(error)

    return html


@app.route('/', methods=['GET', 'POST'])
def index():

    # ...code ...
    
    html = render_ejs('home.ejs')
    # or
    html = render_ejs('home.ejs', "some params")
    # or
    html = render_ejs('home.ejs', "some params", "other/folder")
    # or
    html = render_ejs('home.ejs', folder="other/folder")

    return html
    # or
    return render_template_string(html)

如果您使用render_ejs(...,debug = true)然后在控制台中它将显示ejs的错误。

render_template reads file directly from disk.

You would have to run it as subprocess and get result from this process, and later use it with render_template_string (instead of render_template) or directly with return

import subprocess

process = subprocess.run('ejs home.ejs', shell=True, capture_output=True)

html  = process.stdout.decode()
error = process.stderr.decode()

print(html)
print(error)

And similar in Flask

import subprocess

from flask import (
    Flask, 
    render_template, 
    render_template_string, 
    request
)

app = Flask(__name__)

@app.route('/', methods=['GET', 'POST'])
def index():

    # ...code ...
    
    process = subprocess.run('ejs home.ejs', shell=True, capture_output=True)

    html  = process.stdout.decode()
    #error = process.stderr.decode()

    return html

    # or
    
    return render_template_string(html)

if __name__ == '__main__':
    app.run(debug=True)

EDIT:

To make it more useful you can put code in separated function and use full path to ejs and to folder with templates

# --- constants ---

EJS_EXE = '/full/path/to/ejs.exe'
EJS_TEMPLATES = '/full/path/to/folder/with/templates'

# --- code ---

def render_ejs(filename, params="", folder=EJS_TEMPLATES, debug=False):

    path = os.path.join(folder, filename)
    
    # to make sure I put `ejs` and `path` in `' '` because they may have spaces in full path
    process = subprocess.run(f"'{EJS_EXE}' '{path}' {params}", 
                             shell=True, capture_output=True)

    html  = process.stdout.decode()

    if debug:
        error = process.stderr.decode()
        print(error)

    return html


@app.route('/', methods=['GET', 'POST'])
def index():

    # ...code ...
    
    html = render_ejs('home.ejs')
    # or
    html = render_ejs('home.ejs', "some params")
    # or
    html = render_ejs('home.ejs', "some params", "other/folder")
    # or
    html = render_ejs('home.ejs', folder="other/folder")

    return html
    # or
    return render_template_string(html)

If you use render_ejs(..., debug=True) then in console it will display error from EJS.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文