'is it possible to have a flask app and an ejs website
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
Solution 1:[1]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source | 
|---|---|
| Solution 1 | 
