Improved pagination

This commit is contained in:
ManInDark
2023-08-12 17:08:33 +02:00
parent ff75adab7f
commit 680b290ae7
3 changed files with 29 additions and 10 deletions

View File

@@ -100,14 +100,14 @@ def init():
raise HTTPException(status_code=404, detail="Image not found") raise HTTPException(status_code=404, detail="Image not found")
@server_api.get("/all_images") @server_api.get("/all_images")
def get_all_images(prompt: str = "", model: str = "", page = 0, db: Session = Depends(get_db)): def get_all_images(prompt: str = "", model: str = "", page: int = 0, images_per_page: int = 50, db: Session = Depends(get_db)):
from easydiffusion.easydb.mappings import GalleryImage from easydiffusion.easydb.mappings import GalleryImage
images = db.query(GalleryImage) images = db.query(GalleryImage).order_by(GalleryImage.time_created.desc())
if prompt != "": if prompt != "":
images = images.filter(GalleryImage.path.like("%"+prompt+"%")) images = images.filter(GalleryImage.path.like("%"+prompt+"%"))
if model != "": if model != "":
images = images.filter(GalleryImage.use_stable_diffusion_model.like("%"+model+"%")) images = images.filter(GalleryImage.use_stable_diffusion_model.like("%"+model+"%"))
images = images.offset(page*50).limit(50) images = images.offset(page*images_per_page).limit(images_per_page)
return images.all() return images.all()
@server_api.get("/single_image") @server_api.get("/single_image")

View File

@@ -518,10 +518,13 @@
</div> </div>
<div id="tab-content-gallery" class="tab-content"> <div id="tab-content-gallery" class="tab-content">
<div id="gallery-search"> <div id="gallery-search">
<button class="primaryButton" onclick="decrementGalleryPage()"><i class="fa-solid fa-arrow-left"></i></button>
<textarea id="gallery-prompt-search" onkeydown="gallery_keyDown_handler(event)" placeholder="Search for a prompt..."></textarea> <textarea id="gallery-prompt-search" onkeydown="gallery_keyDown_handler(event)" placeholder="Search for a prompt..."></textarea>
<textarea id="gallery-model-search" onkeydown="gallery_keyDown_handler(event)" placeholder="Search for a model..."></textarea> <textarea id="gallery-model-search" onkeydown="gallery_keyDown_handler(event)" placeholder="Search for a model..."></textarea>
<input type="hidden" id="gallery-page" name="page" value="0"> <label for="gallery-page">Page:</label>
<button class="primaryButton" onclick="refreshGallery()">Refresh</button> <input id="gallery-page" name="Page" value="0" size="1" onkeypress="gallery_keyDown_handler(event)">
<button class="primaryButton" id="gallery-refresh" onclick="refreshGallery()">Load</button>
<button class="primaryButton" onclick="incrementGalleryPage()"><i class="fa-solid fa-arrow-right"></i></button>
</div> </div>
<div class="gallery"> <div class="gallery">
<div class="gallery-container" id="imagecontainer"></div> <div class="gallery-container" id="imagecontainer"></div>

View File

@@ -3110,7 +3110,7 @@ function galleryImage(item) {
function refreshGallery() { function refreshGallery() {
let container = document.getElementById("imagecontainer") let container = document.getElementById("imagecontainer")
params = new URLSearchParams({ let params = new URLSearchParams({
prompt: document.getElementById("gallery-prompt-search").value, prompt: document.getElementById("gallery-prompt-search").value,
model: document.getElementById("gallery-model-search").value, model: document.getElementById("gallery-model-search").value,
page: document.getElementById("gallery-page").value page: document.getElementById("gallery-page").value
@@ -3119,11 +3119,27 @@ function refreshGallery() {
fetch('/all_images?' + params) fetch('/all_images?' + params)
.then(response => response.json()) .then(response => response.json())
.then(json => { .then(json => {
console.log(json) if (document.getElementById("gallery-page").value > 0 && json.length == 0) {
decrementGalleryPage()
alert("No more images")
return
}
json.forEach(item => { json.forEach(item => {
container.appendChild(galleryImage(item)) container.appendChild(galleryImage(item))
}) })
}) })
document.getElementById("gallery-refresh").innerText = "Refresh"
}
function decrementGalleryPage() {
let page = Math.max(document.getElementById("gallery-page").value - 1, 0)
document.getElementById("gallery-page").value = page
refreshGallery()
}
function incrementGalleryPage() {
document.getElementById("gallery-page").value++
refreshGallery()
} }
function gallery_keyDown_handler(event) { function gallery_keyDown_handler(event) {