Skip to content

Commit 42f2f03

Browse files
committed
Update startup command, styling html response, and add watch files to nodemon for hot reload
1 parent 86c630f commit 42f2f03

5 files changed

Lines changed: 162 additions & 69 deletions

File tree

Dockerfile.dev

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,12 @@ ENV PORT=8000
3232
ENV HOST="0.0.0.0"
3333

3434
ENTRYPOINT ["/usr/local/bin/nodemon"]
35-
CMD ["--delay", "1", "--exec", "uv", "run", "--isolated", "src/main.py"]
35+
36+
CMD ["--delay", "1", \
37+
"--watch", "pyproject.toml", \
38+
"--watch", "requirements.txt", \
39+
"--watch", ".venv/lib/*", \
40+
"--watch", ".venv/lib64/*", \
41+
"--watch", "src", \
42+
"--ext", "py", \
43+
"--exec", "sh -c 'if [ -f pyproject.toml ]; then uv run --isolated --with . src/main.py; elif [ -f requirements.txt ]; then uv run --isolated --with-requirements requirements.txt src/main.py; else uv run --isolated src/main.py; fi'"]

README.md

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,44 @@
22

33
# Python Component for Diploi
44

5-
Uses [uv](https://docs.astral.sh/uv/) to manage Python versions & packages.
5+
[![launch with diploi badge](https://diploi.com/launch.svg)](https://diploi.com/component/python)
6+
[![component on diploi badge](https://diploi.com/component.svg)](https://diploi.com/component/python)
7+
[![latest tag badge](https://badgen.net/github/tag/diploi/component-python)](https://diploi.com/component/python)
68

79
## Operation
810

11+
### Getting started
12+
13+
1. In the Dashboard, click **Create Project +**
14+
2. Under **Pick Components**, choose **Python**. Here you can also add a frontend framework to create a monorepo app, eg, Python for backend and React+Vite for frontend
15+
3. In **Pick Add-ons**, you can add one or multiple databases to your app
16+
4. Choose **Create Repository** to generate a new GitHub repo
17+
5. Finally, click **Launch Stack**
18+
919
### Development
1020

11-
Will activate a virtual environment in `.venv` and run `uv sync` when component is first initialized, and `python src/main.py` when deployment is started.
21+
During development, the container installs Node.js and `nodemon` to enable automatic reloads when files change. The development server is started with:
22+
23+
```sh
24+
nodemon --delay 1 --watch "pyproject.toml" --watch "requirements.txt" --watch ".venv/lib/*" --watch ".venv/lib64/*" --watch "src" --ext "py" --exec "sh -c 'if [ -f pyproject.toml ]; then uv run --isolated --with . src/main.py; elif [ -f requirements.txt ]; then uv run --isolated --with-requirements requirements.txt src/main.py; else uv run --isolated src/main.py; fi'"
25+
```
26+
27+
This will:
28+
- Use `nodemon` to watch for file changes and restart the server automatically.
29+
- Run `src/main.py` in an isolated Python environment managed by `uv`.
30+
- Automatically detect whether to use `pyproject.toml` or `requirements.txt` for dependency resolution.
1231

1332
### Production
1433

15-
Will build a production ready image. Image runs `uv sync` when being created. Once the image runs, `python src/main.py` is called.
34+
Builds a production-ready image. During the build, dependencies are installed with `uv sync`. When the container starts, it runs:
35+
36+
```sh
37+
uv run --frozen src/main.py
38+
```
39+
40+
This starts your Python application using the exact dependency versions locked in the project.
41+
42+
## Links
43+
44+
- [Python documentation](https://docs.python.org/)
45+
- [uv documentation](https://docs.astral.sh/uv/)

pyproject.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,4 @@ version = "0.1.0"
44
description = "Add your description here"
55
readme = "README.md"
66
requires-python = ">=3.13"
7-
dependencies = [
8-
"numpy>=2.3.2",
9-
]
7+
dependencies = []

src/main.py

Lines changed: 119 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,130 @@
11
import http.server
22
import socketserver
33
from http import HTTPStatus
4-
import numpy as np
4+
import logging
5+
logging.basicConfig(level=logging.INFO)
56

7+
8+
logging.info("initializing Python server")
69
class Handler(http.server.SimpleHTTPRequestHandler):
710
def do_GET(self):
8-
random_value = np.random.default_rng().random()
9-
response = f'Hello world!\nRandom value: {random_value}\n'
11+
html = """
12+
<!DOCTYPE html>
13+
<html>
14+
<head>
15+
<title>Python</title>
16+
<meta name="viewport" content="width=device-width, initial-scale=1" />
17+
18+
<style>
19+
* {
20+
font-family: sans-serif;
21+
font-size: 16px;
22+
}
23+
24+
html,
25+
body {
26+
margin: 0;
27+
min-height: 100vh;
28+
background: #202328;
29+
color: #fff;
30+
}
31+
32+
body {
33+
display: flex;
34+
flex-direction: column;
35+
gap: 8px;
36+
padding: 32px;
37+
align-items: center;
38+
justify-content: center;
39+
box-sizing: border-box;
40+
}
41+
42+
h1 {
43+
font-size: 24px;
44+
}
45+
46+
p,
47+
form,
48+
hr {
49+
max-width: min(400px, 100%);
50+
}
51+
52+
p {
53+
text-align: center;
54+
opacity: 0.8;
55+
line-height: 1.5;
56+
}
57+
58+
button,
59+
.button {
60+
padding: 10px 18px;
61+
align-self: center;
62+
text-decoration: none;
63+
background: #6650fa;
64+
border-radius: 64px;
65+
border: none;
66+
color: #fff;
67+
cursor: pointer;
68+
}
69+
70+
a {
71+
font-size: inherit;
72+
color: inherit;
73+
}
74+
75+
hr {
76+
display: block;
77+
margin: 32px 0;
78+
width: 100%;
79+
height: 2px;
80+
background: #31363f;
81+
border: none;
82+
}
83+
84+
a:last-child {
85+
margin-top: 32px;
86+
}
87+
88+
code {
89+
font-family: monospace;
90+
font-size: 14px;
91+
background: #31363f;
92+
padding: 2px 4px;
93+
border-radius: 4px;
94+
}
95+
</style>
96+
97+
</head>
98+
<body>
99+
<img
100+
alt="Python logo"
101+
src="https://github.com/diploi/component-python/raw/main/.diploi/icon.svg"
102+
width="64"
103+
height="64"
104+
/>
105+
106+
<h1>Python</h1>
107+
108+
<p>
109+
Your Python application is up and running! You can start editing the code.
110+
In development stage, Python will automatically reload as you make changes.
111+
<br><br>
112+
<b> Install dependencies: </b><br>
113+
Please use <code>uv add package_name</code> to add Python packages to your environment.
114+
</p>
115+
116+
<hr />
117+
118+
<a href="https://diploi.com/"
119+
><img width="54" height="16" src="https://diploi.com/logo-white.svg"
120+
/></a>
121+
</body>
122+
</html>
123+
"""
10124
self.send_response(HTTPStatus.OK)
125+
self.send_header("Content-Type", "text/html; charset=utf-8")
11126
self.end_headers()
12-
self.wfile.write(response.encode('utf-8'))
127+
self.wfile.write(html.encode("utf-8"))
13128

14129
class Server(socketserver.TCPServer):
15130
allow_reuse_address = True

0 commit comments

Comments
 (0)