Spaces:
Sleeping
Sleeping
Download frontend/src/pages/FileTest.tsx from shaun07/grid-backtest: direct link, hf CLI and curl.
- Browser
- Download file 6.94 kB
-
https://huggingface.co/spaces/shaun07/grid-backtest/resolve/main/frontend/src/pages/FileTest.tsx
- Command line
-
hf download hf://spaces/shaun07/grid-backtest/frontend/src/pages/FileTest.tsx
-
curl -L -o FileTest.tsx https://huggingface.co/spaces/shaun07/grid-backtest/resolve/main/frontend/src/pages/FileTest.tsx
6.94 kB
| import { useState } from 'react' | |
| import { Card, CardContent, CardHeader, CardTitle } from '../components/ui/card' | |
| import { FileUpload } from '../components/ui/file-upload' | |
| import { parseStockDataFile, ParseResult } from '../utils/fileParser' | |
| import { CheckCircle, AlertCircle, FileText, Database } from 'lucide-react' | |
| export default function FileTest() { | |
| const [uploadedFile, setUploadedFile] = useState<File | null>(null) | |
| const [parseResult, setParseResult] = useState<ParseResult | null>(null) | |
| const [parsing, setParsing] = useState(false) | |
| const handleFileSelect = async (file: File) => { | |
| setUploadedFile(file) | |
| setParsing(true) | |
| setParseResult(null) | |
| try { | |
| const result = await parseStockDataFile(file) | |
| setParseResult(result) | |
| if (result.success) { | |
| console.log('解析成功:', result) | |
| } else { | |
| console.error('解析失败:', result.error) | |
| } | |
| } catch (error) { | |
| console.error('解析异常:', error) | |
| setParseResult({ | |
| success: false, | |
| error: `解析异常: ${error}` | |
| }) | |
| } finally { | |
| setParsing(false) | |
| } | |
| } | |
| const handleFileRemove = () => { | |
| setUploadedFile(null) | |
| setParseResult(null) | |
| } | |
| return ( | |
| <div className="w-full"> | |
| <div className="mb-6"> | |
| <h1 className="text-2xl font-bold mb-2">文件解析测试</h1> | |
| <p className="text-muted-foreground"> | |
| 测试EXCEL、CSV、JSON文件的解析功能 | |
| </p> | |
| </div> | |
| <div className="grid gap-6"> | |
| {/* 文件上传区域 */} | |
| <Card> | |
| <CardHeader> | |
| <CardTitle className="flex items-center gap-2"> | |
| <FileText className="h-5 w-5" /> | |
| 文件上传 | |
| </CardTitle> | |
| </CardHeader> | |
| <CardContent> | |
| <FileUpload | |
| onFileSelect={handleFileSelect} | |
| onFileRemove={handleFileRemove} | |
| selectedFile={uploadedFile} | |
| acceptedTypes={['.csv', '.xlsx', '.xls', '.json']} | |
| maxSize={50} | |
| /> | |
| </CardContent> | |
| </Card> | |
| {/* 解析状态 */} | |
| {parsing && ( | |
| <Card> | |
| <CardContent className="pt-6"> | |
| <div className="flex items-center gap-2 text-blue-600"> | |
| <div className="animate-spin rounded-full h-4 w-4 border-b-2 border-blue-600"></div> | |
| <span>正在解析文件...</span> | |
| </div> | |
| </CardContent> | |
| </Card> | |
| )} | |
| {/* 解析结果 */} | |
| {parseResult && ( | |
| <Card> | |
| <CardHeader> | |
| <CardTitle className="flex items-center gap-2"> | |
| <Database className="h-5 w-5" /> | |
| 解析结果 | |
| {parseResult.success ? ( | |
| <CheckCircle className="h-5 w-5 text-green-500" /> | |
| ) : ( | |
| <AlertCircle className="h-5 w-5 text-red-500" /> | |
| )} | |
| </CardTitle> | |
| </CardHeader> | |
| <CardContent> | |
| {parseResult.success ? ( | |
| <div className="space-y-4"> | |
| <div className="grid grid-cols-2 gap-4"> | |
| <div> | |
| <label className="text-sm font-medium text-muted-foreground">股票代码</label> | |
| <p className="text-lg font-semibold">{parseResult.symbol || '未识别'}</p> | |
| </div> | |
| <div> | |
| <label className="text-sm font-medium text-muted-foreground">股票名称</label> | |
| <p className="text-lg font-semibold">{parseResult.symbolName || '未识别'}</p> | |
| </div> | |
| <div> | |
| <label className="text-sm font-medium text-muted-foreground">数据条数</label> | |
| <p className="text-lg font-semibold">{parseResult.totalRecords || 0} 条</p> | |
| </div> | |
| <div> | |
| <label className="text-sm font-medium text-muted-foreground">日期范围</label> | |
| <p className="text-lg font-semibold"> | |
| {parseResult.dateRange ? | |
| `${parseResult.dateRange.start} ~ ${parseResult.dateRange.end}` : | |
| '未识别' | |
| } | |
| </p> | |
| </div> | |
| </div> | |
| {/* 数据预览 */} | |
| {parseResult.data && parseResult.data.length > 0 && ( | |
| <div> | |
| <h4 className="text-sm font-medium text-muted-foreground mb-2">数据预览(前5条)</h4> | |
| <div className="overflow-x-auto"> | |
| <table className="w-full text-sm border-collapse border border-gray-200"> | |
| <thead> | |
| <tr className="bg-gray-50"> | |
| <th className="border border-gray-200 px-2 py-1 text-left">日期</th> | |
| <th className="border border-gray-200 px-2 py-1 text-left">开盘</th> | |
| <th className="border border-gray-200 px-2 py-1 text-left">最高</th> | |
| <th className="border border-gray-200 px-2 py-1 text-left">最低</th> | |
| <th className="border border-gray-200 px-2 py-1 text-left">收盘</th> | |
| <th className="border border-gray-200 px-2 py-1 text-left">成交量</th> | |
| </tr> | |
| </thead> | |
| <tbody> | |
| {parseResult.data.slice(0, 5).map((row, index) => ( | |
| <tr key={index}> | |
| <td className="border border-gray-200 px-2 py-1">{row.date}</td> | |
| <td className="border border-gray-200 px-2 py-1">{row.open}</td> | |
| <td className="border border-gray-200 px-2 py-1">{row.high}</td> | |
| <td className="border border-gray-200 px-2 py-1">{row.low}</td> | |
| <td className="border border-gray-200 px-2 py-1">{row.close}</td> | |
| <td className="border border-gray-200 px-2 py-1">{row.volume}</td> | |
| </tr> | |
| ))} | |
| </tbody> | |
| </table> | |
| </div> | |
| </div> | |
| )} | |
| </div> | |
| ) : ( | |
| <div className="text-red-600"> | |
| <p className="font-medium">解析失败</p> | |
| <p className="text-sm mt-1">{parseResult.error}</p> | |
| </div> | |
| )} | |
| </CardContent> | |
| </Card> | |
| )} | |
| </div> | |
| </div> | |
| ) | |
| } | |