Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable dynamic templates to be pulled from the repository #3229

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions src/py/flwr/cli/new/new.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
"""Flower command line interface `new` command."""

import os
import urllib.request
from enum import Enum
from string import Template
from typing import Dict, Optional
from typing import Dict, Optional, cast

import typer
from typing_extensions import Annotated
Expand All @@ -37,16 +38,19 @@ 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 cast(str, 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