-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_app.command
More file actions
executable file
·117 lines (102 loc) · 3.47 KB
/
Copy pathrun_app.command
File metadata and controls
executable file
·117 lines (102 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/bin/bash
set -e
cd "$(dirname "$0")"
PID_FILE=".venv/.positionsignal.pid"
PORT_FILE=".venv/.positionsignal.port"
# A second double-click reopens the existing app instead of creating a port conflict.
if [ -f "$PID_FILE" ] && [ -f "$PORT_FILE" ]; then
EXISTING_PID="$(/bin/cat "$PID_FILE")"
EXISTING_PORT="$(/bin/cat "$PORT_FILE")"
EXISTING_URL="http://127.0.0.1:${EXISTING_PORT}"
if /bin/kill -0 "$EXISTING_PID" 2>/dev/null && /usr/bin/curl -fsS "${EXISTING_URL}/_stcore/health" >/dev/null 2>&1; then
echo "PositionSignal is already running. Opening it now."
if [ "${POSITIONSIGNAL_NO_BROWSER:-0}" != "1" ]; then
/usr/bin/open "$EXISTING_URL"
fi
exit 0
fi
/bin/rm -f "$PID_FILE" "$PORT_FILE"
fi
if ! /usr/bin/env python3 -c 'import sys; raise SystemExit(0 if sys.version_info >= (3, 10) else 1)' 2>/dev/null; then
echo "PositionSignal needs Python 3.10 or newer."
echo "Install it from https://www.python.org/downloads/ and try again."
read -r -p "Press Return to close..."
exit 1
fi
if [ ! -x ".venv/bin/python" ]; then
echo "Creating PositionSignal's private Python environment..."
/usr/bin/env python3 -m venv .venv
fi
source .venv/bin/activate
export ARROW_DEFAULT_MEMORY_POOL="${ARROW_DEFAULT_MEMORY_POOL:-system}"
REQUIREMENTS_HASH="$(/usr/bin/shasum -a 256 requirements.txt | /usr/bin/awk '{print $1}')"
READY_FILE=".venv/.positionsignal-requirements-${REQUIREMENTS_HASH}"
if [ ! -f "$READY_FILE" ]; then
echo "First launch: downloading PositionSignal's Python packages. This can take a few minutes."
echo "Later launches will be much faster and can work offline."
python -m pip --disable-pip-version-check install --prefer-binary -r requirements.txt
/bin/rm -f .venv/.positionsignal-requirements-* .venv/.positionsignal-ready
/usr/bin/touch "$READY_FILE"
else
echo "Using the existing PositionSignal environment."
fi
if [ -n "${POSITIONSIGNAL_PORT:-}" ]; then
PORT="$POSITIONSIGNAL_PORT"
else
PORT="$(python - <<'PY'
import socket
for candidate in range(8501, 8600):
sock = socket.socket()
try:
sock.bind(("127.0.0.1", candidate))
except OSError:
continue
finally:
sock.close()
print(candidate)
break
else:
raise SystemExit("No free local port was found between 8501 and 8599.")
PY
)"
fi
URL="http://127.0.0.1:${PORT}"
MAX_UPLOAD_MB="${POSITIONSIGNAL_MAX_UPLOAD_MB:-200}"
echo "Starting PositionSignal at ${URL}..."
python -m streamlit run app.py \
--server.headless=true \
--server.address=127.0.0.1 \
--server.port="$PORT" \
--server.maxUploadSize="$MAX_UPLOAD_MB" \
--server.fileWatcherType=none \
--browser.gatherUsageStats=false &
APP_PID=$!
echo "$APP_PID" > "$PID_FILE"
echo "$PORT" > "$PORT_FILE"
cleanup() {
/bin/rm -f "$PID_FILE" "$PORT_FILE"
if /bin/kill -0 "$APP_PID" 2>/dev/null; then
/bin/kill "$APP_PID" 2>/dev/null || true
fi
}
trap cleanup EXIT INT TERM
ATTEMPT=1
while [ "$ATTEMPT" -le 120 ]; do
if /usr/bin/curl -fsS "${URL}/_stcore/health" >/dev/null 2>&1; then
echo "PositionSignal is ready. Opening your browser..."
if [ "${POSITIONSIGNAL_NO_BROWSER:-0}" != "1" ]; then
/usr/bin/open "$URL"
fi
wait "$APP_PID"
exit $?
fi
if ! /bin/kill -0 "$APP_PID" 2>/dev/null; then
echo "PositionSignal stopped before it became ready. Review the message above."
wait "$APP_PID"
exit $?
fi
ATTEMPT=$((ATTEMPT + 1))
/bin/sleep 0.25
done
echo "PositionSignal took too long to start. Review the message above, then try again."
exit 1