(同时,在用Trae,在写一个游戏教学,total用时3小时,浓缩时长估计5分钟)
用了CSDN推荐的IDE,基于AI开发代码
几乎不自己写代码
习惯了双屏幕ctrl+c , ctrl+v的程序员,自然也会习惯这种一半代码,一半AI的界面
界面如下:
开发过程如下:
一开始
要实现一个文档索引和显示界面,我们可以使用 Flask 框架结合 HTML 和 Python 来完成。以下是一个简单的实现步骤和示例代码:
### 步骤概述
1. **创建文档目录**:在项目中创建一个目录,用于存放文档文件。
2. **生成文档索引**:编写 Python 代码,遍历文档目录,生成文档的索引列表。
3. **创建 Flask 应用**:使用 Flask 框架创建一个 Web 应用,处理用户请求。
4. **创建 HTML 模板**:创建 HTML 模板,用于显示文档索引和文档内容。
### 示例代码
#### 1. 创建文档目录
在项目根目录下创建一个名为 `docs` 的目录,用于存放文档文件。
#### 2. 生成文档索引
修改 `app.py` 文件,添加生成文档索引的代码:
```python:f:\360Downloads\MrSelfDocs\app.py
#pip install flask
from flask import Flask, render_template
import os
# 创建 Flask 应用实例
app = Flask(__name__)
# 定义文档目录
DOCS_DIR = 'docs'
# 生成文档索引
def get_doc_index():
index = []
for root, dirs, files in os.walk(DOCS_DIR):
for file in files:
if file.endswith('.txt'):
index.append(os.path.join(root, file))
return index
# 定义路由和处理函数
@app.route('/')
def index():
doc_index = get_doc_index()
return render_template('index.html', doc_index=doc_index)
@app.route('/doc/<path:doc_path>')
def show_doc(doc_path):
doc_file = os.path.join(DOCS_DIR, doc_path)
try:
with open(doc_file, 'r', encoding='utf-8') as f:
content = f.read()
return render_template('doc.html', content=content)
except FileNotFoundError:
return "文档未找到", 404
if __name__ == '__main__':
# 启动 Flask 应用
app.run(debug=True)
```
#### 3. 创建 HTML 模板
在项目根目录下创建一个名为 `templates` 的目录,用于存放 HTML 模板文件。
- `templates/index.html`:显示文档索引
```html:f:\360Downloads\MrSelfDocs\templates\index.html
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>文档索引</title>
</head>
<body>
<h1>文档索引</h1>
<ul>
{% for doc in doc_index %}
<li><a href="{{ url_for('show_doc', doc_path=doc[len('docs/'):]) }}">{{ doc }}</a></li>
{% endfor %}
</ul>
</body>
</html>