Skip to content

Commit

Permalink
Merge pull request #204 from zacbrannelly/enhance/pagination
Browse files Browse the repository at this point in the history
ENHANCE: Pagination for Experiment Explorer
  • Loading branch information
ucokzeko authored Nov 19, 2019
2 parents 93ad747 + 965033e commit b188b18
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 2 deletions.
64 changes: 63 additions & 1 deletion surround/experiment/web/experiment_explorer.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ <h1 class="title">Experiment Explorer</h1>
</div>
<div class="level-right">
<div class="level-item">
<a class="button" href="./experiment?project_name={{project['project_name']}}">
<a class="button" href="./experiment?project_name={{project['project_name']}}&page={{page}}">
<span class="icon is-small"><i class="fas fa-sync"></i></span>
</a>
</div>
Expand Down Expand Up @@ -76,6 +76,68 @@ <h1 class="title">Experiment Explorer</h1>
{% end %}
</tbody>
</table>
{% if len(pages) > 1 %}
<hr class="hr" />
<nav class="pagination is-centered" role="navigation" aria-label="pagination">
{% if page - 1 > 0 %}
<a class="pagination-previous" href="./experiment?project_name={{project['project_name']}}&page={{page - 1}}">Previous</a>
{% end %}
{% if page + 1 <= pages[-1] %}
<a class="pagination-next" href="./experiment?project_name={{project['project_name']}}&page={{page + 1}}">Next page</a>
{% end %}
<ul class="pagination-list">
<li><a class="pagination-link {{'is-current' if page == pages[0] else ''}}" aria-label="Goto page {{pages[0]}}" href="./experiment?project_name={{project['project_name']}}">{{pages[0]}}</a></li>
{% if len(pages) > 2 %}
<li><span class="pagination-ellipsis">&hellip;</span></li>
{% if page == pages[0] %}
<li><a class="pagination-link" aria-label="Goto page {{page + 1}}" href="./experiment?project_name={{project['project_name']}}&page={{page + 1}}">{{page + 1}}</a></li>
{% if page + 2 in pages and page + 2 != pages[-1] %}
<li><a class="pagination-link" aria-label="Goto page {{page + 2}}" href="./experiment?project_name={{project['project_name']}}&page={{page + 2}}">{{page + 2}}</a></li>
{% end %}
{% if page + 3 in pages and page + 3 != pages[-1] %}
<li><a class="pagination-link" aria-label="Goto page {{page + 3}}" href="./experiment?project_name={{project['project_name']}}&page={{page + 3}}">{{page + 3}}</a></li>
{% end %}
{% elif page == pages[0] + 1 %}
<li><a class="pagination-link is-current" aria-label="Goto page {{page}}" href="./experiment?project_name={{project['project_name']}}&page={{page}}">{{page}}</a></li>
{% if page + 1 in pages and page + 1 != pages[-1] %}
<li><a class="pagination-link" aria-label="Goto page {{page + 1}}" href="./experiment?project_name={{project['project_name']}}&page={{page + 1}}">{{page + 1}}</a></li>
{% end %}
{% if page + 2 in pages and page + 2 != pages[-1] %}
<li><a class="pagination-link" aria-label="Goto page {{page + 2}}" href="./experiment?project_name={{project['project_name']}}&page={{page + 2}}">{{page + 2}}</a></li>
{% end %}
{% elif page == pages[-1] %}
{% if page - 3 in pages and page - 3 != pages[0] %}
<li><a class="pagination-link" aria-label="Goto page {{page - 3}}" href="./experiment?project_name={{project['project_name']}}&page={{page - 3}}">{{page - 3}}</a></li>
{% end %}
{% if page - 2 in pages and page - 2 != pages[0] %}
<li><a class="pagination-link" aria-label="Goto page {{page - 2}}" href="./experiment?project_name={{project['project_name']}}&page={{page - 2}}">{{page - 2}}</a></li>
{% end %}
<li><a class="pagination-link" aria-label="Goto page {{page - 1}}" href="./experiment?project_name={{project['project_name']}}&page={{page - 1}}">{{page - 1}}</a></li>
{% elif page == pages[-2] %}
{% if page - 2 in pages and page - 2 != pages[0] %}
<li><a class="pagination-link" aria-label="Goto page {{page - 2}}" href="./experiment?project_name={{project['project_name']}}&page={{page - 2}}">{{page - 2}}</a></li>
{% end %}
{% if page - 1 in pages and page - 1 != pages[0] %}
<li><a class="pagination-link" aria-label="Goto page {{page - 1}}" href="./experiment?project_name={{project['project_name']}}&page={{page - 1}}">{{page - 1}}</a></li>
{% end %}
<li><a class="pagination-link is-current" aria-label="Goto page {{page}}" href="./experiment?project_name={{project['project_name']}}&page={{page}}">{{page}}</a></li>
{% else %}
{% if page - 1 > 0 %}
<li><a class="pagination-link" aria-label="Goto page {{page - 1}}" href="./experiment?project_name={{project['project_name']}}&page={{page - 1}}">{{page - 1}}</a></li>
{% end %}
{% if page != pages[0] %}
<li><a class="pagination-link is-current" aria-label="Page {{page}}" aria-current="page">{{page}}</a></li>
{% end %}
{% if page + 1 <= pages[-1] %}
<li><a class="pagination-link" aria-label="Goto page {{page + 1}}" href="./experiment?project_name={{project['project_name']}}&page={{page + 1}}">{{page + 1}}</a></li>
{% end %}
{% end %}
<li><span class="pagination-ellipsis">&hellip;</span></li>
{% end %}
<li><a class="pagination-link {{'is-current' if page == pages[-1] else ''}}" aria-label="Goto page {{pages[-1]}}" href="./experiment?project_name={{project['project_name']}}&page={{pages[-1]}}">{{pages[-1]}}</a></li>
</ul>
</nav>
{% end %}
</div>

<div id="deleteAreYouSure" class="modal">
Expand Down
23 changes: 22 additions & 1 deletion surround/experiment/web/experiment_explorer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import math
import tornado.web

class ExperimentExplorer(tornado.web.RequestHandler):
Expand All @@ -7,6 +8,13 @@ def initialize(self, experiment_reader):

def get(self):
project_name = self.get_argument("project_name", default=None)
page = self.get_argument("page", default="1")

try:
page = int(page)
except ValueError:
self.redirect("./", permanent=True)
return

if not project_name:
self.redirect("./", permanent=True)
Expand All @@ -17,4 +25,17 @@ def get(self):
experiments = self.experiment_reader.list_experiments(project_name)
experiments = sorted(experiments, reverse=True)

self.render(os.path.join(os.path.dirname(__file__), "experiment_explorer.html"), project_names=projects, project=project, experiments=experiments)
if experiments:
num_per_page = 10
pages = list(range(1, math.ceil(len(experiments) / num_per_page) + 1))

if page not in pages:
page = pages[-1]

if len(experiments) > num_per_page:
index = (page - 1) * num_per_page
experiments = experiments[index:index + num_per_page]
else:
pages = [1]

self.render(os.path.join(os.path.dirname(__file__), "experiment_explorer.html"), project_names=projects, project=project, experiments=experiments, page=page, pages=pages)

0 comments on commit b188b18

Please sign in to comment.