Skip to content

Commit 62c480d

Browse files
authored
Merge pull request #28 from martindurant/django
Add django project
2 parents 63c41df + 4d34518 commit 62c480d

File tree

4 files changed

+44
-4
lines changed

4 files changed

+44
-4
lines changed

docs/source/api.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ User Classes
4646
proj.pyscript.PyScript
4747
proj.rust.Rust
4848
proj.rust.RustPython
49+
proj.webapp.Django
4950
proj.webapp.Streamlit
5051
proj.uv.UvScript
5152
proj.uv.Uv
@@ -69,6 +70,7 @@ User Classes
6970
.. autoclass:: projspec.proj.pyscript.PyScript
7071
.. autoclass:: projspec.proj.rust.Rust
7172
.. autoclass:: projspec.proj.rust.RustPython
73+
.. autoclass:: projspec.proj.webapp.Django
7274
.. autoclass:: projspec.proj.webapp.Streamlit
7375
.. autoclass:: projspec.proj.uv.UvScript
7476
.. autoclass:: projspec.proj.uv.Uv

src/projspec/artifact/process.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ class Process(BaseArtifact):
99
Can include batch jobs and long-running services.
1010
"""
1111

12-
def _make(self):
12+
def _make(self, **kwargs):
1313
if self.proc is None:
14-
self.proc = subprocess.Popen(self.cmd, **self.kw)
14+
self.proc = subprocess.Popen(self.cmd, **kwargs)
1515

1616
def _is_done(self) -> bool:
1717
return self.proc is not None and self.proc.poll() is None

src/projspec/proj/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from projspec.proj.python_code import PythonCode, PythonLibrary
1313
from projspec.proj.rust import Rust, RustPython
1414
from projspec.proj.uv import Uv
15-
from projspec.proj.webapp import Streamlit
15+
from projspec.proj.webapp import Django, Streamlit
1616

1717
__all__ = [
1818
"ParseFailed",
@@ -36,6 +36,7 @@
3636
"RTD",
3737
"Rust",
3838
"RustPython",
39+
"Django",
3940
"Streamlit",
4041
"Uv",
4142
"VSCode",

src/projspec/proj/webapp.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,38 @@
1-
from projspec.proj import ProjectSpec
1+
from projspec.proj import ProjectSpec, ParseFailed
2+
3+
4+
class Django(ProjectSpec):
5+
"""A python web app using the django framework"""
6+
7+
def match(self):
8+
return "manage.py" in self.proj.basenames
9+
10+
def parse(self) -> None:
11+
from projspec.artifact.process import Server
12+
13+
# global settings are in ./*/settings.py in a directory also containing urls.py
14+
# the top-level; manage.py may have the line to locate it:
15+
# os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
16+
# and "mysite" is the suggestion in the tutorials;
17+
# can also be given as --settings to manage.py
18+
allpy = self.proj.fs.glob(f"{self.proj.url}/*/*.py")
19+
20+
# We could also choose to parse the settings or URLs - but they are required
21+
s_dirs = {_.rsplit("/", 1)[0] for _ in allpy if _.endswith("settings.py")}
22+
u_dirs = {_.rsplit("/", 1)[0] for _ in allpy if _.endswith("urls.py")}
23+
maindir = s_dirs.intersection(u_dirs)
24+
if not maindir:
25+
raise ParseFailed
26+
27+
# each site is a subdirectory with admin.py and other stuff, typically
28+
# each mapped to a different sub-URL.
29+
appdirs = [_.rsplit("/", 2)[-2] for _ in allpy if _.endswith("admin.py")]
30+
if appdirs:
31+
self.contents["apps"] = appdirs
32+
33+
self.artifacts["server"] = Server(
34+
proj=self.proj, cmd=["python", "manage.py", "runserver"]
35+
)
236

337

438
class Streamlit(ProjectSpec):
@@ -56,6 +90,9 @@ def parse(self) -> None:
5690

5791

5892
# TODO: the following are similar to streamlit, but with perhaps even less metadata
93+
# - flask (from flask import Flask; app = Flask( )
94+
# - fastapi (from fastapi import FastAPI; app = FastAPI( )
5995
# - plotly/dash (from dash import Dash; app = Dash(); app.run())
6096
# - voila (this is just a way to display a notebook)
6197
# - panel (import panel as pn; .servable())
98+
# Each of these takes extra parameters for listen address and port at least.

0 commit comments

Comments
 (0)