From f38c25f7dc9d17c7a99eb1c34da723d856c27b36 Mon Sep 17 00:00:00 2001 From: George Katsikas <giorgakis.katsikas@gmail.com> Date: Wed, 27 Nov 2024 13:03:53 +0100 Subject: [PATCH] WIP improvements to graphs --- scipost_django/graphs/graphs/__init__.py | 2 +- scipost_django/graphs/graphs/options.py | 25 +++++++++++-------- scipost_django/graphs/graphs/plotkind.py | 4 +-- .../graphs/templates/graphs/plot.html | 4 ++- 4 files changed, 21 insertions(+), 14 deletions(-) diff --git a/scipost_django/graphs/graphs/__init__.py b/scipost_django/graphs/graphs/__init__.py index 167df951c..137f60bda 100644 --- a/scipost_django/graphs/graphs/__init__.py +++ b/scipost_django/graphs/graphs/__init__.py @@ -33,7 +33,7 @@ AVAILABLE_MPL_THEMES = { import geopandas as gpd world_map_file_URL = ( - "http://naturalearth.s3.amazonaws.com/50m_cultural/ne_50m_admin_0_countries.zip" + "http://naturalearth.s3.amazonaws.com/110m_cultural/ne_110m_admin_0_countries.zip" ) BASE_WORLD = gpd.read_file(world_map_file_URL) diff --git a/scipost_django/graphs/graphs/options.py b/scipost_django/graphs/graphs/options.py index 83f7d6c80..dcffaa0ee 100644 --- a/scipost_django/graphs/graphs/options.py +++ b/scipost_django/graphs/graphs/options.py @@ -7,7 +7,7 @@ from django.forms import Field T = TypeVar("T") -Option = dict[str, T] +Options = dict[str, T] class BaseOptions: @@ -22,20 +22,25 @@ class BaseOptions: return [key for key in cls.__dict__.keys() if not key.startswith("__")] @classmethod - def get_option_fields(cls) -> Option[Field]: - option_fields: Option[Field] = {} - for option in dir(cls): - if option.startswith("__"): + def get_option_fields(cls) -> Options[Field]: + """ + Returns a dictionary of string keys and Field values, + mapping the options to their respective fields. + The keys are prefixed with the class prefix. + """ + options: Options[Field] = {} + for option_key in dir(cls): + if option_key.startswith("__"): continue - option_value = getattr(cls, option) + option_value = getattr(cls, option_key) if isinstance(option_value, Field): # Try to remove the prefix from the label if it is present if option_value.label is None: - option_value.label = cls.unprefixed(option).title() - option_fields[cls.prefix + option] = option_value + option_value.label = cls.unprefixed(option_key).title() + options[cls.prefix + option_key] = option_value - return option_fields + return options @classmethod def unprefixed(cls, key: str) -> str: @@ -44,7 +49,7 @@ class BaseOptions: return key @classmethod - def parse_prefixed_options(cls, options: Option[T]) -> Option[T]: + def parse_prefixed_options(cls, options: Options[T]) -> Options[T]: """ Returns a dictionary with only unprefixed and valid options. """ diff --git a/scipost_django/graphs/graphs/plotkind.py b/scipost_django/graphs/graphs/plotkind.py index 4d4914073..02b3817ef 100644 --- a/scipost_django/graphs/graphs/plotkind.py +++ b/scipost_django/graphs/graphs/plotkind.py @@ -37,6 +37,7 @@ class PlotKind: @classmethod def get_name(cls) -> str: + """Get the name of the plot kind in title case""" return cls.name.title() def __str__(self): @@ -63,7 +64,7 @@ class PlotKind: """ fig = self.get_figure() ax = fig.add_subplot(111) - ax.set_title(f"{self.get_name().title()} plot of {plotter.model.__name__}") + ax.set_title(f"{self.get_name()} plot of {plotter.model.__name__}") x, y = self.get_data(plotter) ax.plot(x, y) @@ -200,4 +201,3 @@ class MapPlot(PlotKind): *group_by_country_count.values_list(plotter.country_key, "count") ) return countries, count - diff --git a/scipost_django/graphs/templates/graphs/plot.html b/scipost_django/graphs/templates/graphs/plot.html index 45b49a073..339a2fc34 100644 --- a/scipost_django/graphs/templates/graphs/plot.html +++ b/scipost_django/graphs/templates/graphs/plot.html @@ -1 +1,3 @@ -{{ plot_svg|safe }} +{% if plot_svg %} + {{ plot_svg|safe }} +{% endif %} -- GitLab