Skip to content

TencentEdgeOne/python-handler-template

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

5 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Python Cloud Functions on EdgeOne Pages - Handler Mode

A function request demonstration website based on Next.js + Tailwind CSS, showcasing how to deploy Python Cloud Functions using Handler Mode on EdgeOne Pages with file-based routing.

πŸš€ Features

  • File-Based Routing: Python handler files map directly to API endpoints, just like Next.js file routing
  • Modern UI Design: Adopts black background with white text theme, using #1c66e5 as accent color
  • Real-time API Demo: Integrated Python backend with interactive API call testing for all route types
  • Multiple Route Patterns: Supports static, index, dynamic, nested dynamic, and catch-all routes
  • TypeScript Support: Complete type definitions and type safety

πŸ› οΈ Tech Stack

Frontend

  • Next.js 15 - React full-stack framework
  • React 19 - User interface library
  • TypeScript - Type-safe JavaScript
  • Tailwind CSS 4 - Utility-first CSS framework

UI Components

  • Lucide React - Beautiful icon library
  • class-variance-authority - Component style variant management
  • clsx & tailwind-merge - CSS class name merging utilities

Backend

  • Python 3.9+ - Cloud Functions runtime
  • Handler Mode - File-based routing for Python functions on EdgeOne Pages

πŸ“ Project Structure

python-handler-template/
β”œβ”€β”€ cloud-functions/                # Python Cloud Functions source
β”‚   β”œβ”€β”€ hello.py                   # Static route β†’ GET /hello
β”‚   └── api/
β”‚       β”œβ”€β”€ posts/
β”‚       β”‚   └── index.py           # Index route β†’ GET /api/posts
β”‚       β”œβ”€β”€ users/
β”‚       β”‚   β”œβ”€β”€ [userId].py        # Dynamic param β†’ GET /api/users/:userId
β”‚       β”‚   └── [userId]/
β”‚       β”‚       └── posts/
β”‚       β”‚           └── [postId].py # Nested params β†’ GET /api/users/:userId/posts/:postId
β”‚       └── files/
β”‚           └── [[path]].py        # Catch-all β†’ GET /api/files/*path
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ app/                       # Next.js App Router
β”‚   β”‚   β”œβ”€β”€ globals.css           # Global styles
β”‚   β”‚   β”œβ”€β”€ layout.tsx            # Root layout
β”‚   β”‚   └── page.tsx              # Main page (API demo UI)
β”‚   β”œβ”€β”€ components/               # React components
β”‚   β”‚   └── ui/                   # UI base components
β”‚   β”‚       β”œβ”€β”€ button.tsx        # Button component
β”‚   β”‚       └── card.tsx          # Card component
β”‚   └── lib/                      # Utility functions
β”‚       └── utils.ts              # Common utilities
β”œβ”€β”€ public/                        # Static assets
β”œβ”€β”€ package.json                   # Project configuration
└── README.md                     # Project documentation

πŸš€ Quick Start

Requirements

  • Node.js 18+
  • npm or yarn
  • Python 3.9+ (for local development)

Install Dependencies

npm install
# or
yarn install

Development Mode

edgeone pages dev

Visit http://localhost:8088 to view the application.

Build Production Version

edgeone pages build

🎯 Core Features

1. File-Based Python Routing

The cloud-functions/ directory maps directly to API routes:

File Route Pattern
hello.py GET /hello Static route
api/posts/index.py GET /api/posts Index route
api/users/[userId].py GET /api/users/:userId Dynamic parameter
api/users/[userId]/posts/[postId].py GET /api/users/:userId/posts/:postId Nested dynamic parameters
api/files/[[path]].py GET /api/files/*path Catch-all route

2. Interactive API Demo

  • Click "Call" to test each API endpoint in real-time
  • View JSON response with syntax highlighting
  • Expandable source file path display

3. Handler Convention

Each Python file exports a standard handler class:

import json
import time
from http.server import BaseHTTPRequestHandler

class handler(BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200)
        self.send_header('Content-Type', 'application/json')
        self.end_headers()
        self.wfile.write(json.dumps({
            "message": "Hello from Python Cloud Function!",
            "timestamp": time.time()
        }).encode('utf-8'))

πŸ”§ Configuration

Tailwind CSS Configuration

The project uses Tailwind CSS 4 with custom color variables:

:root {
  --primary: #1c66e5;        /* Primary color */
  --background: #000000;     /* Background color */
  --foreground: #ffffff;     /* Foreground color */
}

Component Styling

Uses class-variance-authority to manage component style variants with multiple preset styles.

πŸ“š Documentation

πŸš€ Deployment Guide

EdgeOne Pages Deployment

  1. Push code to GitHub repository
  2. Create new project in EdgeOne Pages console
  3. Select GitHub repository as source
  4. Configure build command: edgeone pages build
  5. Deploy project

Python Cloud Functions Configuration

Create cloud-functions/ folder in project root and add Python handler files:

# cloud-functions/hello.py
import json
import time
from http.server import BaseHTTPRequestHandler

class handler(BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200)
        self.send_header('Content-Type', 'application/json')
        self.end_headers()
        self.wfile.write(json.dumps({
            "message": "Hello from Python!",
            "timestamp": time.time()
        }).encode('utf-8'))

Deploy

Deploy with EdgeOne Pages

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors