Skip to content

Commit

Permalink
Enable dynamic templates from repo
Browse files Browse the repository at this point in the history
  • Loading branch information
tanertopal committed Apr 5, 2024
1 parent 1336aa9 commit 34d3755
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions src/py/flwr/cli/new/new.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down

0 comments on commit 34d3755

Please sign in to comment.