Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

add progress bar

[email protected]1f984e50300b
diff --git a/requirements.txt b/requirements.txt
index 8ffdc830d..a72ef3e39 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -5,6 +5,7 @@
# Application
bleach[css]~=6.1
crispy-bootstrap5==2023.10
+tqdm
# this is a fork of django-bootstrap-breadcrumbs
# we might need to think about migrating away from this completely
diff --git a/wger/core/management/commands/loaddata-progress.py b/wger/core/management/commands/loaddata-progress.py
new file mode 100644
index 000000000..9544af88a
--- /dev/null
+++ b/wger/core/management/commands/loaddata-progress.py
@@ -0,0 +1,25 @@
+from django.core.management.commands.loaddata import Command as LoadDataCommand
+from django.core.management.base import BaseCommand, CommandError
+from tqdm import tqdm # Assuming you have the tqdm library installed
+
+class Command(LoadDataCommand):
+ help = 'Load data from fixture files into the database with progress tracking.'
+
+ def add_arguments(self, parser):
+ super().add_arguments(parser)
+ parser.add_argument(
+ '--progress',
+ action='store_true',
+ dest='progress',
+ help='Show progress bar for loaddata process.',
+ )
+
+ def handle(self, *fixture_labels, **options):
+ fixture_labels = self.track_progress(fixture_labels)
+ super().handle(*fixture_labels, **options)
+
+ def track_progress(self, fixture_labels):
+ return tqdm(fixture_labels, desc='Loading fixtures', unit='fixture', ncols=80)
+
+# Usage example:
+# python manage.py custom_loaddata --progress my_fixture.json
diff --git a/wger/tasks.py b/wger/tasks.py
index dc934557a..292b8b285 100644
--- a/wger/tasks.py
+++ b/wger/tasks.py
@@ -20,6 +20,7 @@ import os
import pathlib
import sys
import tempfile
+from tqdm import tqdm
# Django
import django
@@ -275,15 +276,24 @@ def load_online_fixtures(context, settings_path=None):
print(f'Downloading fixture data from {url}...')
response = requests.get(url, stream=True)
+ total_size = int(response.headers.get("content-length", 0))
size = int(response.headers["content-length"]) / (1024 * 1024)
print(f'-> fixture size: {size:.3} MB')
# Save to temporary file and load the data
- f = tempfile.NamedTemporaryFile(delete=False, suffix='.json.zip')
- print(f'-> saving to temp file {f.name}')
- f.write(response.content)
+ with tempfile.NamedTemporaryFile(delete=False, suffix='.json.zip') as f:
+ print(f'-> saving to temp file {f.name}')
+ with tqdm(total=total_size, unit='B', unit_scale=True, desc='Downloading') as pbar:
+ for data in response.iter_content(chunk_size=1024):
+ f.write(data)
+ pbar.update(len(data))
+
+
+ # f = tempfile.NamedTemporaryFile(delete=False, suffix='.json.zip')
+ # print(f'-> saving to temp file {f.name}')
+ # f.write(response.content)
f.close()
- call_command("loaddata", f.name)
+ call_command("loaddata-progress", f.name)
print('-> removing temp file')
print('')
os.unlink(f.name)