|
1 | | -# sample/__main__.py |
2 | | - |
3 | | -from . import __version__ |
4 | | -import sys |
5 | 1 | import logging |
6 | | -import subprocess |
7 | 2 | from pathlib import Path |
8 | | -import click |
9 | | - |
10 | | -logging.basicConfig(level=logging.INFO) |
11 | | - |
| 3 | +from http.server import SimpleHTTPRequestHandler |
| 4 | +from socketserver import TCPServer |
| 5 | +import os |
12 | 6 |
|
13 | | -@click.group(invoke_without_command=True) |
14 | | -@click.option("--version", is_flag=True, help="Show the Sample version and exit.") |
15 | | -@click.pass_context |
16 | | -def cli(ctx, version): |
17 | | - """Sample command-line tools.""" |
18 | | - if version: |
19 | | - click.echo(__version__) |
20 | | - ctx.exit() |
21 | | - |
22 | | - |
23 | | -@cli.command() |
24 | | -def dev(): |
25 | | - """Run the Sample Streamlit app.""" |
26 | | - main() |
27 | | - |
28 | | - |
29 | | -@cli.command() |
30 | | -def api(): |
31 | | - """Run the Sample FastAPI backend.""" |
32 | | - from api.fast_api import start |
33 | | - |
34 | | - start() |
| 7 | +__version__ = "0.1.0" |
| 8 | +PORT = 8000 |
35 | 9 |
|
36 | 10 |
|
37 | 11 | def main(): |
38 | | - """ |
39 | | - Entrypoint for the Streamlit 'dev' app. |
40 | | - """ |
41 | 12 | print("🏷️ Sample version:", __version__) |
42 | | - logging.info("Starting sample dev script...") |
| 13 | + logging.info("Starting static HTML server...") |
43 | 14 |
|
44 | | - # Paths |
45 | | - Sample_dir = Path(__file__).resolve().parent |
46 | | - dev_root = Sample_dir.parent # src/ |
47 | | - wheel_root = Sample_dir.parent # same in wheel |
| 15 | + sample_dir = Path(__file__).resolve().parent.parent |
48 | 16 |
|
49 | | - # Add correct root to sys.path |
50 | | - if "site-packages" in str(Sample_dir): # running from wheel |
51 | | - if str(wheel_root) not in sys.path: |
52 | | - sys.path.append(str(wheel_root)) |
53 | | - logging.info(f"Added wheel root to sys.path: {wheel_root}") |
54 | | - else: # dev mode |
55 | | - if str(dev_root) not in sys.path: |
56 | | - sys.path.append(str(dev_root)) |
57 | | - logging.info(f"Added dev src root to sys.path: {dev_root}") |
| 17 | + if "site-packages" in str(sample_dir): |
| 18 | + root = sample_dir |
| 19 | + logging.info("Running from wheel") |
| 20 | + else: |
| 21 | + root = sample_dir.parent |
| 22 | + logging.info("Running in dev mode") |
58 | 23 |
|
59 | | - # Locate streamlit_app.py |
60 | | - streamlit_app_path = Sample_dir / "streamlit_app.py" |
61 | | - logging.info(f"Streamlit app path: {streamlit_app_path}") |
| 24 | + logging.info(f"Serving from root: {root}") |
62 | 25 |
|
63 | | - if not streamlit_app_path.exists(): |
64 | | - logging.error(f"Streamlit app not found at: {streamlit_app_path}") |
65 | | - return |
| 26 | + os.chdir(root) |
66 | 27 |
|
67 | | - # Run Streamlit app |
68 | | - python_path = sys.executable |
69 | | - logging.info(f"Using Python executable: {python_path}") |
70 | | - |
71 | | - subprocess.run( |
72 | | - [ |
73 | | - python_path, |
74 | | - "-m", |
75 | | - "streamlit", |
76 | | - "run", |
77 | | - str(streamlit_app_path.resolve()), |
78 | | - "--server.port", |
79 | | - "8501", |
80 | | - ], |
81 | | - check=True, |
82 | | - ) |
| 28 | + with TCPServer(("", PORT), SimpleHTTPRequestHandler) as httpd: |
| 29 | + print(f"🌐 Open http://localhost:{PORT}/templates/index.html") |
| 30 | + httpd.serve_forever() |
83 | 31 |
|
84 | 32 |
|
85 | 33 | if __name__ == "__main__": |
86 | | - cli() |
| 34 | + main() |
0 commit comments