瀏覽代碼

模板继承,多页面

caijynb 2 年之前
父節點
當前提交
185671c2b3
共有 4 個文件被更改,包括 76 次插入33 次删除
  1. 13 6
      main.py
  2. 32 0
      templates/base_index.html
  3. 0 27
      templates/index.html
  4. 31 0
      templates/simple_backtest.html

+ 13 - 6
main.py

@@ -45,8 +45,13 @@ def get_pct(code, start_date, end_date):
 
 
 # 路由定义
-@app.route('/', methods=['GET', 'POST'])
+@app.route('/')
 def index():
+    # 基础页面只负责渲染主页
+    return render_template('base_index.html')
+
+@app.route('/simple_backtest', methods=['GET', 'POST'])
+def simple_backtest():
     if request.method == 'POST':
         file = request.files['file']
         if file:
@@ -54,8 +59,7 @@ def index():
             filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
             file.save(filepath)
 
-
-            # 读取CSV文件
+            # 以下逻辑与原来的index函数中的POST处理逻辑相同
             code_date_list = extract_code_date(filepath)
             pct_list = []
             up_down_flags = []
@@ -73,12 +77,15 @@ def index():
             df['code'] = df['code'].astype(str).str.zfill(6)
             df['10_days_pct'] = pct_list
 
-            return render_template('index.html', 
-                                   tables=[df.to_html(classes='table table-striped')], 
+            # 注意这里渲染的是'simple_backtest.html'
+            return render_template('simple_backtest.html',
+                                   tables=[df.to_html(classes='table table-striped')],
                                    titles=df.columns.values,
                                    up_probability=up_probability)
 
-    return render_template('index.html')
+    # 对于GET请求,渲染'simple_backtest.html'但不包含表格数据
+    return render_template('simple_backtest.html')
+
 
 if __name__ == '__main__':
     app.run(debug=True)

+ 32 - 0
templates/base_index.html

@@ -0,0 +1,32 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
+    <title>股票数据分析</title>
+    <!-- Bootstrap CSS -->
+    <link href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css" rel="stylesheet">
+    <!-- Optional theme -->
+    <link href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap-theme.min.css" rel="stylesheet">
+</head>
+<body>
+
+<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
+    <div class="collapse navbar-collapse" id="navbarNav">
+        <ul class="navbar-nav">
+            <li class="nav-item active">
+                <a class="nav-link" href="/simple_backtest">简单回测</a>
+            </li>
+        </ul>
+    </div>
+</nav>
+
+<div class="container mt-5">
+    {% block content %}{% endblock %}
+</div>
+<!-- Bootstrap JS and dependencies -->
+<script src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/jquery.min.js"></script>
+<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/popper.min.js"></script>
+<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.min.js"></script>
+</body>
+</html>

+ 0 - 27
templates/index.html

@@ -1,27 +0,0 @@
-
-<!DOCTYPE html>
-<html lang="en">
-<head>
-    <meta charset="UTF-8">
-    <title>股票数据分析</title>
-    <link href="https://cdn.bootcdn.net/ajax/libs/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
-</head>
-<body>
-
-<div class="container mt-5">
-    <h1>上传CSV文件</h1>
-    <form action="/" method="post" enctype="multipart/form-data">
-        <input type="file" name="file">
-        <input type="submit" value="上传">
-    </form>
-
-    {% if tables %}
-        <h2>分析结果</h2>
-        {% for table in tables %}
-            {{ table|safe }}
-        {% endfor %}
-    {% endif %}
-</div>
-
-</body>
-</html>

+ 31 - 0
templates/simple_backtest.html

@@ -0,0 +1,31 @@
+{% extends "base_index.html" %}
+{% block content %}
+    <div class="container mt-5">
+        <h1 class="text-center mb-4">上传CSV文件</h1>
+        <div class="row justify-content-center">
+            <div class="col-md-6">
+                <form action="/simple_backtest" method="post" enctype="multipart/form-data" class="mb-3">
+                    <div class="input-group mb-3">
+                        <div class="custom-file">
+                            <input type="file" class="custom-file-input" name="file" id="fileInput">
+                            <label class="custom-file-label" for="fileInput">文件路径</label>
+                        </div>
+                        <div class="input-group-append">
+                            <button class="btn btn-outline-secondary" type="submit">上传</button>
+                        </div>
+                    </div>
+                </form>
+            </div>
+        </div>
+
+        {% if tables %}
+            <h2 class="text-center">分析结果</h2>
+            <div class="table-responsive">
+                {% for table in tables %}
+                    {{ table|safe }}
+                {% endfor %}
+            </div>
+        {% endif %}
+    </div>
+
+{% endblock %}