-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathnew.py
More file actions
209 lines (178 loc) Β· 6.46 KB
/
new.py
File metadata and controls
209 lines (178 loc) Β· 6.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# Copyright 2025 Flower Labs GmbH. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Flower command line interface `new` command."""
import io
import zipfile
from pathlib import Path
from typing import Annotated, cast
import click
import requests
import typer
from flwr.supercore.constant import PLATFORM_API_URL
from flwr.supercore.utils import parse_app_spec, request_download_link
from ..archive_utils import safe_extract_zip
from ..utils import prompt_options, prompt_text
# pylint: disable=too-many-locals,too-many-branches,too-many-statements
def new(
app_spec: Annotated[
str | None,
typer.Argument(
help="Flower app specifier. Use the format "
"'@account_name/app_name' or '@account_name/app_name==x.y.z'. "
"Version is optional (defaults to latest)."
),
] = None,
framework: Annotated[
str | None,
typer.Option(case_sensitive=False, help="Deprecated. The ML framework to use"),
] = None,
username: Annotated[
str | None,
typer.Option(
case_sensitive=False, help="Deprecated. The Flower username of the author"
),
] = None,
) -> None:
"""Create new Flower App."""
if framework is not None or username is not None:
raise click.ClickException(
"The --framework and --username options are deprecated and will be "
"removed in future versions of Flower. Please provide an app specifier "
"after `flwr new` instead, e.g., '@account_name/app_name' or "
"'@account_name/app_name==x.y.z'."
)
if app_spec is None:
# Fetch recommended apps
print(
typer.style(
"\nπΈ Fetching recommended apps...",
fg=typer.colors.GREEN,
bold=True,
)
)
apps = fetch_recommended_apps()
if not apps:
typer.secho(
"No recommended apps found. Please provide an app specifier manually.",
fg=typer.colors.YELLOW,
)
app_spec = prompt_text("Please provide the app specifier")
else:
# Extract app_ids and show selection menu
app_ids = [app["app_id"] for app in apps]
app_spec = prompt_options(
"Select a Flower App to create by entering "
"the number from the list below:",
app_ids,
)
# Download remote app
download_remote_app_via_api(app_spec)
def print_success_prompt(package_name: str) -> None:
"""Print styled setup instructions for running a new Flower App after creation."""
prompt = typer.style(
"π Flower App creation successful.\n\n"
"To run your Flower App, first install its dependencies:\n\n",
fg=typer.colors.GREEN,
bold=True,
)
prompt += typer.style(
f" cd {package_name} && pip install -e .\n\n",
fg=typer.colors.BRIGHT_CYAN,
bold=True,
)
prompt += typer.style(
"then, run the app:\n\n ",
fg=typer.colors.GREEN,
bold=True,
)
prompt += typer.style(
"\tflwr run .\n\n",
fg=typer.colors.BRIGHT_CYAN,
bold=True,
)
prompt += typer.style(
"π‘ Check the README in your app directory to learn how to\n"
"customize it and how to run it using the Deployment Runtime.\n",
fg=typer.colors.GREEN,
bold=True,
)
print(prompt)
def fetch_recommended_apps() -> list[dict[str, str]]:
"""Fetch recommended apps from Platform API."""
url = f"{PLATFORM_API_URL}/hub/apps?tag=recommended"
try:
response = requests.get(url, headers={"accept": "application/json"}, timeout=10)
response.raise_for_status()
data = response.json()
apps = data.get("apps", [])
return cast(list[dict[str, str]], apps)
except requests.RequestException as e:
raise click.ClickException(f"Failed to fetch recommended apps: {e}") from e
def _download_zip_to_memory(presigned_url: str) -> io.BytesIO:
"""Download ZIP file from Platform API to memory."""
try:
r = requests.get(presigned_url, timeout=60)
r.raise_for_status()
except requests.RequestException as e:
raise click.ClickException(f"ZIP download failed: {e}") from e
buf = io.BytesIO(r.content)
# Validate it's a zip
if not zipfile.is_zipfile(buf):
raise click.ClickException("Downloaded file is not a valid ZIP")
buf.seek(0)
return buf
def download_remote_app_via_api(app_spec: str) -> None:
"""Download App from Platform API."""
# Validate app version and ID format
try:
app_id, app_version = parse_app_spec(app_spec)
except ValueError as e:
raise click.ClickException(str(e)) from e
app_name = app_id.split("/")[1]
project_dir = Path.cwd() / app_name
if project_dir.exists():
if not typer.confirm(
typer.style(
f"\n㪠{app_name} already exists, do you want to override it?",
fg=typer.colors.MAGENTA,
bold=True,
)
):
return
typer.secho(
f"\nπ Requesting download link for {app_id}...",
fg=typer.colors.GREEN,
bold=True,
)
# Fetch ZIP downloading URL
url = f"{PLATFORM_API_URL}/hub/fetch-zip"
try:
presigned_url, _ = request_download_link(app_id, app_version, url, "zip_url")
except ValueError as e:
raise click.ClickException(str(e)) from e
typer.secho(
"π½ Downloading ZIP into memory...",
fg=typer.colors.GREEN,
bold=True,
)
zip_buf = _download_zip_to_memory(presigned_url)
typer.secho(
f"π¦ Unpacking into {project_dir}...",
fg=typer.colors.GREEN,
bold=True,
)
with zipfile.ZipFile(zip_buf) as zf:
safe_extract_zip(zf, Path.cwd())
print_success_prompt(app_name)