Skip to content

Commit 779af61

Browse files
committed
Beef up Mathics3Server for 10.0.0 release
1 parent 5048631 commit 779af61

4 files changed

Lines changed: 84 additions & 16 deletions

File tree

CHANGES.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Supports Python 3.14. Python 3.10 support dropped. Python 3.10 may still work, b
1414
* Supports mathics-threejs-backend 1.3.3 (fixes a bug in point size handling) and three.js r184
1515
* Support for production ASGI server via Daphne
1616
* Reduce javascript console warnings
17+
* Mathics3Server command-line program added to run Django manage.py
1718

1819

1920
9.0.0

README.rst

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,38 +67,41 @@ From the place root directory where GitHub was checked out::
6767
Running
6868
-------
6969

70-
This is a Django project, so Django's `manage.py <https://docs.djangoproject.com/en/3.2/ref/django-admin/>`_ script is used.
70+
This is a Django project, so Django's `manage.py <https://docs.djangoproject.com/en/6.0/ref/django-admin/>`_ script is used.
7171

72-
A simple way to start Mathics3 Django when GNU make is installed (which is the case on most POSIX systems):
72+
To start the webserver:
7373

7474
::
7575

76-
make runserver
76+
Mathics3Server
7777

7878
This runs the server in development mode. Here, when changes to the source code are made, the running server detects them and reloads the modified source.
7979

8080
To run Mathics3 Django in production mode and you have the Django ASGI server `Daphne <https://pypi.org/project/daphne/>`_ installed, run::
8181

82-
make runserver-production
82+
Mathics3Server --production
8383

84-
In either case, this runs the Python program ``manage.py`` in ``mathics_django`` directory.
84+
To get a list of the available Django commands, type::
8585

86-
To get a list of the available commands, type::
86+
Mathics3Server help
8787

88-
python mathics_django/manage.py help
88+
will show options for running the Django server.
8989

90-
To get help on a specific command, give that command at the end. For example, two useful commands are the ``runserver`` and ``testserver`` commands::
90+
To get help on a specific Django command, give that command at the end. For example, two useful commands are the ``runserver`` and ``testserver`` commands::
9191

9292
python mathics_django/manage.py help runserver
9393

94-
will show options for running the Django server.
94+
To get a list of the available options for the Mathics3 Webserver, type::
95+
96+
Mathics3Server --help
9597

96-
Once the server is started, you will see a URL listed that looks like this::
98+
Once the server is started, you will see a URL listed that might look like this::
9799

98-
Starting development server at http://127.0.0.1:8000/
100+
...
101+
Starting development server at http://localhost:8000/
99102
Quit the server with CONTROL-C.
100103

101-
Point your browser to the URL listed above. Here it is ``http://127.0.0.1:8000``
104+
Point your browser to the URL listed above. Here it is ``http://localhost:8000``
102105

103106
Environment Variables
104107
+++++++++++++++++++++

mathics_django/manage.py

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,80 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
3+
import argparse
34
import os
45
import sys
56

67
from django.core.management import execute_from_command_line
78

89

910
def main():
11+
"""
12+
Runs the Mathics3 Webserver with supplied options.
13+
"""
14+
# Process options
15+
parser = argparse.ArgumentParser(
16+
prog="Mathics3Server", description="Run the Mathics3 Django webserver."
17+
)
18+
parser.add_argument(
19+
"--debug", action="store_true", help="Set DEBUG to True in settings"
20+
)
21+
parser.add_argument(
22+
"--production", action="store_true", help="Run with Daphne production server"
23+
)
24+
parser.add_argument(
25+
"-p",
26+
"--port",
27+
type=str,
28+
default="8000",
29+
help="Port to listen on (default: 8000)",
30+
)
31+
parser.add_argument(
32+
"-b",
33+
"--host",
34+
type=str,
35+
default="localhost",
36+
help="The host address to listen on (default: localhost)",
37+
)
38+
39+
# Use parse_known_args so we don't crash on standard Django commands (like 'runserver')
40+
# If --help or -h is given in sys.argv, this will print help and exit immediately.
41+
args, unknown = parser.parse_known_args()
42+
1043
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mathics_django.settings")
11-
if len(sys.argv) == 1:
12-
sys.argv.append("runserver")
13-
execute_from_command_line(sys.argv)
44+
45+
# Handle --debug option
46+
if args.debug:
47+
# This overrides settings at runtime by setting an environment variable
48+
# Ensure your mathics_django/settings.py reads DEBUG from the environment
49+
os.environ["DEBUG"] = "True"
50+
51+
# Handle --production ption
52+
if args.production:
53+
try:
54+
# Import and call Daphne's CLI directly
55+
from daphne.cli import CommandLineInterface
56+
57+
# The run() method expects a list of arguments starting with the application path
58+
# Equivalent to: daphne -b args.port -p args.host mathics_django.asgi:application
59+
CommandLineInterface().run(
60+
["mathics_django.asgi:application", "-b", args.host, "-p", args.port]
61+
)
62+
return # Exit after server stops
63+
except ImportError:
64+
print(
65+
"Error: 'daphne' is not installed. Run 'pip install daphne' for production mode."
66+
)
67+
sys.exit(1)
68+
69+
# Fallback to standard Django execution, defaulting to "runserver" commands
70+
# if --production not provided
71+
django_args = [sys.argv[0]] + unknown
72+
73+
# If no command is provided, default to runserver with our port and bind address
74+
if not unknown:
75+
django_args.extend(["runserver", f"{args.host}:{args.port}"])
76+
77+
execute_from_command_line(django_args)
1478

1579

1680
if __name__ == "__main__":

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ production = [
5555

5656

5757
[project.scripts]
58-
Mathics3server = "mathics_django.manage:main"
58+
Mathics3Server = "mathics_django.manage:main"
5959

6060
[project.urls]
6161
Homepage = "https://mathics.org/"

0 commit comments

Comments
 (0)