Skip to content

Commit e06a3c2

Browse files
authored
Merge pull request #59 from chazlarson/new-image-picker
Add alphabet picker for all items in the library between library and items
2 parents 75afae8 + 38810a8 commit e06a3c2

3 files changed

Lines changed: 150 additions & 2 deletions

File tree

Plex Image Picker/app.py

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,72 @@ def libraries():
3737
return render_template("libraries.html", sections=sections)
3838

3939

40-
# http://127.0.0.1:5000/browse/5?page=1&art_type=poster&art_page=1&season=3&episode=
40+
# new paginated item picker
41+
@app.route("/browse/<section_key>/items")
42+
def list_items(section_key):
43+
plex = get_plex()
44+
if not plex:
45+
return redirect(url_for("connect"))
46+
47+
# find the right library section
48+
section = next(
49+
(s for s in plex.library.sections() if str(s.key) == section_key),
50+
None,
51+
)
52+
if not section:
53+
flash("Library not found.")
54+
return redirect(url_for("libraries"))
55+
56+
# full list of items
57+
all_items_full = list(section.all())
58+
59+
# give everything a global index
60+
global_index = 1
61+
for item in all_items_full:
62+
item.global_index = global_index
63+
global_index = global_index + 1
64+
65+
# optional letter filter (defaults to 'All')
66+
letter = request.args.get("letter", "All")
67+
if letter != "All":
68+
if letter[:1].isdigit():
69+
items_filtered = [
70+
item
71+
for item in all_items_full
72+
if item.title and item.title[:1].isdigit()
73+
]
74+
else:
75+
items_filtered = [
76+
item
77+
for item in all_items_full
78+
if item.title and item.title.upper().startswith(letter.upper())
79+
]
80+
else:
81+
items_filtered = all_items_full
82+
83+
# pagination params (based on filtered set)
84+
total = len(items_filtered)
85+
per_page = 20
86+
pages = math.ceil(total / per_page)
87+
88+
# clamp page number
89+
page = int(request.args.get("page", 1))
90+
page = max(1, min(page, pages))
91+
92+
# slice out this page from filtered items
93+
start = (page - 1) * per_page
94+
page_items = items_filtered[start : start + per_page]
95+
96+
return render_template(
97+
"items.html",
98+
section=section,
99+
items=page_items,
100+
page=page,
101+
pages=pages,
102+
per_page=per_page,
103+
alphabet=ALPHABET,
104+
letter=letter,
105+
)
41106

42107

43108
@app.route("/browse/<section_key>")
@@ -54,6 +119,7 @@ def browse(section_key):
54119
items = list(section.all())
55120
pages = len(items)
56121
item_page = int(request.args.get("page", 1))
122+
item_letter = request.args.get("letter", "All")
57123
item_page = max(1, min(item_page, pages))
58124
item = items[item_page - 1]
59125

@@ -74,6 +140,7 @@ def browse(section_key):
74140
season_rating_key=season_rating_key,
75141
items=items,
76142
item_page=item_page,
143+
item_letter=item_letter,
77144
pages=pages,
78145
art_type=art_type,
79146
season=season,

Plex Image Picker/templates/item.html

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
<!-- templates/item.html -->
22
{% extends 'base.html' %}
33
{% block content %}
4-
<h2>{{ section.title }}: {{ item.title }}</h2>
4+
5+
<!-- http://127.0.0.1:5000/browse/1/items?page=1&letter=O -->
6+
<!-- item_page -->
7+
<!-- item_letter -->
8+
9+
<h2><a href="{{ url_for('list_items', section_key=section.key, page=item_page, letter=item_letter) }}">{{ section.title }}</a>: {{ item.title }}</h2>
510
<div class="d-flex justify-content-between mb-3">
611
<div>
712
<a href="{{ url_for('browse', section_key=section.key, page=1) }}" class="btn btn-outline-secondary btn-sm">First Item</a>
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
{% extends 'base.html' %}
2+
3+
{% block content %}
4+
<h2>{{ section.title }} – Pick an Item</h2>
5+
6+
{# ——— Alphabet picker with index‑based active highlighting ——— #}
7+
{% set current_index = 0 %}
8+
{% if letter != 'All' %}
9+
{# Find the position of the selected letter in the alphabet list (0‑based). #}
10+
{% set current_index = alphabet.index(letter) + 1 %}
11+
{% endif %}
12+
13+
<div class="btn-group mb-3" role="group" aria-label="Alphabet filter">
14+
{% for loop_letter in ['All'] + alphabet %}
15+
{% set idx = loop.index0 %}
16+
<a href="{{ url_for('list_items',
17+
section_key=section.key,
18+
page=1,
19+
letter=loop_letter) }}"
20+
class="btn btn-sm btn-outline-secondary{% if idx == current_index %} active{% endif %}">
21+
{{ loop_letter }}
22+
</a>
23+
{% endfor %}
24+
</div>
25+
26+
<ul class="list-group mb-4">
27+
{% for item in items %}
28+
<li class="list-group-item">
29+
{# calculate the global index for browse → item_page #}
30+
{% set global_index = (page - 1) * per_page + loop.index %}
31+
<a href="{{ url_for('browse',
32+
section_key=section.key,
33+
page=item.global_index,
34+
letter=letter) }}">
35+
{{ item.title }}
36+
</a>
37+
</li>
38+
{% endfor %}
39+
</ul>
40+
41+
<nav aria-label="Page navigation">
42+
<ul class="pagination">
43+
<li class="page-item{% if page <= 1 %} disabled{% endif %}">
44+
<a class="page-link"
45+
href="{{ url_for('list_items',
46+
section_key=section.key,
47+
page=page-1,
48+
letter=letter) }}">
49+
Previous
50+
</a>
51+
</li>
52+
53+
{% for p in range(1, pages+1) %}
54+
<li class="page-item{% if p == page %} active{% endif %}">
55+
<a class="page-link"
56+
href="{{ url_for('list_items',
57+
section_key=section.key,
58+
page=p,
59+
letter=letter) }}">
60+
{{ p }}
61+
</a>
62+
</li>
63+
{% endfor %}
64+
65+
<li class="page-item{% if page >= pages %} disabled{% endif %}">
66+
<a class="page-link"
67+
href="{{ url_for('list_items',
68+
section_key=section.key,
69+
page=page+1,
70+
letter=letter) }}">
71+
Next
72+
</a>
73+
</li>
74+
</ul>
75+
</nav>
76+
{% endblock %}

0 commit comments

Comments
 (0)