...
This commit is contained in:
60
pythonsetup/README.md
Normal file
60
pythonsetup/README.md
Normal file
@@ -0,0 +1,60 @@
|
||||
# Python Example setup
|
||||
|
||||
This repository contains the setup scripts and configuration files for how we use python in threefold/ourworld
|
||||
|
||||
```bash
|
||||
./install.sh
|
||||
```
|
||||
|
||||
This script will:
|
||||
|
||||
1. Check if `uv` is installed (a fast Python package installer and resolver)
|
||||
2. Install `uv` if it's not found
|
||||
3. Initialize a uv project if `pyproject.toml` doesn't exist
|
||||
4. Sync all project dependencies using `uv sync`
|
||||
5. Install the herolib package from git repository
|
||||
6. Create necessary directories: (remove if you don't need this for your project)
|
||||
- `static/css`, `static/js`, `static/images` for static assets
|
||||
- `templates` for HTML templates
|
||||
- `md` for markdown files
|
||||
|
||||
check how we install herolib here
|
||||
- `pip install git+https://github.com/ThreeFoldTech/herolib.git` and also have support for the local checked out version
|
||||
|
||||
## Running the Server
|
||||
|
||||
### Production Mode
|
||||
|
||||
To start the server in production mode, run:
|
||||
|
||||
```bash
|
||||
./start_server.sh
|
||||
```
|
||||
|
||||
This script will:
|
||||
1. Set environment variables for production
|
||||
2. Check and free port 9922 if it's in use
|
||||
3. Start the web server on port 9922
|
||||
4. Make the server available at http://localhost:9922
|
||||
|
||||
### Development/Debug Mode
|
||||
|
||||
To start the server in development/debug mode, run:
|
||||
|
||||
```bash
|
||||
./start_server_debug.sh
|
||||
```
|
||||
|
||||
This script will:
|
||||
1. Set environment variables for development
|
||||
2. Enable debug mode
|
||||
3. Check and free port 9922 if it's in use
|
||||
4. Start the web server on port 9922 with debug options
|
||||
5. Make the server available at http://localhost:9922
|
||||
|
||||
## Environment Setup
|
||||
|
||||
The `pipenv.sh` script is automatically sourced by the startup scripts and handles:
|
||||
1. Setting the PYTHONPATH to include the src directory
|
||||
2. Creating a virtual environment with uv if it doesn't exist
|
||||
3. Activating the virtual environment
|
58
pythonsetup/install.sh
Executable file
58
pythonsetup/install.sh
Executable file
@@ -0,0 +1,58 @@
|
||||
#!/bin/bash
|
||||
|
||||
# KnowledgeCenter Web Server Installation Script
|
||||
# This script sets up the necessary environment for the Flask web server.
|
||||
|
||||
set -e # Exit on any error
|
||||
|
||||
# Colors for output
|
||||
RED='\033[0;31m'
|
||||
GREEN='\033[0;32m'
|
||||
YELLOW='\033[1;33m'
|
||||
BLUE='\033[0;34m'
|
||||
NC='\033[0m' # No Color
|
||||
|
||||
# Script directory
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
cd "$SCRIPT_DIR"
|
||||
|
||||
echo -e "${BLUE}🔧 Setting up KnowledgeCenter Web Server Environment${NC}"
|
||||
echo "=================================================="
|
||||
|
||||
# Check if uv is installed
|
||||
if ! command -v uv &> /dev/null; then
|
||||
echo -e "${YELLOW}⚠️ uv is not installed. Installing uv...${NC}"
|
||||
curl -LsSf https://astral.sh/uv/install.sh | sh
|
||||
source $HOME/.cargo/env
|
||||
echo -e "${GREEN}✅ uv installed${NC}"
|
||||
fi
|
||||
|
||||
echo -e "${GREEN}✅ uv found${NC}"
|
||||
|
||||
# Initialize uv project if not already done
|
||||
if [ ! -f "pyproject.toml" ]; then
|
||||
echo -e "${YELLOW}⚠️ No pyproject.toml found. Initializing uv project...${NC}"
|
||||
uv init --no-readme --python 3.13
|
||||
echo -e "${GREEN}✅ uv project initialized${NC}"
|
||||
fi
|
||||
|
||||
# Sync dependencies
|
||||
echo -e "${YELLOW}📦 Installing dependencies with uv...${NC}"
|
||||
uv sync
|
||||
if [ -d "$HOME/code/git.ourworld.tf/herocode/herolib_python/herolib" ]; then
|
||||
echo -e "${GREEN}✅ Found local herolib, installing...${NC}"
|
||||
uv pip install -e "$HOME/code/git.ourworld.tf/herocode/herolib_python"
|
||||
else
|
||||
echo -e "${YELLOW}📦 Local herolib not found, installing from git...${NC}"
|
||||
uv pip install herolib@git+https://git.ourworld.tf/herocode/herolib_python.git --force-reinstall --no-cache-dir
|
||||
fi
|
||||
echo -e "${GREEN}✅ Dependencies installed${NC}"
|
||||
|
||||
# Create necessary directories
|
||||
mkdir -p static/css static/js static/images
|
||||
mkdir -p templates
|
||||
mkdir -p md
|
||||
|
||||
echo -e "${GREEN}✅ Directory structure verified${NC}"
|
||||
|
||||
echo -e "${GREEN}🎉 Installation complete! You can now run start_server.sh${NC}"
|
22
pythonsetup/pipenv.sh
Executable file
22
pythonsetup/pipenv.sh
Executable file
@@ -0,0 +1,22 @@
|
||||
#!/bin/bash
|
||||
export PYTHONPATH=$PYTHONPATH:$(pwd)/src
|
||||
|
||||
|
||||
# Get the directory where this script is located
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
cd "$SCRIPT_DIR"
|
||||
|
||||
echo "Setting up KnowledgeCenter environment in: $SCRIPT_DIR"
|
||||
|
||||
# Create virtual environment if it doesn't exist
|
||||
if [ ! -d ".venv" ]; then
|
||||
echo "📦 Creating Python virtual environment..."
|
||||
uv venv
|
||||
echo "✅ Virtual environment created"
|
||||
else
|
||||
echo "✅ Virtual environment already exists"
|
||||
fi
|
||||
|
||||
# Activate virtual environment
|
||||
echo "🔄 Activating virtual environment..."
|
||||
source .venv/bin/activate
|
23
pythonsetup/pyproject.toml
Normal file
23
pythonsetup/pyproject.toml
Normal file
@@ -0,0 +1,23 @@
|
||||
[project]
|
||||
name = "KnowledgeCenter"
|
||||
version = "0.1.0"
|
||||
description = "Add your description here"
|
||||
requires-python = ">=3.13"
|
||||
dependencies = [
|
||||
"beautifulsoup4>=4.13.4",
|
||||
"flask>=2.3.3",
|
||||
"markdown>=3.5.1",
|
||||
"Werkzeug>=3.1.3",
|
||||
"peewee>=3.17.0",
|
||||
"pygments>=2.16.1",
|
||||
"toml",
|
||||
"flask_socketio",
|
||||
"eventlet",
|
||||
"fastapi>=0.104.0",
|
||||
"uvicorn>=0.24.0",
|
||||
"python-multipart>=0.0.6",
|
||||
"requests>=2.31.0",
|
||||
"herolib @ git+https://git.ourworld.tf/herocode/herolib_python.git",
|
||||
"pudb",
|
||||
"ipython"
|
||||
]
|
70
pythonsetup/start_server.sh
Executable file
70
pythonsetup/start_server.sh
Executable file
@@ -0,0 +1,70 @@
|
||||
#!/bin/bash
|
||||
|
||||
# KnowledgeCenter Web Server Startup Script
|
||||
# This script starts the Flask web server on port 9922 for PRODUCTION
|
||||
|
||||
set -e # Exit on any error
|
||||
|
||||
# Colors for output
|
||||
RED='\033[0;31m'
|
||||
GREEN='\033[0;32m'
|
||||
YELLOW='\033[1;33m'
|
||||
BLUE='\033[0;34m'
|
||||
NC='\033[0m' # No Color
|
||||
|
||||
# Script directory
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
cd "$SCRIPT_DIR"
|
||||
|
||||
source pipenv.sh
|
||||
|
||||
echo -e "${BLUE}🚀 KnowledgeCenter Web Server Startup (PRODUCTION)${NC}"
|
||||
echo "================================================="
|
||||
|
||||
# Check if uv is installed
|
||||
# Check if port 9922 is available
|
||||
if lsof -Pi :9922 -sTCP:LISTEN -t >/dev/null 2>&1; then
|
||||
echo -e "${YELLOW}⚠️ Port 9922 is already in use. Attempting to stop existing process...${NC}"
|
||||
PID=$(lsof -ti:9922)
|
||||
if [ ! -z "$PID" ]; then
|
||||
kill -9 $PID 2>/dev/null || true
|
||||
sleep 2
|
||||
echo -e "${GREEN}✅ Existing process stopped${NC}"
|
||||
fi
|
||||
fi
|
||||
|
||||
# Set environment variables for production
|
||||
export FLASK_APP=src/app.py
|
||||
export FLASK_ENV=production
|
||||
export FLASK_DEBUG=0
|
||||
|
||||
# Display startup information
|
||||
echo ""
|
||||
echo -e "${BLUE}📋 Server Information:${NC}"
|
||||
echo " • Application: KnowledgeCenter Interest Registration"
|
||||
echo " • Port: 9922"
|
||||
echo " • URL: http://localhost:9922"
|
||||
echo " • Environment: Production"
|
||||
echo " • Debug Mode: Disabled"
|
||||
echo ""
|
||||
|
||||
# Function to handle cleanup on exit
|
||||
cleanup() {
|
||||
echo -e "\n${YELLOW}🛑 Shutting down server...${NC}"
|
||||
exit 0
|
||||
}
|
||||
|
||||
# Set trap for cleanup
|
||||
trap cleanup SIGINT SIGTERM
|
||||
|
||||
# Start the Flask development server
|
||||
echo -e "${GREEN}🌟 Starting KnowledgeCenter web server...${NC}"
|
||||
echo -e "${BLUE}📡 Server will be available at: http://localhost:9922${NC}"
|
||||
echo -e "${YELLOW}💡 Press Ctrl+C to stop the server${NC}"
|
||||
echo ""
|
||||
|
||||
# Start the server with uv, specifying production config
|
||||
uv run python src/app.py --cfg prod --port 9922 --host 0.0.0.0
|
||||
|
||||
# This line should not be reached unless the server exits
|
||||
echo -e "${RED}❌ Server stopped unexpectedly${NC}"
|
70
pythonsetup/start_server_debug.sh
Executable file
70
pythonsetup/start_server_debug.sh
Executable file
@@ -0,0 +1,70 @@
|
||||
#!/bin/bash
|
||||
|
||||
# KnowledgeCenter Web Server Startup Script
|
||||
# This script starts the Flask web server on port 9922 for PRODUCTION
|
||||
|
||||
set -e # Exit on any error
|
||||
|
||||
# Colors for output
|
||||
RED='\033[0;31m'
|
||||
GREEN='\033[0;32m'
|
||||
YELLOW='\033[1;33m'
|
||||
BLUE='\033[0;34m'
|
||||
NC='\033[0m' # No Color
|
||||
|
||||
# Script directory
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
cd "$SCRIPT_DIR"
|
||||
|
||||
source pipenv.sh
|
||||
|
||||
echo -e "${BLUE}🚀 KnowledgeCenter Web Server Startup (DEVELOPMENT/DEBUG)${NC}"
|
||||
echo "================================================="
|
||||
|
||||
# Check if uv is installed
|
||||
# Check if port 9922 is available
|
||||
if lsof -Pi :9922 -sTCP:LISTEN -t >/dev/null 2>&1; then
|
||||
echo -e "${YELLOW}⚠️ Port 9922 is already in use. Attempting to stop existing process...${NC}"
|
||||
PID=$(lsof -ti:9922)
|
||||
if [ ! -z "$PID" ]; then
|
||||
kill -9 $PID 2>/dev/null || true
|
||||
sleep 2
|
||||
echo -e "${GREEN}✅ Existing process stopped${NC}"
|
||||
fi
|
||||
fi
|
||||
|
||||
# Set environment variables for production
|
||||
export FLASK_APP=src/app.py
|
||||
export FLASK_ENV=development
|
||||
export FLASK_DEBUG=1
|
||||
|
||||
# Display startup information
|
||||
echo ""
|
||||
echo -e "${BLUE}📋 Server Information:${NC}"
|
||||
echo " • Application: KnowledgeCenter Interest Registration"
|
||||
echo " • Port: 9922"
|
||||
echo " • URL: http://localhost:9922"
|
||||
echo " • Environment: Development"
|
||||
echo " • Debug Mode: Enabled"
|
||||
echo ""
|
||||
|
||||
# Function to handle cleanup on exit
|
||||
cleanup() {
|
||||
echo -e "\n${YELLOW}🛑 Shutting down server...${NC}"
|
||||
exit 0
|
||||
}
|
||||
|
||||
# Set trap for cleanup
|
||||
trap cleanup SIGINT SIGTERM
|
||||
|
||||
# Start the Flask development server
|
||||
echo -e "${GREEN}🌟 Starting KnowledgeCenter web server...${NC}"
|
||||
echo -e "${BLUE}📡 Server will be available at: http://localhost:9922${NC}"
|
||||
echo -e "${YELLOW}💡 Press Ctrl+C to stop the server${NC}"
|
||||
echo ""
|
||||
|
||||
# Start the server with uv, specifying production config
|
||||
uv run python src/app.py --cfg dev --port 9922 --host 0.0.0.0 --debug
|
||||
|
||||
# This line should not be reached unless the server exits
|
||||
echo -e "${RED}❌ Server stopped unexpectedly${NC}"
|
Reference in New Issue
Block a user