-1

I have this list, generated in Python file (Google Colab)

pitch_dataset = 
['0', 'G#3 / Ab3', 'A#3 / Bb3', 'B3', 'C4', 'C#4 / Db4', 'D4', 'D#4 / Eb4', 'E4', 'F4', 'F#4 / Gb4', 'G4']

I want to print this list without [] or ' ' inside my HTML output page.

0, G#3 / Ab3, A#3 / Bb3, B3, C4, C#4 / Db4, D4, D#4 / Eb4, E4, F4, F#4 / Gb4, G4]

However, by using this code:

<div class="form-group row">
            <label for="detected_pitch" class="col-sm-2 col-form-label">Pitch from dataset: </label>
            <div class="col-sm-10" style="padding-top: 8px">                
                {% for i in pitch_dataset: %}
                    {% print(i, end=", ") %}
                {% endfor %}
            </div>
</div>

I got this error

[2021-03-26 14:14:12,584] ERROR in app: Exception on /usertest [POST]
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/flask/app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.7/dist-packages/flask/app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python3.7/dist-packages/flask/app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.7/dist-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/usr/local/lib/python3.7/dist-packages/flask/app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python3.7/dist-packages/flask/app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "<ipython-input-35-e88c3eaabc23>", line 4, in no_cache
    response = make_response(view(*args, **kwargs))
  File "<ipython-input-56-21918cccddbd>", line 42, in usertest
    return render_template('/usertest.html', song_title=song_title, page_title=page_title, pitch_dataset=pitch_dataset, pitch_class=pitch_class, distance=distance)
  File "/usr/local/lib/python3.7/dist-packages/flask/templating.py", line 138, in render_template
    ctx.app.jinja_env.get_or_select_template(template_name_or_list),
  File "/usr/local/lib/python3.7/dist-packages/jinja2/environment.py", line 930, in get_or_select_template
    return self.get_template(template_name_or_list, parent, globals)
  File "/usr/local/lib/python3.7/dist-packages/jinja2/environment.py", line 883, in get_template
    return self._load_template(name, self.make_globals(globals))
  File "/usr/local/lib/python3.7/dist-packages/jinja2/environment.py", line 857, in _load_template
    template = self.loader.load(self, name, globals)
  File "/usr/local/lib/python3.7/dist-packages/jinja2/loaders.py", line 127, in load
    code = environment.compile(source, name, filename)
  File "/usr/local/lib/python3.7/dist-packages/jinja2/environment.py", line 638, in compile
    self.handle_exception(source=source_hint)
  File "/usr/local/lib/python3.7/dist-packages/jinja2/environment.py", line 832, in handle_exception
    reraise(*rewrite_traceback_stack(source=source))
  File "/usr/local/lib/python3.7/dist-packages/jinja2/_compat.py", line 28, in reraise
    raise value.with_traceback(tb)
  File "/content/./drive/MyDrive/1117002_Code Skripsi/FlaskUI/templates/usertest.html", line 23, in template
    {% print(i, end=", ") %}
jinja2.exceptions.TemplateSyntaxError: expected token ')', got '='

Is there any other way to do that? I've tried all codes found in here but none of them is working well.

2 Answers2

2

What if you convert the list to a string?

pitch_dataset_string = ', '.join(pitch_dataset)

And in the template you do something like this:

<div class="form-group row">
            <label for="detected_pitch" class="col-sm-2 col-form-label">Pitch from dataset: </label>
            <div class="col-sm-10" style="padding-top: 8px">                
                {{ pitch_dataset_string }}
            </div>
</div>
Andrea Barnabò
  • 534
  • 6
  • 19
2

The correct version is:

<div class="form-group row">
    <label for="detected_pitch" class="col-sm-2 col-form-label">Pitch from dataset: </label>
        <div class="col-sm-10" style="padding-top: 8px">                
            {% for i in pitch_dataset %}
                {{ i }}{% if not loop.last %}, {% endif %}
            {% endfor %}
        </div>
</div>

Note that Jinja2 is not Python, although inspired by it. Take a look at the documentation: https://jinja.palletsprojects.com/en/2.11.x/templates/

tobias
  • 291
  • 2
  • 9