Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modal window in Jinja2 template. Flask

Currently I am working on history page for web-application. App (written on Flask) saves history in sqlite and works with db through SQLAlchemy. It looks as follow:

enter image description here

As you can see in latest cell there are a lot of text data.

More to the point, I want to list this data on history page. For now my code looks like this:

{% extends "_base.html" %}

{% block content %}

<div id="div1" class="col-md-3">
    <p><a href='/' class='btn btn-primary btn-block' role='button'><span class='glyphicon glyphicon-chevron-left'></span> Back</a></p>
</div>

<div class="bs-example col-md-12">
    <br>
    <table class="table table-hover">
        <thead>
            <tr>
                <th>Task ID</th>
                <th>Date</th>
                <th>Host</th>
                <th>User</th>
                <th>Playbook</th>
                <th>Log</th>
            </tr>
        </thead>
        <tbody>
        {% for history in histories %}
            <tr>
                <td>{{ history.task_id }}</td>
                <td>{{ history.date.strftime("%d/%m/%y %H:%M") }} UTC</td>
                <td>{{ history.hostname }}</td>
                <td>{{ history.username }}</td>
                <td>{{ history.playbook }}</td>
                <td>{{ history.output }}</td>
            </tr>
        {% endfor %}
        </tbody>
    </table>
</div>

{% endblock %}

It produces such view:

enter image description here

Obviously it looks ugly, so I've decided to hide this output (latest cell) via button with bootstrap module window, like this one

enter image description here

And at this point I have a problem. I've created next template:

{% extends "_base.html" %}

{% block content %}

<div id="div1" class="col-md-3">
    <p><a href='/' class='btn btn-primary btn-block' role='button'><span class='glyphicon glyphicon-chevron-left'></span> Back</a></p>
</div>

<div class="bs-example col-md-12">
    <br>
    <table class="table table-hover">
        <thead>
            <tr>
                <th>Task ID</th>
                <th>Date</th>
                <th>Host</th>
                <th>User</th>
                <th>Playbook</th>
                <th>Log</th>
            </tr>
        </thead>
        <tbody>
        {% for history in histories %}
            <tr>
                <td>{{ history.task_id }}</td>
                <td>{{ history.date.strftime("%d/%m/%y %H:%M") }} UTC</td>
                <td>{{ history.hostname }}</td>
                <td>{{ history.username }}</td>
                <td>{{ history.playbook }}</td>
                <td><button type="button" class="btn btn-info btn-sm" data-toggle="modal" data-target="#myOutput">Output</button></td>
            </tr>
        {% endfor %}
        </tbody>
    </table>
</div>

 <!-- Modal -->
  <div class="modal fade" id="myOutput" role="dialog">
    <div class="modal-dialog">

      <!-- Modal content-->
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h4 class="modal-title">Modal Header</h4>
        </div>
        <div class="modal-body">
          <p>Some text in the modal.</p>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
      </div>

    </div>
  </div>

{% endblock %}

But I have no clue how to add correct output to body of every modal window. Do I need to generate a lot of modal windows in code with different ids like <div class="modal fade" id="myOutput1" role="dialog">, <div class="modal fade" id="myOutput2" role="dialog">and so on? Will it be correct?

like image 343
ipeacocks Avatar asked Jun 17 '17 15:06

ipeacocks


1 Answers

I've done that with next code:

{% extends "_base.html" %}

{% block content %}

<div id="div1" class="col-md-3">
    <p><a href='/' class='btn btn-primary btn-block' role='button'><span class='glyphicon glyphicon-chevron-left'></span> Back</a></p>
</div>

<div class="bs-example col-md-12">
    <br>
    <table class="table table-hover">
        <thead>
            <tr>
                <th>Task ID</th>
                <th>Date</th>
                <th>Host</th>
                <th>User</th>
                <th>Playbook</th>
                <th>Log</th>
            </tr>
        </thead>
        <tbody>
        {% for history in histories %}
            <tr>
                <td>{{ history.task_id }}</td>
                <td>{{ history.date.strftime("%d/%m/%y %H:%M") }} UTC</td>
                <td>{{ history.hostname }}</td>
                <td>{{ history.username }}</td>
                <td>{{ history.playbook }}</td>
                <td><button type="button" class="btn btn-info btn-sm" data-toggle="modal" data-target="#myOutput{{ history.task_id }}">Output</button></td>
            </tr>
        {% endfor %}
        </tbody>
    </table>
</div>

{% for history in histories %}
 <!-- Modal -->
  <div class="modal fade" id="myOutput{{ history.task_id }}" role="dialog">
    <div class="modal-dialog modal-lg">

      <!-- Modal content-->
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h4 class="modal-title">Task Output</h4>
        </div>
        <div class="modal-body">
          <p>{{ history.output }}</p>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
      </div>

    </div>
  </div>
{% endfor %}

{% endblock %}

data-target in button and id in modal window were generated dynamically based on task_id value. I don't know if it's the best way but at least it works.

like image 185
ipeacocks Avatar answered Oct 01 '22 04:10

ipeacocks