Skip to content

Commit 0e74019

Browse files
v1: Flet CLI: Add flet serve command to run HTTP server with WASM support (#5441)
* Flet CLI: Add `flet serve` command to run HTTP server with WASM support * Make web_root as a positional argument * Rename and update Flet CLI documentation Replaces old CLI command docs (build, create, doctor, pack, publish, run) with new files prefixed by 'flet-' and updates their content for improved clarity and completeness. Updates all internal references and navigation to use the new filenames and command names. Also updates build.py to recommend 'flet serve' for serving web apps instead of Python's http.server. * `flet serve` to support `-p` option
1 parent e4a096a commit 0e74019

File tree

26 files changed

+759
-367
lines changed

26 files changed

+759
-367
lines changed

sdk/python/packages/flet-cli/src/flet_cli/cli.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import flet_cli.commands.pack
99
import flet_cli.commands.publish
1010
import flet_cli.commands.run
11+
import flet_cli.commands.serve
1112
from flet.version import update_version
1213

1314

@@ -76,6 +77,7 @@ def main():
7677
flet_cli.commands.build.Command.register_to(sp, "build")
7778
flet_cli.commands.pack.Command.register_to(sp, "pack")
7879
flet_cli.commands.publish.Command.register_to(sp, "publish")
80+
flet_cli.commands.serve.Command.register_to(sp, "serve")
7981
flet_cli.commands.doctor.Command.register_to(
8082
sp, "doctor"
8183
) # Register the doctor command

sdk/python/packages/flet-cli/src/flet_cli/commands/build.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -665,8 +665,7 @@ def handle(self, options: argparse.Namespace) -> None:
665665
f"Find it in [cyan]{self.rel_out_dir}[/cyan] directory. "
666666
f"{self.emojis['directory']}"
667667
+ (
668-
f"\nRun [cyan]python -m http.server --directory "
669-
f"{self.rel_out_dir}[/cyan] command to start dev web server "
668+
"\nRun [cyan]flet serve[/cyan] command to start a web server "
670669
"with your app. "
671670
if self.options.target_platform == "web"
672671
else ""
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import argparse
2+
import http.server
3+
import socketserver
4+
from pathlib import Path
5+
6+
from flet_cli.commands.base import BaseCommand
7+
from rich.console import Console
8+
from rich.style import Style
9+
10+
error_style = Style(color="red1", bold=True)
11+
console = Console(log_path=False)
12+
13+
14+
class CustomHandler(http.server.SimpleHTTPRequestHandler):
15+
def __init__(self, *args, directory=None, **kwargs):
16+
super().__init__(*args, directory=directory, **kwargs)
17+
18+
def end_headers(self):
19+
self.send_header("Cross-Origin-Opener-Policy", "same-origin")
20+
self.send_header("Cross-Origin-Embedder-Policy", "require-corp")
21+
self.send_header("Access-Control-Allow-Origin", "*")
22+
super().end_headers()
23+
24+
25+
class Command(BaseCommand):
26+
"""
27+
Serve static files from a directory with optional WASM headers.
28+
"""
29+
30+
def add_arguments(self, parser: argparse.ArgumentParser) -> None:
31+
parser.add_argument(
32+
"web_root",
33+
type=str,
34+
nargs="?",
35+
help="directory to serve (default: ./build/web)",
36+
default="build/web",
37+
)
38+
parser.add_argument(
39+
"-p",
40+
"--port",
41+
type=int,
42+
default=8000,
43+
help="Port to serve on (default: 8000)",
44+
)
45+
46+
def handle(self, options: argparse.Namespace) -> None:
47+
directory = Path(options.web_root).resolve()
48+
if not directory.is_dir():
49+
console.print(
50+
f"Error: Directory '{directory}' does not exist or is not a folder.",
51+
style=error_style,
52+
)
53+
exit(1)
54+
55+
def handler(*args, **kwargs):
56+
return CustomHandler(
57+
*args,
58+
directory=str(directory),
59+
**kwargs,
60+
)
61+
62+
try:
63+
with socketserver.TCPServer(("", options.port), handler) as httpd:
64+
console.print(
65+
f"Serving [green]{directory}[/green] at [cyan]http://localhost:{options.port}[/cyan] (Press Ctrl+C to stop)\n"
66+
)
67+
httpd.serve_forever()
68+
except KeyboardInterrupt:
69+
console.print("\n[bold yellow]Server stopped by user.[/bold yellow]")
70+
except OSError as e:
71+
console.print(f"Error: {e}", style=error_style)
72+
exit(1)

sdk/python/packages/flet/docs/cli/build.md

Lines changed: 0 additions & 172 deletions
This file was deleted.

sdk/python/packages/flet/docs/cli/create.md

Lines changed: 0 additions & 24 deletions
This file was deleted.

sdk/python/packages/flet/docs/cli/doctor.md

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)