changed progressbar to tqdm

This commit is contained in:
2024-06-27 10:24:34 +02:00
committed by Flo Greistorfer
parent 238787d721
commit 14f08acc4f
2 changed files with 17 additions and 23 deletions

View File

@@ -1,6 +1,6 @@
# generate_html.py # generate_html.py
`generate_html.py` is a Python script designed to generate static HTML files for hosting images on a web server. It traverses a specified root directory, creates thumbnail previews for images, and generates corresponding HTML files to display the images and subfolders in a user-friendly format. `generate_html.py` is a Python script designed to generate static HTML files for hosting images on a web server. It traverses a specified root directory, creates thumbnail previews for images, and generates corresponding HTML files to display the images and subfolders in a user-friendly format.
## Features ## Features
@@ -14,14 +14,14 @@
- Python 3.x - Python 3.x
- `numpy` library - `numpy` library
- `alive_progress` library - `tqdm` library
## Installation ## Installation
Install the required libraries using pip: Install the required libraries using pip:
```sh ```sh
pip install numpy alive_progress pip install numpy tqdm
``` ```
## Usage ## Usage

View File

@@ -5,7 +5,7 @@ import urllib.parse
from multiprocessing import Pool from multiprocessing import Pool
from string import Template from string import Template
import numpy as np import numpy as np
from alive_progress import alive_bar from tqdm.auto import tqdm
""" """
root and webroot must point to the same folder, one on filesystem and one on the webserver. Use absolut paths, e.g. /data/pictures/ and https://pictures.example.com/ root and webroot must point to the same folder, one on filesystem and one on the webserver. Use absolut paths, e.g. /data/pictures/ and https://pictures.example.com/
@@ -152,15 +152,12 @@ HTMLHEADER = """
def thumbnail_convert(arguments: tuple[str, str]): def thumbnail_convert(arguments: tuple[str, str]):
global bar
folder, item = arguments folder, item = arguments
if not os.path.exists(os.path.join(args.root, ".previews", folder.removeprefix(args.root), item)) or args.regenerate: if not os.path.exists(os.path.join(args.root, ".previews", folder.removeprefix(args.root), os.path.splitext(item)[0] + ".jpg")) or args.regenerate:
os.system(f'magick "{os.path.join(folder, item)}" -quality 75% -define jpeg:size=1024x1024 -define jpeg:extent=100kb -thumbnail 512x512 -auto-orient "{os.path.join(args.root, ".previews", folder.removeprefix(args.root), os.path.splitext(item)[0])}.jpg"') os.system(f'magick "{os.path.join(folder, item)}" -quality 75% -define jpeg:size=1024x1024 -define jpeg:extent=100kb -thumbnail 512x512 -auto-orient "{os.path.join(args.root, ".previews", folder.removeprefix(args.root), os.path.splitext(item)[0])}.jpg"')
bar()
def listfolder(folder: str, title: str): def listfolder(folder: str, title: str):
global bar
items: list[str] = os.listdir(folder) items: list[str] = os.listdir(folder)
items.sort() items.sort()
images: list[str] = [] images: list[str] = []
@@ -218,12 +215,11 @@ def listfolder(folder: str, title: str):
else: else:
if os.path.exists(os.path.join(folder, "index.html")): if os.path.exists(os.path.join(folder, "index.html")):
os.remove(os.path.join(folder, "index.html")) os.remove(os.path.join(folder, "index.html"))
bar() pbar.update(1)
def gettotal(folder): def gettotal(folder):
global total global total
global bar
items: list[str] = os.listdir(folder) items: list[str] = os.listdir(folder)
items.sort() items.sort()
@@ -233,13 +229,12 @@ def gettotal(folder):
if os.path.isdir(os.path.join(folder, item)): if os.path.isdir(os.path.join(folder, item)):
gettotal(os.path.join(folder, item)) gettotal(os.path.join(folder, item))
total += 1 total += 1
bar() pbar.update(1)
def main(): def main():
global args global args
global bar global pbar
global total
# Parse command-line arguments # Parse command-line arguments
parser = argparse.ArgumentParser(description="Generate html files for static image host.") parser = argparse.ArgumentParser(description="Generate html files for static image host.")
parser.add_argument("-p", "--root", help="Root folder", default=_ROOT, required=False, type=str, dest="root") parser.add_argument("-p", "--root", help="Root folder", default=_ROOT, required=False, type=str, dest="root")
@@ -265,18 +260,17 @@ def main():
print("Generating thumbnails...") print("Generating thumbnails...")
p.map(thumbnail_convert, thumbnails) p.map(thumbnail_convert, thumbnails)
else: else:
print("Traversing filesystem...") pbar = tqdm(desc="Traversing filesystem", unit="folders")
with alive_bar(0, spinner="classic", bar="classic") as bar: gettotal(args.root)
gettotal(args.root) pbar.close()
print("Generating html files...") pbar = tqdm(total=total + 1, desc="Generating html files", unit="files")
with alive_bar(total, spinner="classic", bar="classic") as bar: listfolder(args.root, _ROOTTITLE)
listfolder(args.root, _ROOTTITLE) pbar.close()
with alive_bar(len(thumbnails), spinner="classic", bar="classic") as bar: with Pool(os.cpu_count()) as p:
with Pool(os.cpu_count()) as p: for r in tqdm(p.imap_unordered(thumbnail_convert, thumbnails), total=len(thumbnails), desc="Generating thumbnails", unit="files"):
print("Generating thumbnails...") ...
p.map(thumbnail_convert, thumbnails)
if __name__ == "__main__": if __name__ == "__main__":