1

I am deploying my machine learning model locally and use flask for that. I have my main.html and app.py but when I try to run it in the console I get the Error message above.

I cannot find any "&" in both files which is why I cannot solve the problem.

I checked all files but could not find a char like "&" and the debug mode just raises the error which I mentioned.

This is the html form:

<!doctype html>
<html>
<style>
form {
    margin: auto;
    width: 35;
}
.result {
    margin: auto;
    width: 35;
    border: 1px solid 
}
</style>
<head>
    <title>Sales Prediction</title>
</head>
<form action="{{url_for('main')}}" method="POST">
    <fieldset>
        <legend>Input values:</legend>
        Week:
        <input name="week" type="number" required>
        <br>
        <br> Grad:
        <input name="grad" type="number" required>
        <br>
        <br> Preis:
        <input name="preis" type="number" required>
        <br>
        <br>
        <input type="submit">
    </fieldset>
</form>
<br>
<div class="result" align="center">
    {%if result%}
        {%for variable, value in original_input.items()%}
            <b>{{variable}}</b> : {{value}}
        {%endfor%}
        <br>
        <br> Predicted number of sales :
            <p style="font-size:50px">{{result}}</p>
    {%endif%}
</div>
</html>

and this is the app.py:

import flask
import pickle
import pandas as pd

with open('model/model.pkl', 'rb') as f:
    model = pickle.load(f)

app = flask.Flask(__name__, template_folder='templates')
@app.route('/', methods=['GET', 'POST'])
def main():
    if flask.request.method == 'GET':
        return(flask.render_template('main.html'))
    if flask.request.method == 'POST':
        week = flask.request.form['week']
        grad = flask.request.form['grad']
        preis = flask.request.form['preis']
        input_variables = pd.DataFrame([[week, grad, preis]],
                                       columns=['week', 'grad', 'preis'],
                                       dtype=float)
        prediction = model.predict(input_variables)[0]
        return flask.render_template('main.html',
                                     original_input={'week':week,
                                                     'grad':grad,
                                                     'preis':preis},
                                     result=prediction,
                                     )
if __name__ == '__main__':
    app.debug = True
    app.run(host = '0.0.0.0', port = 5000)

As soon as I run it in the console I get

jinja2.exceptions.TemplateSyntaxError: unexpected char '&' at 250601

2
  • 1
    Can you add the full stack trace?
    – djnz
    Jun 25, 2019 at 21:50
  • What do you mean with full stack trace? @dylanj.nz
    – Agostino
    Jun 26, 2019 at 5:34

3 Answers 3

5

There needs to be space separating the Jinja delimiters ({{, {%, etc.) from the code inside them.

<form action="{{ url_for('main') }}" method=post>
    <fieldset>
        <legend>Input values:</legend>
        Week:
        <input name="week" type="number" required>
        <br>
        <br> Grad:
        <input name="grad" type="number" required>
        <br>
        <br> Preis:
        <input name="preis" type="number" required>
        <br>
        <br>
        <input type="submit">
    </fieldset>
</form>
<br>
<div class="result" align="center">
    {% if result %}
        {% for variable, value in original_input.items() %}
            <b>{{ variable }}</b> : {{ value }}
        {% endfor %}
        <br>
        <br> Predicted number of sales :
            <p style="font-size:50px">{{ result }}</p>
    {% endif %}
</div>
7
  • Hey, thank you so much for your help und the support. Unfortunately it is not working and I am getting the same error. Do you have another idea? :)
    – Agostino
    Jun 25, 2019 at 20:22
  • If you could add the error message then I can check.
    – ParthS007
    Jun 25, 2019 at 20:23
  • It is still this here: jinja2.exceptions.TemplateSyntaxError: unexpected char '&' at 250602 I can send you the files too if this might help
    – Agostino
    Jun 25, 2019 at 20:24
  • Do you have base.html or any other HTML/Jinja template file?
    – ParthS007
    Jun 25, 2019 at 20:29
  • It is a base.html file. Downloaded it directly from Jupyter Notebook.
    – Agostino
    Jun 25, 2019 at 20:33
1

I received this error because my text was being escaped before being sent to Jinja. For example, it seemed that this particular text snippet was being sent to Jinja:

Vous pouvez vous connecter à {{track_url(account_url,'sign_in')|shorten_url}} 

but this was actually what was sent:

Vous pouvez vous connecter à {{track_url(account_url,&#x27;sign_in&#x27;)|shorten_url}}

The & in the &#x27; triggered the error.

Once I replaced the escaped apostrophes with actual apostrophes, the error went away.

In your case, perhaps the {{url_for('main')}} is being escaped, which would result in something like {{url_for(&#27;main&#27;)}} or {{url_for(&apos;main&apos;)}} causing the error.

0

Sometime you can get this error with the @ symbol instead of the & symbol.

In my case I had some ASP.Net code in my flask app and the ampersand was actually the problem in the HTML file:

<p class="nav navbar-text navbar-right">Hello, {{ @User.Identity.GetLogin()! }}</p>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.