Zenaight commited on
Commit
d17f871
Β·
verified Β·
1 Parent(s): 1709668

Create PYTHON_HF_EXPORT_README.md

Browse files
Files changed (1) hide show
  1. PYTHON_HF_EXPORT_README.md +214 -0
PYTHON_HF_EXPORT_README.md ADDED
@@ -0,0 +1,214 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Python HF Export Implementation
2
+
3
+ This document describes the HF Export functionality that has been added to your Python FastAPI server, allowing you to export business plans as PDF and Word documents.
4
+
5
+ ## πŸš€ What's New
6
+
7
+ Your FastAPI server now includes:
8
+ - **PDF Export**: Generate professional PDF business plans using `reportlab`
9
+ - **Word Export**: Create Word documents using `python-docx`
10
+ - **Chart Integration**: Support for base64 encoded chart images
11
+ - **File Download**: Direct download endpoints for generated documents
12
+
13
+ ## πŸ“‹ New Endpoints
14
+
15
+ ### 1. `/export` (POST)
16
+ **Purpose**: Export business plans in PDF or Word format
17
+
18
+ **Request Body**:
19
+ ```json
20
+ {
21
+ "summary": "Business plan summary",
22
+ "sections": [
23
+ {
24
+ "key": "executive_summary",
25
+ "title": "Executive Summary",
26
+ "content": "Section content..."
27
+ }
28
+ ],
29
+ "businessIdea": "Business idea name",
30
+ "isFullPlan": true,
31
+ "format": "pdf", // "pdf" or "word"
32
+ "chartImages": ["base64_image_1", "base64_image_2"] // Optional
33
+ }
34
+ ```
35
+
36
+ **Response**:
37
+ ```json
38
+ {
39
+ "success": true,
40
+ "downloadUrl": "/download/filename.pdf",
41
+ "filename": "filename.pdf",
42
+ "size": 12345,
43
+ "message": "Document exported successfully as PDF"
44
+ }
45
+ ```
46
+
47
+ ### 2. `/download/{filename}` (GET)
48
+ **Purpose**: Download generated export files
49
+
50
+ **Usage**: Visit the download URL returned by the export endpoint
51
+
52
+ ### 3. `/export-test` (GET)
53
+ **Purpose**: Test the export functionality with sample data
54
+
55
+ ### 4. `/export-plan` (POST)
56
+ **Purpose**: Generate a business plan and export it in one request
57
+
58
+ ## πŸ”§ Dependencies Added
59
+
60
+ The following packages have been added to your `requirements.txt`:
61
+
62
+ ```txt
63
+ python-docx==0.8.11 # Word document generation
64
+ fpdf2 # PDF generation (simple & powerful)
65
+ Pillow==10.0.0 # Image processing
66
+ requests # For testing
67
+ ```
68
+
69
+ ## πŸ“ File Structure
70
+
71
+ ```
72
+ mybusinessdraftDEVRobert/
73
+ β”œβ”€β”€ app/
74
+ β”‚ └── main.py # Updated with export functionality
75
+ β”œβ”€β”€ requirements.txt # Updated with new dependencies
76
+ β”œβ”€β”€ test_export.py # Test script for export functionality
77
+ β”œβ”€β”€ PYTHON_HF_EXPORT_README.md # This file
78
+ └── temp_exports/ # Generated by the export system
79
+ ```
80
+
81
+ ## πŸ§ͺ Testing the Export System
82
+
83
+ ### 1. Install Dependencies
84
+ ```bash
85
+ pip install -r requirements.txt
86
+ ```
87
+
88
+ ### 2. Start Your Server
89
+ ```bash
90
+ cd app
91
+ uvicorn main:app --reload
92
+ ```
93
+
94
+ ### 3. Run the Test Script
95
+ ```bash
96
+ python test_export.py
97
+ ```
98
+
99
+ ### 4. Test Manually
100
+ - Visit `http://localhost:8000/` to see available endpoints
101
+ - Use `http://localhost:8000/export-test` to test export with sample data
102
+ - Test the `/export` endpoint with your own data
103
+
104
+ ## πŸ’‘ How It Works
105
+
106
+ ### PDF Generation
107
+ 1. **Data Processing**: Receives business plan data and chart images
108
+ 2. **Document Creation**: Uses `fpdf2` to create a professional PDF (simple & fast)
109
+ 3. **Image Integration**: Embeds base64 encoded chart images
110
+ 4. **File Output**: Returns PDF bytes for frontend handling
111
+ 5. **Download Link**: Frontend uploads to UploadThing for user access
112
+
113
+ ### Word Generation
114
+ 1. **Data Processing**: Same as PDF
115
+ 2. **Document Creation**: Uses `python-docx` to create a Word document
116
+ 3. **Image Integration**: Embeds chart images in the document
117
+ 4. **File Output**: Saves as `.docx` file
118
+ 5. **Download Link**: Returns download URL
119
+
120
+ ## πŸ”’ Security Notes
121
+
122
+ - **File Storage**: Currently saves files to local `temp_exports/` directory
123
+ - **Production**: Consider using cloud storage (AWS S3, Google Cloud Storage) for production
124
+ - **File Cleanup**: Implement periodic cleanup of temporary files
125
+ - **Access Control**: Add authentication if needed for production use
126
+
127
+ ## πŸš€ Production Considerations
128
+
129
+ ### File Storage
130
+ ```python
131
+ # Instead of local storage, use cloud storage:
132
+ import boto3 # For AWS S3
133
+ # or
134
+ from google.cloud import storage # For Google Cloud
135
+ ```
136
+
137
+ ### Error Handling
138
+ ```python
139
+ # Add more robust error handling:
140
+ try:
141
+ # Export logic
142
+ except FileNotFoundError:
143
+ raise HTTPException(status_code=404, detail="Template not found")
144
+ except PermissionError:
145
+ raise HTTPException(status_code=403, detail="Permission denied")
146
+ ```
147
+
148
+ ### Rate Limiting
149
+ ```python
150
+ # Add rate limiting for export requests:
151
+ from slowapi import Limiter, _rate_limit_exceeded_handler
152
+ from slowapi.util import get_remote_address
153
+ ```
154
+
155
+ ## πŸ” Troubleshooting
156
+
157
+ ### Common Issues
158
+
159
+ 1. **Import Errors**
160
+ ```bash
161
+ pip install python-docx reportlab Pillow
162
+ ```
163
+
164
+ 2. **File Permission Errors**
165
+ ```bash
166
+ # Ensure temp_exports directory is writable
167
+ mkdir -p temp_exports
168
+ chmod 755 temp_exports
169
+ ```
170
+
171
+ 3. **Image Processing Errors**
172
+ - Ensure images are valid base64 strings
173
+ - Check image format compatibility
174
+ - Verify image size limits
175
+
176
+ ### Debug Mode
177
+ ```python
178
+ # Add debug logging to export functions:
179
+ logging.debug(f"Processing image {i}: {len(img_data)} bytes")
180
+ ```
181
+
182
+ ## πŸ“š API Documentation
183
+
184
+ Once your server is running, visit:
185
+ - `http://localhost:8000/docs` - Interactive API documentation
186
+ - `http://localhost:8000/redoc` - Alternative API documentation
187
+
188
+ ## 🎯 Next Steps
189
+
190
+ 1. **Test the export functionality** with your existing business plan data
191
+ 2. **Customize the document templates** to match your branding
192
+ 3. **Add more export formats** if needed (HTML, Markdown, etc.)
193
+ 4. **Implement file cleanup** for temporary exports
194
+ 5. **Add authentication** for production use
195
+
196
+ ## 🀝 Integration with Frontend
197
+
198
+ Your frontend can now:
199
+ 1. **Send business plan data** to `/export` endpoint
200
+ 2. **Receive download URLs** for generated documents
201
+ 3. **Trigger file downloads** using the returned URLs
202
+ 4. **Handle different formats** (PDF/Word) based on user preference
203
+
204
+ ## πŸ“ž Support
205
+
206
+ If you encounter issues:
207
+ 1. Check the server logs for error messages
208
+ 2. Verify all dependencies are installed
209
+ 3. Test with the provided test script
210
+ 4. Check file permissions for the `temp_exports` directory
211
+
212
+ ---
213
+
214
+ **Happy Exporting! πŸŽ‰**