Skip to content

Commit f86f58b

Browse files
committed
Add code listing for chapter 1~2
1 parent d0ca780 commit f86f58b

File tree

2 files changed

+197
-0
lines changed

2 files changed

+197
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# 第 1 章:代码清单
2+
3+
每一章后面都会有一个代码清单章节,这里会列出上一章所有的代码变动和相关命令。本章的目的是提供一个更清晰的代码改动列表,供你在编写代码时作为参考。
4+
5+
## 目录
6+
7+
```text
8+
watchlist/
9+
├── .venv/
10+
├── .git/
11+
└── .gitignore
12+
```
13+
14+
## 代码
15+
16+
### .gitignore
17+
18+
```text
19+
*.pyc
20+
*~
21+
__pycache__
22+
.DS_Store
23+
.venv
24+
```
25+
26+
## 命令
27+
28+
### 创建项目文件夹
29+
30+
```bash
31+
$ mkdir watchlist
32+
$ cd watchlist
33+
```
34+
35+
### 设置 Git 身份信息
36+
37+
```bash
38+
$ git config --global user.name "Your Name" # 替换成你的名字
39+
$ git config --global user.email "[email protected]" # 替换成你的邮箱地址
40+
```
41+
42+
### 初始化 Git 仓库
43+
44+
```bash
45+
$ git init
46+
Initialized empty Git repository in ~/watchlist/.git/
47+
```
48+
49+
### 创建 .gitignore 文件
50+
51+
```bash
52+
$ nano .gitignore
53+
```
54+
55+
### 生成 SSH 密钥
56+
57+
```bash
58+
$ ssh-keygen -t ed25519 -C "[email protected]"
59+
$ cat ~/.ssh/id_ed25519.pub
60+
```
61+
62+
### 设置远程仓库
63+
64+
```bash
65+
$ git remote add origin [email protected]:greyli/watchlist.git # 注意更换地址中的用户名
66+
```
67+
68+
### 创建虚拟环境
69+
70+
```bash
71+
$ python -m venv .venv # Windows
72+
```
73+
74+
或:
75+
76+
```bash
77+
$ python3 -m venv .venv # Linux 和 macOS
78+
```
79+
80+
### 激活虚拟环境
81+
82+
```bash
83+
$ .venv\Scripts\activate # Windows
84+
```
85+
86+
或:
87+
88+
```bash
89+
$ source .venv/bin/activate # Linux 或 macOS
90+
```
91+
92+
### 安装 Flask
93+
94+
```bash
95+
(.venv) $ pip install flask
96+
```
97+
98+
### 提交代码
99+
100+
```bash
101+
$ git add .
102+
$ git commit -m "Init the project"
103+
$ git push -u origin main
104+
```

chapters/2-hello-code-listing.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# 第 2 章:代码清单
2+
3+
## 目录
4+
5+
```text
6+
watchlist/
7+
├── app.py
8+
├── .env
9+
├── .flaskenv
10+
└── .gitignore
11+
```
12+
13+
> **提示** 实际目录中的 .venv 和 .git 没有列出。
14+
## 代码
15+
16+
### app.py
17+
18+
```python
19+
from flask import Flask
20+
21+
app = Flask(__name__)
22+
23+
@app.route('/')
24+
def hello():
25+
return 'Welcome to My Watchlist!'
26+
```
27+
28+
### .gitignore
29+
30+
```diff
31+
*.pyc
32+
*~
33+
__pycache__
34+
.DS_Store
35+
.venv
36+
+.env
37+
```
38+
39+
## 命令
40+
41+
### 激活虚拟环境
42+
43+
```bash
44+
$ .venv\Scripts\activate # Windows
45+
```
46+
47+
或:
48+
49+
```bash
50+
$ source .venv/bin/activate # Linux 或 macOS
51+
```
52+
53+
> **提示** 确保在执行 `flask``python``pip` 等命令前激活了虚拟环境,后续章节不再列出激活命令。
54+
55+
### 启动程序
56+
57+
默认启动命令:
58+
59+
```bash
60+
(.venv) $ flask run
61+
```
62+
63+
以调试模式启动程序:
64+
65+
```bash
66+
(.venv) $ flask run --debug
67+
```
68+
69+
设置不同的端口:
70+
71+
```bash
72+
(.venv) $ flask run --debug --port 8000
73+
```
74+
75+
### 安装 python-dotenv
76+
77+
```bash
78+
(.venv) $ pip install python-dotenv
79+
```
80+
81+
### 创建 .env 和 .flaskenv 文件
82+
83+
```bash
84+
$ touch .env .flaskenv
85+
```
86+
87+
### 提交代码
88+
89+
```bash
90+
$ git add .
91+
$ git commit -m "Add a minimal home page"
92+
$ git push
93+
```

0 commit comments

Comments
 (0)