From 34d37554bc4ca97179eb8593736bb255f785487c Mon Sep 17 00:00:00 2001 From: Taner Topal Date: Fri, 5 Apr 2024 16:28:26 +0200 Subject: [PATCH] Enable dynamic templates from repo --- src/py/flwr/cli/new/new.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/py/flwr/cli/new/new.py b/src/py/flwr/cli/new/new.py index 7eb47e3e3548..b281a015cfc0 100644 --- a/src/py/flwr/cli/new/new.py +++ b/src/py/flwr/cli/new/new.py @@ -15,6 +15,7 @@ """Flower command line interface `new` command.""" import os +import urllib.request from enum import Enum from string import Template from typing import Dict, Optional @@ -37,16 +38,17 @@ class TemplateNotFound(Exception): """Raised when template does not exist.""" -def load_template(name: str) -> str: - """Load template from template directory and return as text.""" - tpl_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), "templates")) - tpl_file_path = os.path.join(tpl_dir, name) +templates_base_url = "https://raw.githubusercontent.com/adap/flower/main/src/py/flwr/cli/new/templates" - if not os.path.isfile(tpl_file_path): - raise TemplateNotFound(f"Template '{name}' not found") - with open(tpl_file_path, encoding="utf-8") as tpl_file: - return tpl_file.read() +def load_template(name: str) -> str: + """Load template from template directory and return as text.""" + try: + with urllib.request.urlopen(f"{templates_base_url}/{name}") as res: + content = res.read().decode("utf-8") + return content + except Exception as ex: + raise TemplateNotFound(f"Template '{name}' not found") from ex def render_template(template: str, data: Dict[str, str]) -> str: