-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.xml
622 lines (294 loc) · 981 KB
/
search.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
<?xml version="1.0" encoding="utf-8"?>
<search>
<entry>
<title>Python_Scrapy_Post</title>
<link href="/2021/06/27/python-scrapy-post/"/>
<url>/2021/06/27/python-scrapy-post/</url>
<content type="html"><![CDATA[<h3 id="Python-爬虫处理-Post-请求"><a href="#Python-爬虫处理-Post-请求" class="headerlink" title="Python 爬虫处理 Post 请求"></a>Python 爬虫处理 Post 请求</h3><h4 id="Post-请求发送的数据为-Data-数据"><a href="#Post-请求发送的数据为-Data-数据" class="headerlink" title="Post 请求发送的数据为 Data 数据"></a>Post 请求发送的数据为 Data 数据</h4><ul><li><p>携带 Data 数据的 post 请求</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">RequestURL:http://127.0.0.1:8080/test/test.do</span><br><span class="line">Request Method:POST</span><br><span class="line">Status Code:200 OK</span><br><span class="line"></span><br><span class="line">Request Headers</span><br><span class="line">Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8</span><br><span class="line">Accept-Encoding:gzip,deflate,sdch</span><br><span class="line">Accept-Language:zh-CN,zh;q=0.8,en;q=0.6</span><br><span class="line">AlexaToolbar-ALX_NS_PH:AlexaToolbar/alxg-3.2</span><br><span class="line">Cache-Control:max-age=0</span><br><span class="line">Connection:keep-alive</span><br><span class="line">Content-Length:25</span><br><span class="line">Content-Type:application/x-www-form-urlencoded</span><br><span class="line">Cookie:JSESSIONID=74AC93F9F572980B6FC10474CD8EDD8D</span><br><span class="line">Host:127.0.0.1:8080</span><br><span class="line">Origin:http://127.0.0.1:8080</span><br><span class="line">Referer:http://127.0.0.1:8080/test/index.jsp</span><br><span class="line">User-Agent:Mozilla/5.0 (Windows NT 6.1)AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.149 Safari/537.36</span><br><span class="line"></span><br><span class="line">Form Data</span><br><span class="line">name:mikan</span><br><span class="line">address:street</span><br><span class="line"></span><br><span class="line">Response Headers</span><br><span class="line">Content-Length:2</span><br><span class="line">Date:Sun, 11 May 2014 11:05:33 GMT</span><br><span class="line">Server:Apache-Coyote/1.1</span><br></pre></td></tr></table></figure></li><li><p>使用 Requests 模块发生 携带 Data 数据 的 post 请求</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">response = requests.post(</span><br><span class="line"> url = <span class="string">"请求 url 地址"</span>,</span><br><span class="line"> headers = <span class="string">"请求头字典"</span>,</span><br><span class="line"> data = <span class="string">"请求数据字典"</span>,</span><br><span class="line">)</span><br></pre></td></tr></table></figure></li></ul><h4 id="Post-请求发送的数据为-Request-Payload-数据"><a href="#Post-请求发送的数据为-Request-Payload-数据" class="headerlink" title="Post 请求发送的数据为 Request Payload 数据"></a>Post 请求发送的数据为 Request Payload 数据</h4><ul><li><p>携带 Request Payload 数据的 post 请求</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line">General</span><br><span class="line">Request URL: https://account.alibabacloud.com/csp/report.htm</span><br><span class="line">Request Method: POST</span><br><span class="line">Status Code: 200 </span><br><span class="line">Remote Address: 47.88.251.186:443</span><br><span class="line">Referrer Policy: strict-origin-when-cross-origin</span><br><span class="line"></span><br><span class="line">Response Headers</span><br><span class="line">content-encoding: gzip</span><br><span class="line">content-type: text/html;charset=UTF-8</span><br><span class="line">date: Fri, 25 Jun 2021 07:54:07 GMT</span><br><span class="line">eagleeye-traceid: 0a98a6bb16246076475364468e2bdc</span><br><span class="line">server: Tengine</span><br><span class="line">strict-transport-security: max-age=0</span><br><span class="line">timing-allow-origin: *</span><br><span class="line">vary: Accept-Encoding</span><br><span class="line"></span><br><span class="line">Request Headers</span><br><span class="line">:authority: account.alibabacloud.com</span><br><span class="line">:method: POST</span><br><span class="line">:path: /csp/report.htm</span><br><span class="line">:scheme: https</span><br><span class="line">accept: */*</span><br><span class="line">accept-encoding: gzip, deflate, br</span><br><span class="line">accept-language: zh-CN,zh;q=0.9</span><br><span class="line">content-length: 1047</span><br><span class="line">content-type: application/csp-report</span><br><span class="line">cookie: ******************************************************************************************************</span><br><span class="line">origin: https://account.alibabacloud.com</span><br><span class="line">referer: https://account.alibabacloud.com/login/login.htm</span><br><span class="line">sec-ch-ua: " Not;A Brand";v="99", "Google Chrome";v="91", "Chromium";v="91"</span><br><span class="line">sec-ch-ua-mobile: ?0</span><br><span class="line">sec-fetch-dest: report</span><br><span class="line">sec-fetch-mode: no-cors</span><br><span class="line">sec-fetch-site: same-origin</span><br><span class="line">user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36</span><br><span class="line"></span><br><span class="line">Request Payload</span><br><span class="line">{"csp-report":{"document-uri":"https://account.alibabacloud.com/login/login.htm","referrer":"https://www.google.com/","violated-directive":"script-src-elem","effective-directive":"script-src-elem","original-policy":"base-uri 'self';script-src 'self' 'unsafe-inline' 'unsafe-eval' 'report-sample' https: http: 'sha256-lfXlPY3+MCPOPb4mrw1Y961+745U3WlDQVcOXdchSQc=' 'sha256-QbgF6nrAFOI1VumLs3RwKgg0Qmj5JImgLwiAhJOUoeQ=' 'sha256-rRMdkshZyJlCmDX27XnL7g3zXaxv7ei6Sg+yt4R3svU=' 'sha256-kbHtQyYDQKz4SWMQ8OHVol3EC0t3tHEJFPCSwNG9NxQ=' 'sha256-46mc3H6z56gnOReRHr//8M7FxjqtSaDN7KetqqduuiE=' 'Strict-Dynamic' 'unsafe-hashes' 'nonce-0MVMRYu19o';frame-src 'self' *.aliyun.com *.alibaba.com *.alibabacloud.com gaic.alicdn.com g.alicdn.com;worker-src blob: 'self' data:;object-src 'self' g.alicdn.com;frame-ancestors *.aliyun.com;report-uri /csp/report.htm;","disposition":"report","blocked-uri":"inline","line-number":59,"source-file":"https://account.alibabacloud.com/login/login.htm","status-code":0,"script-sample":"var ALIYUN_ACCOUNT_LOGIN_CONFIG = {\n "}}</span><br></pre></td></tr></table></figure></li><li><p>使用 Requests 模块发生 携带 Request Payload 数据 的 post 请求</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> json</span><br><span class="line"><span class="keyword">import</span> requests</span><br><span class="line"><span class="keyword">from</span> pprint <span class="keyword">import</span> pprint</span><br><span class="line"></span><br><span class="line">url = <span class="string">""</span></span><br><span class="line">payloadHeaders = {</span><br><span class="line"> <span class="string">"user-agent"</span>: <span class="string">"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36"</span>,</span><br><span class="line"> <span class="string">"content-type"</span>: <span class="string">"application/json;charset=UTF-8"</span>,</span><br><span class="line">} </span><br><span class="line"></span><br><span class="line">payloadData = {<span class="string">"query"</span>: [],</span><br><span class="line"> <span class="string">"start"</span>: <span class="number">6000</span>,</span><br><span class="line"> <span class="string">"rows"</span>: <span class="number">100</span>,</span><br><span class="line"> <span class="string">"sort_field"</span>: {<span class="string">"sort_field"</span>: <span class="string">"ImpactFactor"</span>},</span><br><span class="line"> <span class="string">"highlight_field"</span>: <span class="string">""</span>,</span><br><span class="line"> <span class="string">"pinyin_title"</span>: [],</span><br><span class="line"> <span class="string">"class_code"</span>: <span class="string">""</span>,</span><br><span class="line"> <span class="string">"core_periodical"</span>: [],</span><br><span class="line"> <span class="string">"sponsor_region"</span>: [],</span><br><span class="line"> <span class="string">"publishing_period"</span>: [],</span><br><span class="line"> <span class="string">"publish_status"</span>: <span class="string">""</span>,</span><br><span class="line"> <span class="string">"return_fields"</span>: [<span class="string">"Title"</span>, <span class="string">"Id"</span>, <span class="string">"CorePeriodical"</span>, <span class="string">"Award"</span>, <span class="string">"IsPrePublished"</span>]}</span><br><span class="line"></span><br><span class="line">response = requests.post(url,</span><br><span class="line"> <span class="comment"># 将 data 转化为 json 格式数据</span></span><br><span class="line"> data=json.dumps(payloadData),</span><br><span class="line"> headers=payloadHeaders)</span><br><span class="line">dates = response.json()</span><br><span class="line">print(dates[<span class="string">'value'</span>][<span class="number">-1</span>])</span><br><span class="line">print(len(dates[<span class="string">'value'</span>]))</span><br></pre></td></tr></table></figure></li></ul><h4 id="两者区别"><a href="#两者区别" class="headerlink" title="两者区别"></a>两者区别</h4><ul><li>请求头中 <code>Content-Type: application/x-www-form-urlencoded</code>,那么就是一个 POST 表单请求,请求主体将以一个标准的键值对和&的querystring形式出现。这种方式是HTML表单的默认设置,所以在过去这种方式更加常见。</li><li>其他形式的POST请求,是放到 Request payload 中(现在是为了方便阅读,使用了Json这样的数据格式),请求的<code>Content-Type: application/json;charset=UTF-8</code>或者不指定。</li></ul>]]></content>
<tags>
<tag> Scrapy </tag>
</tags>
</entry>
<entry>
<title>DynamoDB_Get_Start.md</title>
<link href="/2021/06/27/dynamodb-get-start-md/"/>
<url>/2021/06/27/dynamodb-get-start-md/</url>
<content type="html"><![CDATA[<h2 id="Dynamodb-入门学习"><a href="#Dynamodb-入门学习" class="headerlink" title="Dynamodb 入门学习"></a>Dynamodb 入门学习</h2><h3 id="DynamoDB-本地部署"><a href="#DynamoDB-本地部署" class="headerlink" title="DynamoDB 本地部署"></a>DynamoDB 本地部署</h3><h4 id="准备工作"><a href="#准备工作" class="headerlink" title="准备工作"></a>准备工作</h4><ul><li>安装 Java 8 及以上版本</li><li>下载并安装 aws CLI <a href="https://awscli.amazonaws.com/AWSCLIV2.msi" target="_blank" rel="noopener">下载地址</a></li><li>下载 DynamoDB 文件 <a href="https://s3.ap-northeast-1.amazonaws.com/dynamodb-local-tokyo/dynamodb_local_latest.zip" target="_blank" rel="noopener">下载链接</a></li></ul><h4 id="安装及部署"><a href="#安装及部署" class="headerlink" title="安装及部署"></a>安装及部署</h4><ol><li><p>将下载的 DynamoDB 压缩包文件 解压到 自定义文件下</p></li><li><p>在 自定义文件夹下 (DynamoDBLocal.jar 同级目录内) 打开 命令行窗口</p></li><li><p>输入以下命令,启动 DynamoDB</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -sharedDb</span><br></pre></td></tr></table></figure></li><li><p>配置凭证用于工作使用,凭证内容可以自定义</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">AWS Access Key ID [None]: *********************</span><br><span class="line">AWS Secret Access Key [None]: *****************</span><br><span class="line">Default region name [None]: us-west-2</span><br><span class="line">Default output format [None]: json</span><br></pre></td></tr></table></figure></li></ol><h3 id="Python-操作-DynamoDB"><a href="#Python-操作-DynamoDB" class="headerlink" title="Python 操作 DynamoDB"></a>Python 操作 DynamoDB</h3><h4 id="准备工作-1"><a href="#准备工作-1" class="headerlink" title="准备工作"></a>准备工作</h4><ul><li><p>pip 安装 外置包 boto3 操作 DynamoDB </p><p><code>pip install boto3</code></p></li></ul><h4 id="连接-DynamoDB-数据库"><a href="#连接-DynamoDB-数据库" class="headerlink" title="连接 DynamoDB 数据库"></a>连接 DynamoDB 数据库</h4><ol><li><p>本地连接</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">dynamodb = boto3.resource(<span class="string">'ddynamodb'</span>, endpoint_url=<span class="string">"http://localhost:8000"</span>)</span><br></pre></td></tr></table></figure></li></ol><ol start="2"><li><p>使用线上 Web 服务 (region_name: 表示使用服务器区域)</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">dynamodb = boto3.resource(<span class="string">'dynamodb'</span>, region_name=<span class="string">'us-west-2'</span>)</span><br></pre></td></tr></table></figure></li></ol><h4 id="创建表操作-create-table"><a href="#创建表操作-create-table" class="headerlink" title="创建表操作: create_table()"></a>创建表操作: create_table()</h4><ul><li><p><code>create_table()</code></p><ul><li>调用时,需要指定表名称、主键属性及其数据类型</li><li><code>KeySchema</code> : 设置分区键与排序键</li><li><code>ProvisionedThroughput</code> : 必须参数,定义读取与写入单位</li><li><code>AttributeDefinitions</code> : 设置分区主键与排序主键内容类型</li></ul></li><li><p>创表实例</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line">table = dynamodb.create_table(</span><br><span class="line"> TableName=<span class="string">'Movies'</span>,</span><br><span class="line"> KeySchema=[</span><br><span class="line"> <span class="comment"># 设置分区键</span></span><br><span class="line"> {</span><br><span class="line"> <span class="string">'AttributeName'</span>: <span class="string">'year'</span>,</span><br><span class="line"> <span class="string">'KeyType'</span>: <span class="string">'HASH'</span></span><br><span class="line"> },</span><br><span class="line"> <span class="comment"># 设置排序键</span></span><br><span class="line"> {</span><br><span class="line"> <span class="string">'AttributeName'</span>: <span class="string">'title'</span>,</span><br><span class="line"> <span class="string">'KeyType'</span>: <span class="string">'RANGE'</span></span><br><span class="line"> }</span><br><span class="line"> ],</span><br><span class="line"> AttributeDefinitions=[</span><br><span class="line"> <span class="comment"># 设置分区键 year 的数据类型 为 Number</span></span><br><span class="line"> {</span><br><span class="line"> <span class="string">'AttributeName'</span>: <span class="string">'year'</span>,</span><br><span class="line"> <span class="string">'AttributeType'</span>: <span class="string">'N'</span></span><br><span class="line"> },</span><br><span class="line"> <span class="comment"># 设置排序键 title 的数据类型为 String</span></span><br><span class="line"> {</span><br><span class="line"> <span class="string">'AttributeName'</span>: <span class="string">'title'</span>,</span><br><span class="line"> <span class="string">'AttributeType'</span>: <span class="string">'S'</span></span><br><span class="line"> }</span><br><span class="line"> ],</span><br><span class="line"> <span class="comment"># 定义读取与写入容量单位</span></span><br><span class="line"> ProvisionedThroughput={</span><br><span class="line"> <span class="string">'ReadCapacityUnits'</span>: <span class="number">10</span>,</span><br><span class="line"> <span class="string">'WriteCapacityUnits'</span>: <span class="number">10</span></span><br><span class="line"> }</span><br><span class="line"> )</span><br></pre></td></tr></table></figure></li></ul><h4 id="将-JSON-数据加载到-表内-put-item"><a href="#将-JSON-数据加载到-表内-put-item" class="headerlink" title="将 JSON 数据加载到 表内: put_item()"></a>将 JSON 数据加载到 表内: put_item()</h4><ul><li><p>加载实例</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">load_movies</span><span class="params">(movies, dynamodb=None)</span>:</span></span><br><span class="line"> <span class="keyword">if</span> <span class="keyword">not</span> dynamodb:</span><br><span class="line"> dynamodb = boto3.resource(<span class="string">'dynamodb'</span>, endpoint_url=<span class="string">"http://localhost:8000"</span>)</span><br><span class="line"></span><br><span class="line"> table = dynamodb.Table(<span class="string">'Movies'</span>)</span><br><span class="line"> <span class="keyword">for</span> movie <span class="keyword">in</span> movies:</span><br><span class="line"> <span class="comment"># 读取并显示数据到面板</span></span><br><span class="line"> year = int(movie[<span class="string">'year'</span>])</span><br><span class="line"> title = movie[<span class="string">'title'</span>]</span><br><span class="line"> print(<span class="string">"Adding movie:"</span>, year, title)</span><br><span class="line"> <span class="comment"># 加载数据</span></span><br><span class="line"> table.put_item(Item=movie)</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</span><br><span class="line"> <span class="keyword">with</span> open(<span class="string">"moviedata.json"</span>) <span class="keyword">as</span> json_file:</span><br><span class="line"> movie_list = json.load(json_file, parse_float=Decimal)</span><br><span class="line"> load_movies(movie_list)</span><br></pre></td></tr></table></figure></li></ul><h4 id="CRUD-操作"><a href="#CRUD-操作" class="headerlink" title="CRUD 操作"></a>CRUD 操作</h4><h5 id="向已存在表内创建新项目-put-item"><a href="#向已存在表内创建新项目-put-item" class="headerlink" title="向已存在表内创建新项目 put_item()"></a>向已存在表内创建新项目 put_item()</h5><ul><li><p>创建实例</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 创建新项目</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">put_movie</span><span class="params">(title, year, plot, rating, dynamodb=None)</span>:</span></span><br><span class="line"> <span class="keyword">if</span> <span class="keyword">not</span> dynamodb:</span><br><span class="line"> dynamodb = boto3.resource(<span class="string">'dynamodb'</span>, endpoint_url=<span class="string">'http://localhost:8000'</span>)</span><br><span class="line"></span><br><span class="line"> table = dynamodb.Table(<span class="string">'Movies'</span>)</span><br><span class="line"> response = table.put_item(</span><br><span class="line"> Item={</span><br><span class="line"> <span class="string">'year'</span>: year,</span><br><span class="line"> <span class="string">'title'</span>: title,</span><br><span class="line"> <span class="string">'info'</span>: {</span><br><span class="line"> <span class="string">'plot'</span>: plot,</span><br><span class="line"> <span class="string">'rating'</span>: rating</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> )</span><br><span class="line"> <span class="keyword">return</span> response</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</span><br><span class="line"> movie_resp = put_movie(<span class="string">'The Big New Movie'</span>, <span class="number">2015</span>, <span class="string">'Nothing happens at all.'</span>, <span class="number">0</span>)</span><br><span class="line"> print(<span class="string">'Put movie succeeded:'</span>)</span><br><span class="line"> pprint(movie_resp)</span><br></pre></td></tr></table></figure></li></ul><h5 id="指定主键值读取项目-get-item"><a href="#指定主键值读取项目-get-item" class="headerlink" title="指定主键值读取项目: get_item()"></a>指定主键值读取项目: get_item()</h5><ul><li><p><code>get_item(Key={'分区键名': data1, '排序键名: data2})</code></p></li><li><p>读取实例</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> boto3</span><br><span class="line"><span class="keyword">from</span> pprint <span class="keyword">import</span> pprint</span><br><span class="line"><span class="keyword">from</span> botocore.exceptions <span class="keyword">import</span> ClientError</span><br><span class="line"></span><br><span class="line"><span class="comment"># 读取数据</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">get_movie</span><span class="params">(title, year, dynamodb=None)</span>:</span></span><br><span class="line"> <span class="keyword">if</span> <span class="keyword">not</span> dynamodb:</span><br><span class="line"> dynamodb = boto3.resource(<span class="string">'dynamodb'</span>, endpoint_url=<span class="string">'http://localhost:8000'</span>)</span><br><span class="line"></span><br><span class="line"> table = dynamodb.Table(<span class="string">'Movies'</span>)</span><br><span class="line"></span><br><span class="line"> <span class="keyword">try</span>:</span><br><span class="line"> response = table.get_item(Key={<span class="string">'year'</span>: year, <span class="string">'title'</span>: title})</span><br><span class="line"> <span class="keyword">except</span> ClientError <span class="keyword">as</span> e:</span><br><span class="line"> print(e.response[<span class="string">'Error'</span>][<span class="string">'Message'</span>])</span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> <span class="keyword">return</span> response[<span class="string">'Item'</span>]</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</span><br><span class="line"> movie = get_movie(<span class="string">'The Big New Movie'</span>, <span class="number">2015</span>,)</span><br><span class="line"> <span class="keyword">if</span> movie:</span><br><span class="line"> print(<span class="string">'Get movie succeeded:'</span>)</span><br><span class="line"> pprint(movie)</span><br><span class="line"> <span class="comment"># python 3.6 没有 sort_dicts 形参</span></span><br><span class="line"> <span class="comment"># pprint(movie, sort_dicts=False)</span></span><br></pre></td></tr></table></figure></li></ul><h5 id="更新项目-更新现有属性的值、添加新属性或删除属性-update-item"><a href="#更新项目-更新现有属性的值、添加新属性或删除属性-update-item" class="headerlink" title="更新项目 (更新现有属性的值、添加新属性或删除属性): update_item()"></a>更新项目 (更新现有属性的值、添加新属性或删除属性): update_item()</h5><ul><li><p><code>update_item()</code> </p><ul><li>更改现有属性的值</li><li>向现有项目内添加新的列表属性</li></ul></li><li><p>注意事项</p><ul><li><code>DecimalEncoder</code> 类用于打印使用 <code>Decimal</code> 类存储的数字。BotoSDK 使用<code>Decimal</code>类来保存 Amazon DynamoDB 数字值。</li><li>使用 <code>UpdateExpression</code> 来描述您要对指定项目执行的所有更新</li><li><code>ReturnValues</code>参数用于指示 DynamoDB 仅返回更新后的属性 (<code>UPDATED_NEW</code>)</li></ul></li><li><p>更新实现</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> boto3</span><br><span class="line"><span class="keyword">from</span> decimal <span class="keyword">import</span> Decimal</span><br><span class="line"><span class="keyword">from</span> pprint <span class="keyword">import</span> pprint</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">update_movie</span><span class="params">(title, year, rating, plot, actors, dynamodb=None)</span>:</span></span><br><span class="line"> <span class="keyword">if</span> <span class="keyword">not</span> dynamodb:</span><br><span class="line"> dynamodb = boto3.resource(<span class="string">'dynamodb'</span>, endpoint_url=<span class="string">'http://localhost:8000'</span>)</span><br><span class="line"></span><br><span class="line"> table = dynamodb.Table(<span class="string">'Movies'</span>)</span><br><span class="line"></span><br><span class="line"> response = table.update_item(</span><br><span class="line"> Key={</span><br><span class="line"> <span class="string">'year'</span>: year,</span><br><span class="line"> <span class="string">'title'</span>: title</span><br><span class="line"> },</span><br><span class="line"> <span class="comment"># UpdateExpression 来描述您要对指定项目执行的所有更新。</span></span><br><span class="line"> <span class="comment"># 对更新内容设置</span></span><br><span class="line"> UpdateExpression=<span class="string">'set info.rating=:r, info.plot=:p, info.actors=:a'</span>,</span><br><span class="line"> ExpressionAttributeValues={</span><br><span class="line"> <span class="string">':r'</span>: Decimal(rating),</span><br><span class="line"> <span class="string">':p'</span>: plot,</span><br><span class="line"> <span class="string">':a'</span>: actors</span><br><span class="line"> },</span><br><span class="line"> <span class="comment"># ReturnValues参数用于指示 DynamoDB 仅返回更新后的属性 (UPDATED_NEW)。</span></span><br><span class="line"> ReturnValues=<span class="string">"UPDATED_NEW"</span></span><br><span class="line"> )</span><br><span class="line"> <span class="keyword">return</span> response</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</span><br><span class="line"> update_response = update_movie(</span><br><span class="line"> <span class="string">'The Big New Movie'</span>, <span class="number">2015</span>, <span class="number">5.5</span>, <span class="string">'Everything happens all at once.'</span>,</span><br><span class="line"> [<span class="string">'larry'</span>, <span class="string">'Moe'</span>, <span class="string">'Curly'</span>]</span><br><span class="line"> )</span><br><span class="line"> print(<span class="string">"Ypdate movie succeeded:"</span>)</span><br><span class="line"> pprint(update_response)</span><br></pre></td></tr></table></figure></li></ul><h5 id="增加原子计数器-update-item"><a href="#增加原子计数器-update-item" class="headerlink" title="增加原子计数器: update_item()"></a>增加原子计数器: update_item()</h5><ul><li><p><code>update_item()</code></p><ul><li>递增或递减现有属性的值而不干扰其他写入请求</li></ul></li><li><p>原子计数器实例</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> boto3</span><br><span class="line"><span class="keyword">from</span> decimal <span class="keyword">import</span> Decimal</span><br><span class="line"><span class="keyword">from</span> pprint <span class="keyword">import</span> pprint</span><br><span class="line"></span><br><span class="line"><span class="comment"># 增加原子计数器:使用update_item方法递增或递减现有属性的值而不干扰其他写入请求</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">increase_rating</span><span class="params">(title, year, rating_increase, dynamodb=None)</span>:</span></span><br><span class="line"> <span class="keyword">if</span> <span class="keyword">not</span> dynamodb:</span><br><span class="line"> dynamodb = boto3.resource(<span class="string">'dynamodb'</span>, endpoint_url=<span class="string">'http://localhost:8000'</span>)</span><br><span class="line"></span><br><span class="line"> table = dynamodb.Table(<span class="string">'Movies'</span>)</span><br><span class="line"> response = table.update_item(</span><br><span class="line"> Key={</span><br><span class="line"> <span class="string">'year'</span>: year,</span><br><span class="line"> <span class="string">'title'</span>: title</span><br><span class="line"> },</span><br><span class="line"> UpdateExpression=<span class="string">"set info.rating = info.rating + :val"</span>,</span><br><span class="line"> ExpressionAttributeValues={</span><br><span class="line"> <span class="string">':val'</span>: Decimal(rating_increase)</span><br><span class="line"> },</span><br><span class="line"> ReturnValues=<span class="string">'UPDATED_NEW'</span></span><br><span class="line"> )</span><br><span class="line"> <span class="keyword">return</span> response</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</span><br><span class="line"> update_response = increase_rating(<span class="string">'The Big New Movie'</span>, <span class="number">2015</span>, <span class="number">1</span>)</span><br><span class="line"> print(<span class="string">'Update movie succeeded:'</span>)</span><br><span class="line"> pprint(update_response)</span><br></pre></td></tr></table></figure></li></ul><h5 id="满足指定条件更新项目-update-item"><a href="#满足指定条件更新项目-update-item" class="headerlink" title="满足指定条件更新项目: update_item()"></a>满足指定条件更新项目: update_item()</h5><ul><li><p>注意事项</p><ul><li><code>ExpressionAttributeValues</code> 提供值替换功能。使用此参数是因为您不能在任何表达式 (包括 <code>KeyConditionExpression</code>) 中使用文本。您可使用表达式属性值 <code>:yyyy</code> 来解决此问题。</li></ul></li><li><p>更新实例</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> boto3</span><br><span class="line"><span class="keyword">from</span> pprint <span class="keyword">import</span> pprint</span><br><span class="line"><span class="keyword">from</span> botocore.exceptions <span class="keyword">import</span> ClientError</span><br><span class="line"></span><br><span class="line"><span class="comment"># 有条件更新项目 (演员数目)</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">remove_actors</span><span class="params">(title, year, actor_count, dynamodb=None)</span>:</span></span><br><span class="line"> <span class="keyword">if</span> <span class="keyword">not</span> dynamodb:</span><br><span class="line"> dynamodb = boto3.resource(<span class="string">'dynamodb'</span>, endpoint_url=<span class="string">'http://localhost:8000'</span>)</span><br><span class="line"></span><br><span class="line"> table = dynamodb.Table(<span class="string">'Movies'</span>)</span><br><span class="line"> <span class="keyword">try</span>:</span><br><span class="line"> response = table.update_item(</span><br><span class="line"> Key={</span><br><span class="line"> <span class="string">'year'</span>: year,</span><br><span class="line"> <span class="string">'title'</span>: title</span><br><span class="line"> },</span><br><span class="line"> UpdateExpression=<span class="string">'remove info.actors[0]'</span>,</span><br><span class="line"> ConditionExpression=<span class="string">"size(info.actors) >= :num"</span>,</span><br><span class="line"> ExpressionAttributeValues={<span class="string">':num'</span>: actor_count},</span><br><span class="line"> ReturnValues=<span class="string">"UPDATED_NEW"</span></span><br><span class="line"> )</span><br><span class="line"> <span class="keyword">except</span> ClientError <span class="keyword">as</span> e:</span><br><span class="line"> <span class="keyword">if</span> e.response[<span class="string">"Error"</span>][<span class="string">"Code"</span>] == <span class="string">"ConditionalCheckFailedException"</span>:</span><br><span class="line"> print(e.response[<span class="string">"Error"</span>][<span class="string">"Message"</span>])</span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> <span class="keyword">raise</span></span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> <span class="keyword">return</span> response</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</span><br><span class="line"> print(<span class="string">"Attempting conditional update (expecting failure) ..."</span>)</span><br><span class="line"> update_response = remove_actors(<span class="string">'The Big New Movie'</span>, <span class="number">2015</span>, <span class="number">3</span>)</span><br><span class="line"> <span class="keyword">if</span> update_response:</span><br><span class="line"> print(<span class="string">'Update movie succeeded:'</span>)</span><br><span class="line"> pprint(update_response)</span><br></pre></td></tr></table></figure></li></ul><h5 id="删除项目-delete-item"><a href="#删除项目-delete-item" class="headerlink" title="删除项目: delete_item()"></a>删除项目: delete_item()</h5><ul><li><p><code>delete_item()</code> </p><ul><li>通过指定项目主键删除项目</li></ul></li><li><p>删除实例</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> boto3</span><br><span class="line"><span class="keyword">from</span> pprint <span class="keyword">import</span> pprint</span><br><span class="line"><span class="keyword">from</span> decimal <span class="keyword">import</span> Decimal</span><br><span class="line"><span class="keyword">from</span> botocore.exceptions <span class="keyword">import</span> ClientError</span><br><span class="line"></span><br><span class="line"><span class="comment"># 删除项目 delete_item 通过指定主键删除</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">delete_underrated_movie</span><span class="params">(title, year, rating, dynamodb=None)</span>:</span></span><br><span class="line"> <span class="keyword">if</span> <span class="keyword">not</span> dynamodb:</span><br><span class="line"> dynamodb = boto3.resource(<span class="string">'dynamodb'</span>, endpoint_url=<span class="string">"http://localhost:8000"</span>)</span><br><span class="line"></span><br><span class="line"> table = dynamodb.Table(<span class="string">'Movies'</span>)</span><br><span class="line"></span><br><span class="line"> <span class="keyword">try</span>:</span><br><span class="line"> response = table.delete_item(</span><br><span class="line"> Key={</span><br><span class="line"> <span class="string">'year'</span>: year,</span><br><span class="line"> <span class="string">'title'</span>: title</span><br><span class="line"> },</span><br><span class="line"> ConditionExpression=<span class="string">"info.rating >= :val"</span>,</span><br><span class="line"> ExpressionAttributeValues={</span><br><span class="line"> <span class="string">":val"</span>: Decimal(rating)</span><br><span class="line"> }</span><br><span class="line"> )</span><br><span class="line"> <span class="keyword">except</span> ClientError <span class="keyword">as</span> e:</span><br><span class="line"> <span class="keyword">if</span> e.response[<span class="string">'Error'</span>][<span class="string">'Code'</span>] == <span class="string">"ConditionalCheckFailedException"</span>:</span><br><span class="line"> print(e.response[<span class="string">"Error"</span>][<span class="string">"Message"</span>])</span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> <span class="keyword">raise</span></span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> <span class="keyword">return</span> response</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</span><br><span class="line"> print(<span class="string">"Attempting a conditional delete ..."</span>)</span><br><span class="line"> delete_response = delete_underrated_movie(<span class="string">'The Big New Movie'</span>, <span class="number">2015</span>, <span class="number">5</span>)</span><br><span class="line"> <span class="keyword">if</span> delete_response:</span><br><span class="line"> print(<span class="string">'Delete movie succeeded:'</span>)</span><br><span class="line"> pprint(delete_response)</span><br></pre></td></tr></table></figure></li></ul><h4 id="查询和扫描数据"><a href="#查询和扫描数据" class="headerlink" title="查询和扫描数据"></a>查询和扫描数据</h4><ul><li><code>query()</code> : 检索表中数据,分区键值必选,排序键可选</li><li><code>scan()</code> : 检索表中所有数据</li></ul><h5 id="单条件查询-query"><a href="#单条件查询-query" class="headerlink" title="单条件查询: query()"></a>单条件查询: query()</h5><ul><li><p>注意事项</p><ul><li>使用从 <code>ConditionExpression</code> 导入的 <code>Key</code> 和 <code>Attr</code> 函数时,Boto 3 开发工具包会为您构建一个 <code>boto3.dynamodb.conditions</code>。您还可以字符串形式指定 <code>ConditionExpression</code>。</li></ul></li><li><p>查询实例</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> boto3</span><br><span class="line"><span class="keyword">from</span> boto3.dynamodb.conditions <span class="keyword">import</span> Key</span><br><span class="line"></span><br><span class="line"><span class="comment"># 根据分区键 query 查找 同一分区内数据</span></span><br><span class="line"><span class="comment"># 检索 year 1985 发行的所有电影。</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">query_movies</span><span class="params">(year, dynamodb=None)</span>:</span></span><br><span class="line"> <span class="keyword">if</span> <span class="keyword">not</span> dynamodb:</span><br><span class="line"> dynamodb = boto3.resource(<span class="string">'dynamodb'</span>, endpoint_url=<span class="string">'http://localhost:8000'</span>)</span><br><span class="line"></span><br><span class="line"> table = dynamodb.Table(<span class="string">'Movies'</span>)</span><br><span class="line"></span><br><span class="line"> response = table.query(</span><br><span class="line"> KeyConditionExpression=Key(<span class="string">'year'</span>).eq(year)</span><br><span class="line"> )</span><br><span class="line"> <span class="keyword">return</span> response[<span class="string">'Items'</span>]</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</span><br><span class="line"> query_year = <span class="number">1985</span></span><br><span class="line"> print(<span class="string">f'Movies from <span class="subst">{query_year}</span>'</span>)</span><br><span class="line"> movies = query_movies(query_year)</span><br><span class="line"> <span class="keyword">for</span> movie <span class="keyword">in</span> movies:</span><br><span class="line"> print(movie[<span class="string">'year'</span>], <span class="string">':'</span>, movie[<span class="string">'title'</span>])</span><br></pre></td></tr></table></figure></li></ul><h5 id="多条件混合查询"><a href="#多条件混合查询" class="headerlink" title="多条件混合查询"></a>多条件混合查询</h5><ul><li><p>查询实例</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> boto3</span><br><span class="line"><span class="keyword">from</span> pprint <span class="keyword">import</span> pprint</span><br><span class="line"><span class="keyword">from</span> boto3.dynamodb.conditions <span class="keyword">import</span> Key</span><br><span class="line"></span><br><span class="line"><span class="comment"># 多条件查询</span></span><br><span class="line"><span class="comment"># 将检索 year 1992 发行并且 title 以字母“A”至字母“L”开头的所有电影</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">query_and_project_movies</span><span class="params">(year, title_range, dynamodb=None)</span>:</span></span><br><span class="line"> <span class="keyword">if</span> <span class="keyword">not</span> dynamodb:</span><br><span class="line"> dynamodb = boto3.resource(<span class="string">'dynamodb'</span>, endpoint_url=<span class="string">'http://localhost:8000'</span>)</span><br><span class="line"></span><br><span class="line"> table = dynamodb.Table(<span class="string">'Movies'</span>)</span><br><span class="line"> print(<span class="string">f'获取年份、标题、类型和主角'</span>)</span><br><span class="line"></span><br><span class="line"> <span class="comment"># 表达式属性名称只能引用投影表达式中的项。</span></span><br><span class="line"> response = table.query(</span><br><span class="line"> ProjectionExpression=<span class="string">"#yr, title, info.genres, info.actors[0]"</span>,</span><br><span class="line"> ExpressionAttributeNames={<span class="string">'#yr'</span>: <span class="string">'year'</span>},</span><br><span class="line"> KeyConditionExpression=</span><br><span class="line"> Key(<span class="string">'year'</span>).eq(year) & Key(<span class="string">'title'</span>).between(title_range[<span class="number">0</span>], title_range[<span class="number">1</span>])</span><br><span class="line"> )</span><br><span class="line"> <span class="keyword">return</span> response[<span class="string">'Items'</span>]</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</span><br><span class="line"> query_year = <span class="number">1992</span></span><br><span class="line"> query_range = (<span class="string">'A'</span>, <span class="string">'L'</span>)</span><br><span class="line"> print(<span class="string">f'Get movies from <span class="subst">{query_year}</span> with titles from <span class="subst">{query_range[<span class="number">0</span>]}</span> to <span class="subst">{query_range[<span class="number">1</span>]}</span>'</span>)</span><br><span class="line"> movies = query_and_project_movies(query_year, query_range)</span><br><span class="line"> <span class="keyword">for</span> movie <span class="keyword">in</span> movies:</span><br><span class="line"> print(<span class="string">f'\n<span class="subst">{movie[<span class="string">"year"</span>]}</span> : <span class="subst">{movie[<span class="string">"title"</span>]}</span>'</span>)</span><br><span class="line"> pprint(movie[<span class="string">'info'</span>])</span><br></pre></td></tr></table></figure></li></ul><h5 id="扫描数据-scan"><a href="#扫描数据-scan" class="headerlink" title="扫描数据: scan()"></a>扫描数据: scan()</h5><ul><li><p><code>scan()</code></p><ul><li>读取整个表中的所有项目,并返回表中的所有数据</li><li>提供一个可选的 <code>filter_expression</code>,以便仅返回符合条件的项目。但是,筛选条件仅在扫描整个表后应用。</li></ul></li><li><p>注意事项</p><ul><li><code>FilterExpression</code> 用于指定一个条件,以便仅返回符合条件的项目。所有其他项目都将被舍弃</li><li><code>ProjectionExpression</code> 用于指定要在扫描结果中包含的属性。</li><li>响应中的 <code>LastEvaluatedKey</code> 值随后通过 <code>scan</code> 参数传递给 <code>ExclusiveStartKey</code> 方法。当返回最后一页后,<code>LastEvaluatedKey</code> 将不是响应的一部分。</li><li><code>ExpressionAttributeNames</code> 提供名称替换功能。我们使用这个是因为<code>year</code>是 DynamoDB 中的保留字您不能直接在任何表达式中使用它,包括<code>KeyConditionExpression</code>。您可使用表达式属性名称 <code>#yr</code> 来解决此问题</li></ul></li><li><p>扫描实例</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> boto3</span><br><span class="line"><span class="keyword">from</span> pprint <span class="keyword">import</span> pprint</span><br><span class="line"><span class="keyword">from</span> boto3.dynamodb.conditions <span class="keyword">import</span> Key</span><br><span class="line"></span><br><span class="line"><span class="comment"># scan 方法将读取整个表中的所有项目,并返回表中的所有数据。</span></span><br><span class="line"><span class="comment"># 可提供一个可选的 filter_expression,以便仅返回符合条件的项目</span></span><br><span class="line"><span class="comment"># 筛选条件仅在扫描整个表后应用, 不适合单纯的查找操作</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 扫描整个 Movies 表,表中包含约 5000 个项目。</span></span><br><span class="line"><span class="comment"># 扫描时可指定可选筛选条件,以便仅检索 20 世纪 50 年代以来的电影 (约 100 个项目),同时舍弃所有其他项目</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">scan_movies</span><span class="params">(year_range, display_movies, dynamodb=None)</span>:</span></span><br><span class="line"> <span class="keyword">if</span> <span class="keyword">not</span> dynamodb:</span><br><span class="line"> dynamodb = boto3.resource(<span class="string">'dynamodb'</span>, endpoint_url=<span class="string">'http://localhost:8000'</span>)</span><br><span class="line"></span><br><span class="line"> table = dynamodb.Table(<span class="string">"Movies"</span>)</span><br><span class="line"> scan_kwargs = {</span><br><span class="line"> <span class="comment"># 指定一个条件,以便仅返回符合条件的项目。所有其他项目都将被舍弃。</span></span><br><span class="line"> <span class="string">'FilterExpression'</span>: Key(<span class="string">'year'</span>).between(*year_range),</span><br><span class="line"> <span class="comment"># 指定要在扫描结果中包含的属性</span></span><br><span class="line"> <span class="string">'ProjectionExpression'</span>: <span class="string">"#yr, title, info.rating"</span>,</span><br><span class="line"> <span class="comment"># ExpressionAttributeNames 提供名称替换功能。我们使用这个是因为year是 DynamoDB 中的保留字您不能直接在任何表达式中使用它,</span></span><br><span class="line"> <span class="comment"># 包括KeyConditionExpression。您可使用表达式属性名称 #yr 来解决此问题。</span></span><br><span class="line"> <span class="string">'ExpressionAttributeNames'</span>: {<span class="string">'#yr'</span>: <span class="string">"year"</span>}</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> done = <span class="literal">False</span></span><br><span class="line"> start_key = <span class="literal">None</span></span><br><span class="line"> <span class="keyword">while</span> <span class="keyword">not</span> done:</span><br><span class="line"> <span class="keyword">if</span> start_key:</span><br><span class="line"> scan_kwargs[<span class="string">'ExclusiveStartKey'</span>] = start_key</span><br><span class="line"> response = table.scan(**scan_kwargs)</span><br><span class="line"> <span class="comment"># 调用外部的 print_movies 进行输出</span></span><br><span class="line"> display_movies(response.get(<span class="string">'Items'</span>, []))</span><br><span class="line"> start_key = response.get(<span class="string">'LastEvaluatedKey'</span>, <span class="literal">None</span>)</span><br><span class="line"> done = start_key <span class="keyword">is</span> <span class="literal">None</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">print_movies</span><span class="params">(movies)</span>:</span></span><br><span class="line"> <span class="keyword">for</span> movie <span class="keyword">in</span> movies:</span><br><span class="line"> print(<span class="string">f'\n<span class="subst">{movie[<span class="string">"year"</span>]}</span> : <span class="subst">{movie[<span class="string">"title"</span>]}</span>'</span>)</span><br><span class="line"> pprint(movie[<span class="string">'info'</span>])</span><br><span class="line"></span><br><span class="line"> query_range = (<span class="number">1950</span>, <span class="number">1959</span>)</span><br><span class="line"> print(<span class="string">f"Scanning for movies released from <span class="subst">{query_range[<span class="number">0</span>]}</span> to <span class="subst">{query_range[<span class="number">1</span>]}</span>..."</span>)</span><br><span class="line"> scan_movies(query_range, print_movies)</span><br></pre></td></tr></table></figure></li></ul><h4 id="删除表"><a href="#删除表" class="headerlink" title="删除表"></a>删除表</h4><h5 id="删除已创建的表-delete"><a href="#删除已创建的表-delete" class="headerlink" title="删除已创建的表: delete()"></a>删除已创建的表: delete()</h5><ul><li><p>删除实例</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> boto3</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">delete_movie_table</span><span class="params">(dynamodb=None)</span>:</span></span><br><span class="line"> <span class="keyword">if</span> <span class="keyword">not</span> dynamodb:</span><br><span class="line"> dynamodb = boto3.resource(<span class="string">'dynamodb'</span>, endpoint_url=<span class="string">"http://localhost:8000"</span>)</span><br><span class="line"></span><br><span class="line"> table = dynamodb.Table(<span class="string">'Movies'</span>)</span><br><span class="line"> table.delete()</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</span><br><span class="line"> delete_movie_table()</span><br><span class="line"> print(<span class="string">'Movies table deleted.'</span>)</span><br></pre></td></tr></table></figure></li></ul>]]></content>
</entry>
<entry>
<title>redis_basis</title>
<link href="/2021/03/01/redis-basis/"/>
<url>/2021/03/01/redis-basis/</url>
<content type="html"><![CDATA[<h3 id="Redis-数据结构"><a href="#Redis-数据结构" class="headerlink" title="Redis 数据结构"></a>Redis 数据结构</h3><h4 id="一、Redis基础知识"><a href="#一、Redis基础知识" class="headerlink" title="一、Redis基础知识"></a>一、Redis基础知识</h4><ol><li><p>redis 启动客户端与服务器命令</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">redis-server [服务器配置文件] : 服务器命令 [启动特定端口的服务器,默认端口为 6379]</span><br><span class="line"></span><br><span class="line">redis-cli [-p 端口号]: 客户端命令 [启动一个特定端口的客户端,默认端口为 6379]</span><br></pre></td></tr></table></figure><ul><li><p>一个redis.windows.conf配置就是一个redis服务器。需要启动多个服务器时,只需要修改一下这个配置文件redis.windows.conf的名称,更改端口号,再用redis-server就可以启动。</p><ol><li><p>复制配置文件 redis.windows.conf 并改名</p><p><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/setfile.png" alt></p></li><li><p>在新配置中更改端口号</p><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/reset_port.png" style="zoom:50%;"></li><li><p>使用启动命令启动服务端</p><ol><li><p>启动默认服务器</p><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/morenportserver.png" style="zoom:50%;"></li><li><p>启动修改后端口服务器</p><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/6308portserver.png" style="zoom:50%;"></li></ol></li><li><p>使用启动命令启动客户端</p><ol><li><p>启动默认客户端</p><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/morenportclient.png" style="zoom:50%;"></li><li><p>启动修改端口后客户端</p><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/6308portclient.png" style="zoom:50%;"></li></ol></li></ol></li></ul></li><li><p>redis 和 memcached 独特之处</p><ul><li><span style="color: red">redis 可以<b>持久化保存</b>数据到硬盘;memcached 只可以做缓存</span></li><li><span style="color: red">redis 相比于 memcached,<b>支持数据类型更多</b></span></li></ul></li><li><p>redis 优点</p><ol><li><b style="color: red">读写速度快</b>: redis 数据存放在内存内,数据结构类似于 HashMap, HashMap 的优势就是<strong>查找和操作时间复杂度都是O(1)</strong></li><li><b style="color: red">支持丰富的数据类型</b>: string, hash, list, set, sorted</li><li><b style="color: red">支持简单的事务</b>: watch</li><li><b style="color: red">丰富的特性</b>: 可以用于缓存,消息队列,按 key 设置过期时间,到期后自动删除</li><li><b style="color: red">支持数据持久化</b>: 支持AOF和RDB 两种持久化方式,从而进行数据的恢复操作,可以有效防止数据丢失</li><li><b style="color: red">支持主从复制实现数据备份</b>: 主机会自动将数据同步到从机</li></ol></li><li><p>Redis 目录下的重要文件意义</p><ul><li><strong>redis-benchmark</strong> :性能测试工具</li><li><strong>redis-check-aof</strong>: 日志文件检测工具 (可以检测并修复由于各种突发原因造成的日志损坏, eg:断电)</li><li><strong>redis-check-dump</strong>: 快照文件检测工具 (可以检测并修复由于各种突发原因造成的快照损坏, eg:断电)</li><li><strong>redis-cli</strong>: redis 客户端</li><li><strong>redis-server</strong>: redis 服务器</li><li><strong>redis.windows.conf</strong>: redis 配置文件 <span style="color:red">在启动 redis 时 一个配置文件相当于一个服务器数据库</span></li></ul></li><li><p>redis基础命令</p><ul><li><code>keys *</code> : 返回所有键 (key)</li><li><code>keys na*</code>: 返回所有以 na 开头的键</li><li><code>exists name</code>: 判断 name 键 是否存在</li><li><code>del name</code>: 删除 name 键</li><li><code>expire key_name 10</code>: 设置 key_name 键的过期时间为 10s 后 (自动删除) <ul><li>通过这种有效期可以做到自动删除,其实有效期到了,也不是立马删除的,删除的时机是有配置文件决定的</li></ul></li><li><code>ttl key_name</code>: 查看 key_name 键的过期时间,-1 表示已过期 或永不过期</li><li><code>move name 1</code>: 将键名为 name 的键转移到 1 的数据库</li><li><code>select 1</code>: 进入到 1 的数据库中,默认为在 0 数据库</li><li><code>persist name</code>: 移除 name 键的过期时间</li><li><code>flushdb</code>: 删除所有的数据,清除当前所在库的所有数据</li><li><code>flushall</code>: 清空所有数据</li></ul></li></ol><h4 id="二、redis-数据类型"><a href="#二、redis-数据类型" class="headerlink" title="二、redis 数据类型"></a>二、redis 数据类型</h4><h5 id="1-String-字符串"><a href="#1-String-字符串" class="headerlink" title="1. String 字符串"></a>1. String 字符串</h5><ul><li><p>十二种方法</p><ol><li><p><strong>set 方法</strong>: 设置 key 对应的值为 <strong>string 类型的 value</strong>,如果 key 已经存在,则覆盖 key 对应的 value 值.(在 redis 中 key 只能有一个)</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379[1]> set name liuliu</span><br><span class="line">OK</span><br></pre></td></tr></table></figure></li><li><p><strong>get 方法</strong>: 根据 key 获取 value 值</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379[1]> get name</span><br><span class="line">"liuliu"</span><br></pre></td></tr></table></figure></li><li><p><strong>setnx 方法</strong>: 设置一个不存在的字符串,返回 0 表示设置失败,已存在;返回 1 标识设置新值成功(nx: not exist) </p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379[1]> setnx name angel</span><br><span class="line">(integer) 0</span><br><span class="line">127.0.0.1:6379[1]> setnx age 23</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379[1]> keys *</span><br><span class="line">1) "name"</span><br><span class="line">2) "age"</span><br><span class="line">127.0.0.1:6379[1]> get name</span><br><span class="line">"liuliu" </span><br><span class="line">127.0.0.1:6379[1]> get age</span><br><span class="line">"23"</span><br></pre></td></tr></table></figure></li><li><p><strong>setex</strong>: 设置字符串,同时设置有效期。(ex – expire 有效期)</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379[1]> setex color 20 red</span><br><span class="line">OK</span><br><span class="line">立即查看 keys</span><br><span class="line">127.0.0.1:6379[1]> keys *</span><br><span class="line">1) "color"</span><br><span class="line">2) "name"</span><br><span class="line">3) "age"</span><br><span class="line">127.0.0.1:6379[1]> get color</span><br><span class="line">"red"</span><br><span class="line">20s 后查看 keys</span><br><span class="line">127.0.0.1:6379[1]> keys *</span><br><span class="line">1) "name"</span><br><span class="line">2) "age"</span><br><span class="line">127.0.0.1:6379[1]> get color</span><br><span class="line">(nil)</span><br></pre></td></tr></table></figure></li><li><p><strong>setrange</strong>: 替换字符串: <code>etrange key offset value</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379[1]> get name</span><br><span class="line">"liuliu"</span><br><span class="line">127.0.0.1:6379[1]> setrange name 3 fei</span><br><span class="line">(integer) 6</span><br><span class="line">127.0.0.1:6379[1]> get name</span><br><span class="line">"liufei"</span><br></pre></td></tr></table></figure></li><li><p><strong>mset</strong>: 一次设置多个 key-value, 返回 ok 表示设置成功,返回 0 表示全部失败。如果存在则替换.(m – multi) <code>mset key value [key value ...]</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379[1]> mset name1 lisi name2 zhangsan</span><br><span class="line">OK</span><br><span class="line">127.0.0.1:6379[1]> keys *</span><br><span class="line">1) "name1"</span><br><span class="line">2) "name"</span><br><span class="line">3) "name2"</span><br><span class="line">4) "age"</span><br><span class="line">127.0.0.1:6379[1]> get name1</span><br><span class="line">"lisi"</span><br><span class="line">127.0.0.1:6379[1]> get name2</span><br><span class="line">"zhangsan"</span><br></pre></td></tr></table></figure></li><li><p><strong>msetnx</strong>: 一次设置多个不存在的 key-value,返回 1 表示全部设置成功,返回 0 表示全部失败<code>msetnx key value [key value ...]</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">设置已存在的 keys 返回 0</span><br><span class="line">127.0.0.1:6379[1]> msetnx name1 wangwu name3 xiaoer</span><br><span class="line">(integer) 0</span><br><span class="line">127.0.0.1:6379[1]> keys *</span><br><span class="line">1) "name1"</span><br><span class="line">2) "name"</span><br><span class="line">3) "name2"</span><br><span class="line">4) "age"</span><br><span class="line">设置不存在的 keys</span><br><span class="line">127.0.0.1:6379[1]> msetnx name3 wangwu name4 xiaoer</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379[1]> keys *</span><br><span class="line">1) "name1"</span><br><span class="line">2) "name3"</span><br><span class="line">3) "age"</span><br><span class="line">4) "name2"</span><br><span class="line">5) "name4"</span><br><span class="line">6) "name"</span><br></pre></td></tr></table></figure></li><li><p><b style="color: red">getset</b>: 获取原值,并设置新值<code>getset key value</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379[1]> get age</span><br><span class="line">"23"</span><br><span class="line">127.0.0.1:6379[1]> getset age 110</span><br><span class="line">"23"</span><br><span class="line">127.0.0.1:6379[1]> get age</span><br><span class="line">"110"</span><br></pre></td></tr></table></figure></li><li><p><strong>getrange</strong>: 获取 key 对应 value 指定范围内的的字符串<code>getrange key start end</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379[1]> get name1</span><br><span class="line">"lisi"</span><br><span class="line">127.0.0.1:6379[1]> getrange name1 1 3</span><br><span class="line">"isi"</span><br></pre></td></tr></table></figure></li><li><p><strong>mget</strong>: 一次获取多个 key 对应的 value 值,不存在则返回 nil <code>mget key [key ...]</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379[1]> keys *</span><br><span class="line">1) "name1"</span><br><span class="line">2) "name3"</span><br><span class="line">3) "age"</span><br><span class="line">4) "name2"</span><br><span class="line">5) "name4"</span><br><span class="line">6) "name"</span><br><span class="line">7) "gae"</span><br><span class="line">127.0.0.1:6379[1]> mget name name1 name2 name3 name4 name5 name6</span><br><span class="line">1) "liufei"</span><br><span class="line">2) "lisi"</span><br><span class="line">3) "zhangsan"</span><br><span class="line">4) "wangwu"</span><br><span class="line">5) "xiaoer"</span><br><span class="line">6) (nil)</span><br><span class="line">7) (nil)</span><br></pre></td></tr></table></figure></li><li><p><strong>incr</strong>: 对 key 对应的 value 做<strong>加一</strong>操作,并返回新值 <code>incr key</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379[1]> get age</span><br><span class="line">"110"</span><br><span class="line">127.0.0.1:6379[1]> incr age</span><br><span class="line">(integer) 111</span><br><span class="line">127.0.0.1:6379[1]> get age</span><br><span class="line">"111"</span><br></pre></td></tr></table></figure></li><li><p><strong>incrby</strong>: 与 incr 类似,<strong>加指定值</strong>,key不存在的时候会创建 key, 并认为 该 key 原 value=0 <code>incrby key increment</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379[1]> get age</span><br><span class="line">"111"</span><br><span class="line">127.0.0.1:6379[1]> incrby age 50</span><br><span class="line">(integer) 161</span><br><span class="line">127.0.0.1:6379[1]> get age</span><br><span class="line">"161"</span><br><span class="line">127.0.0.1:6379[1]> keys *</span><br><span class="line">1) "name1"</span><br><span class="line">2) "name3"</span><br><span class="line">3) "age"</span><br><span class="line">4) "name2"</span><br><span class="line">5) "name4"</span><br><span class="line">6) "name"</span><br><span class="line">7) "gae"</span><br><span class="line">127.0.0.1:6379[1]> incrby length 180</span><br><span class="line">(integer) 180</span><br><span class="line">127.0.0.1:6379[1]> get length</span><br><span class="line">"180"</span><br></pre></td></tr></table></figure></li><li><p><strong>decr</strong>:对 key 对应的 value 做 <strong>减一</strong>操作 <code>decr key</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379[1]> get age</span><br><span class="line">"161"</span><br><span class="line">127.0.0.1:6379[1]> decr age</span><br><span class="line">(integer) 160</span><br><span class="line">127.0.0.1:6379[1]> get age</span><br><span class="line">"160"</span><br></pre></td></tr></table></figure></li><li><p><strong>decrby</strong>: 对 key 对应的 value 减去指定的值 <code>decrby key decrement</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379[1]> get age</span><br><span class="line">"160"</span><br><span class="line">127.0.0.1:6379[1]> decrby age 100</span><br><span class="line">(integer) 60</span><br><span class="line">127.0.0.1:6379[1]> get age</span><br><span class="line">"60"</span><br></pre></td></tr></table></figure></li><li><p><strong>append</strong>: 对 key 对应的 value 字符串追加,返回新字符串的长度 <code>append key value</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379[1]> get name3</span><br><span class="line">"wangwu"</span><br><span class="line">127.0.0.1:6379[1]> append name3 test</span><br><span class="line">(integer) 10</span><br><span class="line">127.0.0.1:6379[1]> get name3</span><br><span class="line">"wangwutest"</span><br></pre></td></tr></table></figure></li><li><p><strong>strlen</strong>: 获取 key 对应 value 的长度<code>strlen key</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379[1]> strlen name1</span><br><span class="line">(integer) 4</span><br><span class="line">127.0.0.1:6379[1]> get name1</span><br><span class="line">"lisi"</span><br></pre></td></tr></table></figure></li></ol></li></ul><h5 id="2-Hash-类型"><a href="#2-Hash-类型" class="headerlink" title="2. Hash 类型"></a>2. Hash 类型</h5><ul><li><p>Hash 类型介绍</p><ul><li>Redis hash 是一个 <strong>string 类型的 filed 和value 的映射表</strong>。相较于对象的每个字段存成单个 string 类型。将一个对象存储在 hash 类型中会占用更少的内存,并且可以更方便存储整个对象</li></ul></li><li><p>十一种类型</p><ol><li><p><strong>hset</strong>:设置 hash filed 为指定值,如果 key 不存在则先创建。key 存在则替换 <code>hset key field value</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379[1]> keys *</span><br><span class="line">1) "name1"</span><br><span class="line">2) "name3"</span><br><span class="line">3) "name4"</span><br><span class="line">4) "name2"</span><br><span class="line">5) "age"</span><br><span class="line">6) "name"</span><br><span class="line">7) "gae"</span><br><span class="line">8) "length"</span><br><span class="line">设置一个 user: 001 的用户 name 为 liuliu (可以将 user:001看作一个表)</span><br><span class="line">127.0.0.1:6379[1]> hset user:001 name liuliu</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379[1]> keys *</span><br><span class="line">1) "name1"</span><br><span class="line">2) "name3"</span><br><span class="line">3) "name4"</span><br><span class="line">4) "name2"</span><br><span class="line">5) "age"</span><br><span class="line">6) "name"</span><br><span class="line">7) "user:001"</span><br><span class="line">8) "gae"</span><br><span class="line">9) "length"</span><br></pre></td></tr></table></figure></li><li><p><strong>hget</strong>: 获取指定 field 字段的值<code>hget key field</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379[1]> hget user name</span><br><span class="line">(nil)</span><br><span class="line">127.0.0.1:6379[1]> hget user:001 name</span><br><span class="line">"liuliu"</span><br></pre></td></tr></table></figure></li><li><p><strong>hsetnx</strong>: 设置hash filed为指定值,如果key不存在,则先创建。如果<strong>key存在</strong>则<strong>返回0表示设置失败</strong>。<code>hsetnx key field value</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379[1]> hsetnx user:001 name lisi</span><br><span class="line">(integer) 0</span><br><span class="line">127.0.0.1:6379[1]> hsetnx user:001 age 19</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379[1]> hget user:001 age</span><br><span class="line">"19"</span><br><span class="line">127.0.0.1:6379[1]> hget user:001 name</span><br><span class="line">"liuliu"</span><br></pre></td></tr></table></figure></li><li><p><strong>hmset</strong>: 同时设置 hash 的多个 file <code>hmset key field value [field value ...]</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379[1]> hmset user:002 name wangwu age 19</span><br><span class="line">OK</span><br></pre></td></tr></table></figure></li><li><p><strong>hmget</strong>: 获取全部指定的 hash filed, 必需指定获取的 key 的名称 <code>hmget key field [field ...]</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379[1]> hmget user:002 name age</span><br><span class="line">1) "wangwu"</span><br><span class="line">2) "19"</span><br></pre></td></tr></table></figure></li><li><p><strong>hincrby</strong>: 对 hash filed 加上指定的值 <code>hincrby key field increment</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379[1]> hget user:001 age</span><br><span class="line">"19"</span><br><span class="line">127.0.0.1:6379[1]> hincrby user:001 age 10</span><br><span class="line">(integer) 29</span><br><span class="line">127.0.0.1:6379[1]> hget user:001 age</span><br><span class="line">"29"</span><br></pre></td></tr></table></figure></li><li><p><strong>hexists</strong>: 测试制定的 filed 是否存在,<strong>返回1表示存在,返回0表示不存在</strong> <code>hexists key field</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379[1]> hexists user:001 name</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379[1]> hexists user:001 age</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379[1]> hexists user:001 weight</span><br><span class="line">(integer) 0</span><br></pre></td></tr></table></figure></li><li><p><strong>hlen</strong>: 返回指定 hash 的 filed 的数量 <code>hlen key</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379[1]> hlen user:001</span><br><span class="line">(integer) 2</span><br></pre></td></tr></table></figure></li><li><p><strong>hdel</strong>: 删除指定 hash 的 field 字段,返回1表示删除成功,0表示删除失败 <code>hdel key field [field ...]</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379> hget user:001 age</span><br><span class="line">"29"</span><br><span class="line">127.0.0.1:6379> hdel user:001 age</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379> hget user:001 age</span><br><span class="line">(nil)</span><br></pre></td></tr></table></figure></li><li><p><strong>hkeys</strong>: 返回 hash 的所有 filed <code>hkeys key</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379> hkeys user:002</span><br><span class="line">1) "name"</span><br><span class="line">2) "age"</span><br></pre></td></tr></table></figure></li><li><p><strong>hvals</strong>: 返回 hash 的所有 value <code>hvals key</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379> hvals user:002</span><br><span class="line">1) "wangwu"</span><br><span class="line">2) "19"</span><br></pre></td></tr></table></figure></li></ol></li></ul><h5 id="3-List-类型"><a href="#3-List-类型" class="headerlink" title="3. List 类型"></a>3. List 类型</h5><ul><li><p>List 类型介绍</p><ul><li>List 是一个链表结构,主要功能是 <strong>push</strong>、<strong>pop</strong>,获取一个范围的所有值等等,操作中 key 理解为链表的名字。</li><li>Redis 的 list 类型其实就是每个子元素都是 string类型的<strong>双向链表</strong>.<b style="color: red">我们可以通过push、pop操作链表的头部或者链表尾部添加元素,这样list既可以作为栈,又可以作为队列。</b></li></ul></li><li><p>十二种方法</p><ol><li><p><strong>lpush</strong>: 在 key 对应 lisy 的头部添加字符串元素,返回 list 中元素的个数 <code>lpush key value [value ...]</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379> lpush list1 "hello"</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379> lpush list1 " " "word"</span><br><span class="line">(integer) 3</span><br><span class="line">127.0.0.1:6379> lrange list1 0 -1 (0 -1 第一个到最后一个)</span><br><span class="line">1) "word"</span><br><span class="line">2) " "</span><br><span class="line">3) "hello"</span><br></pre></td></tr></table></figure></li><li><p><strong>lrange</strong>: 获取 list 中的元素 <code>lrange key start[0:第一个] stop[-1:最后一个]</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379> lrange list1 0 -1 (0 -1 第一个到最后一个)</span><br><span class="line">1) "word"</span><br><span class="line">2) " "</span><br><span class="line">3) "hello"</span><br></pre></td></tr></table></figure></li><li><p><strong>rpush</strong>: 在 key 对应的 list 尾部添加元素 <code>rpush key value [value ...]</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379> rpush list1 "abc" "test"</span><br><span class="line">(integer) 5</span><br><span class="line">127.0.0.1:6379> lrange list1 0 -1</span><br><span class="line">1) "word"</span><br><span class="line">2) " "</span><br><span class="line">3) "hello"</span><br><span class="line">4) "abc"</span><br><span class="line">5) "test"</span><br></pre></td></tr></table></figure></li><li><p><strong>linsert</strong>: 在 key 对应 list 的特定位置前或后添加字符串 <code>linsert key BEFORE|AFTER pivot value</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379> linsert list1 before abc "test0"</span><br><span class="line">(integer) 6</span><br><span class="line">127.0.0.1:6379> lrange list1 0 -1</span><br><span class="line">1) "word"</span><br><span class="line">2) " "</span><br><span class="line">3) "hello"</span><br><span class="line">4) "test0"</span><br><span class="line">5) "abc"</span><br><span class="line">6) "test"</span><br></pre></td></tr></table></figure></li><li><p><strong>lset</strong>: 更改 list 中指定下标的元素,返回 ok 则设置成功 <code>lset key index value</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379> lrange list1 0 -1</span><br><span class="line">1) "word"</span><br><span class="line">2) " "</span><br><span class="line">3) "hello"</span><br><span class="line">4) "test0"</span><br><span class="line">5) "abc"</span><br><span class="line">6) "test"</span><br><span class="line">127.0.0.1:6379> lset list1 2 "liuliu"</span><br><span class="line">OK</span><br><span class="line">127.0.0.1:6379> lrange list1 0 -1</span><br><span class="line">1) "word"</span><br><span class="line">2) " "</span><br><span class="line">3) "liuliu"</span><br><span class="line">4) "test0"</span><br><span class="line">5) "abc"</span><br><span class="line">6) "test"</span><br></pre></td></tr></table></figure></li><li><p><strong>irem</strong>: 从 key 对应list 中删除 n 个和 value 相同的元素 (n<0 从未不删除, n=0 全部删除,n>0从头不删除) <code>lrem key count value</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379> lpush list2 "one" "two" "three"</span><br><span class="line">(integer) 3</span><br><span class="line">127.0.0.1:6379> lrange list2 0 -1</span><br><span class="line">1) "three"</span><br><span class="line">2) "two"</span><br><span class="line">3) "one"</span><br><span class="line">127.0.0.1:6379> lrem list2 1 one</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379> lrange list2 0 -1</span><br><span class="line">1) "three"</span><br><span class="line">2) "two"</span><br><span class="line">127.0.0.1:6379> lrem list2 0 three</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379> lrange list2 0 -1</span><br><span class="line">1) "two"</span><br></pre></td></tr></table></figure></li><li><p><b style="color: red">ltrim</b>: 保留 list 中指定范围的数据,其他的批量删除 <code>ltrim key start stop</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379> lrange list2 0 -1</span><br><span class="line">1) "three"</span><br><span class="line">2) "one"</span><br><span class="line">3) "two"</span><br><span class="line">127.0.0.1:6379> ltrim list2 1 2</span><br><span class="line">OK</span><br><span class="line">127.0.0.1:6379> lrange list2 0 -1</span><br><span class="line">1) "one"</span><br><span class="line">2) "two"</span><br></pre></td></tr></table></figure></li><li><p><strong>lpop</strong>: 从 list 的头部删除元素,并返回该元素 <code>lpop key</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379> lrange list2 0 -1</span><br><span class="line">1) "one"</span><br><span class="line">2) "two"</span><br><span class="line">127.0.0.1:6379> lpop list2</span><br><span class="line">"one"</span><br><span class="line">127.0.0.1:6379> lrange list2 0 -1</span><br><span class="line">1) "two"</span><br></pre></td></tr></table></figure></li><li><p><strong>rpop</strong>: 从 list 尾部删除元素,并返回该元素 <code>rpop key</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379> lrange list2 0 -1</span><br><span class="line">1) "three"</span><br><span class="line">2) "one"</span><br><span class="line">3) "two"</span><br><span class="line">127.0.0.1:6379> rpop list2</span><br><span class="line">"two"</span><br><span class="line">127.0.0.1:6379> lrange list2 0 -1</span><br><span class="line">1) "three"</span><br><span class="line">2) "one"</span><br></pre></td></tr></table></figure></li><li><p><strong>rpoplpush</strong>: 从第一个 list 的尾部删除元素,并添加到第二个 list 的头部 <code>rpoplpush source destination</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379> lrange list2 0 -1</span><br><span class="line">1) "three"</span><br><span class="line">2) "one"</span><br><span class="line">127.0.0.1:6379> lrange list1 0 -1</span><br><span class="line">1) "word"</span><br><span class="line">2) " "</span><br><span class="line">3) "liuliu"</span><br><span class="line">4) "test0"</span><br><span class="line">5) "abc"</span><br><span class="line">6) "test"</span><br><span class="line">127.0.0.1:6379> rpoplpush list1 list2</span><br><span class="line">"test"</span><br><span class="line">127.0.0.1:6379> lrange list1 0 -1</span><br><span class="line">1) "word"</span><br><span class="line">2) " "</span><br><span class="line">3) "liuliu"</span><br><span class="line">4) "test0"</span><br><span class="line">5) "abc"</span><br><span class="line">127.0.0.1:6379> lrange list2 0 -1</span><br><span class="line">1) "test"</span><br><span class="line">2) "three"</span><br><span class="line">3) "one"</span><br></pre></td></tr></table></figure></li><li><p><strong>lindex</strong>: 返回名称为 key 的 list 中index 位置的元素 <===> list[index] <code>lindex key index</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379> lrange list2 0 -1</span><br><span class="line">1) "test"</span><br><span class="line">2) "three"</span><br><span class="line">3) "one"</span><br><span class="line">127.0.0.1:6379> lindex list2 2</span><br><span class="line">"one"</span><br></pre></td></tr></table></figure></li><li><p><b style="color: red">llen</b>: 返回指定 key 对应的 list 的长度 <===> len(list) <code>llen key</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379> llen list2</span><br><span class="line">(integer) 3</span><br><span class="line">127.0.0.1:6379> lrange list2 0 -1</span><br><span class="line">1) "test"</span><br><span class="line">2) "three"</span><br><span class="line">3) "one"</span><br></pre></td></tr></table></figure></li></ol></li></ul><h5 id="4-Sets-类型"><a href="#4-Sets-类型" class="headerlink" title="4. Sets 类型"></a>4. Sets 类型</h5><ul><li><p>Sets 类型介绍</p><ul><li>Set 是一个 string 的,<b style="color: red">不允许重复</b>。Set 是通过 hash table 实现的.添加、删除、查找的复杂度都是 O(1).对集合我们可以取并集、交集、差集</li></ul></li><li><p>十四种方法</p><ol><li><p><b style="color: red">sadd</b>:向 key 对应的 set 集合中添加元素,返回 1 表示添加成功,返回 0 表示失败 <code>sadd key member [member ...]</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379[1]> sadd myset one two</span><br><span class="line">(integer) 2</span><br><span class="line">127.0.0.1:6379[1]> smembers myset</span><br><span class="line">1) "two"</span><br><span class="line">2) "one"</span><br></pre></td></tr></table></figure></li><li><p><strong>Smembers</strong>: 查看 set 集合中的元素 <code>smembers key</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379[1]> smembers myset</span><br><span class="line">1) "two"</span><br><span class="line">2) "one"</span><br></pre></td></tr></table></figure></li><li><p><strong>srem</strong>: 删除 key 对应 set 集合中的指定元素,返回 1 表示删除成功,返回 0 表示删除失败 <code>srem key member [member ...]</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379[1]> smembers myset</span><br><span class="line">1) "two"</span><br><span class="line">2) "one"</span><br><span class="line">3) "three"</span><br><span class="line">127.0.0.1:6379[1]> srem myset one</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379[1]> smembers myset</span><br><span class="line">1) "two"</span><br><span class="line">2) "three"</span><br><span class="line">127.0.0.1:6379[1]> srem myset five</span><br><span class="line">(integer) 0</span><br></pre></td></tr></table></figure></li><li><p><strong>spop</strong>: <strong>随机删除</strong> set 中的指定个元素并返回该元素 <code>spop key [count]</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379[1]> smembers myset</span><br><span class="line">1) "two"</span><br><span class="line">2) "one"</span><br><span class="line">3) "three"</span><br><span class="line">127.0.0.1:6379[1]> spop myset 2</span><br><span class="line">1) "two"</span><br><span class="line">2) "three"</span><br><span class="line">127.0.0.1:6379[1]> smembers myset</span><br><span class="line">1) "one"</span><br></pre></td></tr></table></figure></li><li><p><strong>sdiff</strong>: 返回给定 set 集合的<strong>差集</strong> (以在前的 set 集合为标准) <code>sdiff key [key ...]</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379[1]> smembers myset</span><br><span class="line">1) "two"</span><br><span class="line">2) "one"</span><br><span class="line">127.0.0.1:6379[1]> smembers myset1</span><br><span class="line">1) "two"</span><br><span class="line">2) "three"</span><br><span class="line">127.0.0.1:6379[1]> sdiff myset myset1</span><br><span class="line">1) "one"</span><br><span class="line">127.0.0.1:6379[1]> sdiff myset1 myset</span><br><span class="line">1) "three"</span><br></pre></td></tr></table></figure></li><li><p><strong>sdiffstore</strong>: 返回所有给定 set 集合的<strong>差集</strong>,并将差集添加到另外一个集合中 <code>sdiffstore destination key [key ...]</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379[1]> sdiffstore myset3 myset1 myset</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379[1]> smembers myset3</span><br><span class="line">1) "three"</span><br></pre></td></tr></table></figure></li><li><p><strong>sinter</strong>: 返回所有给定集合的<strong>交集</strong> <code>sinter key [key ...]</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379[1]> smembers myset</span><br><span class="line">1) "two"</span><br><span class="line">2) "one"</span><br><span class="line">127.0.0.1:6379[1]> smembers myset1</span><br><span class="line">1) "two"</span><br><span class="line">2) "three"</span><br><span class="line">127.0.0.1:6379[1]> sinter myset myset1</span><br><span class="line">1) "two"</span><br></pre></td></tr></table></figure></li><li><p><strong>sinterstore</strong>: 返回所有给定集合 key 的<strong>交集</strong>,并将结果存为另一个 key <code>sinterstore destination key [key ...]</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379[1]> sinterstore myset2 myset myset1</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379[1]> smembers myset2</span><br><span class="line">1) "two"</span><br></pre></td></tr></table></figure></li><li><p><strong>sunion</strong>: 返回所有给定集合的<strong>并集</strong> <code>sunion key [key ...]</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379[1]> smembers myset</span><br><span class="line">1) "two"</span><br><span class="line">2) "one"</span><br><span class="line">127.0.0.1:6379[1]> smembers myset1</span><br><span class="line">1) "two"</span><br><span class="line">2) "three"</span><br><span class="line">127.0.0.1:6379[1]> sunion myset myset1</span><br><span class="line">1) "one"</span><br><span class="line">2) "two"</span><br><span class="line">3) "three"</span><br></pre></td></tr></table></figure></li><li><p><strong>sunionstore</strong>: 返回所有给定集合的<strong>并集</strong>,并将结果存入另一个集合 <code>sunionstore destination key [key ...]</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379[1]> sunionstore myset4 myset myset1</span><br><span class="line">(integer) 3</span><br><span class="line">127.0.0.1:6379[1]> smembers myset4</span><br><span class="line">1) "one"</span><br><span class="line">2) "two"</span><br><span class="line">3) "three"</span><br></pre></td></tr></table></figure></li><li><p><strong>smove</strong>: 从第一个集合中移除元素并将该元素添加到另一个集合中 <code>smove source destination member</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379[1]> smembers myset</span><br><span class="line">1) "two"</span><br><span class="line">2) "one"</span><br><span class="line">127.0.0.1:6379[1]> smembers myset1</span><br><span class="line">1) "two"</span><br><span class="line">2) "three"</span><br><span class="line">127.0.0.1:6379[1]> smove myset myset1 one</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379[1]> smembers myset</span><br><span class="line">1) "two"</span><br><span class="line">127.0.0.1:6379[1]> smembers myset1</span><br><span class="line">1) "one"</span><br><span class="line">2) "two"</span><br><span class="line">3) "three"</span><br></pre></td></tr></table></figure></li><li><p><strong>Scard</strong>: 返回 set 集合中元素的个数 <b style="color: red">llen(list) strlen(str) hlen(hash)</b> <code>scard key</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379[1]> smembers myset1</span><br><span class="line">1) "one"</span><br><span class="line">2) "two"</span><br><span class="line">3) "three"</span><br><span class="line">127.0.0.1:6379[1]> scard myset1</span><br><span class="line">(integer) 3</span><br></pre></td></tr></table></figure></li><li><p><strong>sismember</strong>: 测试 member 元素是否是名称为 key 的 set 集合; 是的话返回 1 否则为 0 <code>sismember key member</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379[1]> smembers myset1</span><br><span class="line">1) "one"</span><br><span class="line">2) "two"</span><br><span class="line">3) "three"</span><br><span class="line">127.0.0.1:6379[1]> sismember myset1 one</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379[1]> sismember myset1 five</span><br><span class="line">(integer) 0</span><br></pre></td></tr></table></figure></li><li><p><strong>srandmember</strong>: 随机返回 set 集合中的几个元素 ,但不删除该元素 <code>srandmember key [count]</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379[1]> srandmember myset1 2</span><br><span class="line">1) "one"</span><br><span class="line">2) "three"</span><br><span class="line">127.0.0.1:6379[1]> srandmember myset1 2</span><br><span class="line">1) "two"</span><br><span class="line">2) "one"</span><br></pre></td></tr></table></figure></li></ol></li></ul><h5 id="5-Sorted-set-zset-类型"><a href="#5-Sorted-set-zset-类型" class="headerlink" title="5. Sorted set (zset) 类型"></a>5. Sorted set (zset) 类型</h5><ul><li><p>zset 类型介绍</p><ul><li>Sorted set 是 set 的升级版,在 set 的基础上增加了顺序属性,在添加元素时可以指定,每次指定后,zset会自动重新按新的值调整顺序。可以理解为有两列的 mysql 表,一列存 value,一列存顺序。操作 key 理解为 zset 的名字</li></ul></li><li><p>十一种方法 ( rank: 下标索引:从0开始的; score: 分数,序号)</p><ol><li><p><b style="color: red">zadd</b>: 向有序集合 set 中添加元素并指定顺序,如果该元素已存在就更新元素顺序 <code>zadd key [NX|XX] [CH] [INCR] score member [score member ...]</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379[1]> zadd myzset 1 one 2 two</span><br><span class="line">(integer) 2</span><br><span class="line">127.0.0.1:6379[1]> zrange myzset 0 -1</span><br><span class="line">1) "one"</span><br><span class="line">2) "two</span><br></pre></td></tr></table></figure></li><li><p><strong>zrange</strong>: 从 zset 集合中取元素 并 输出元素序号 (WITHSCORES 输出元素序号) <code>zrange key start stop [WITHSCORES]</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379[1]> zadd myzset 1 one 2 two</span><br><span class="line">(integer) 2</span><br><span class="line">127.0.0.1:6379[1]> zrange myzset 0 -1</span><br><span class="line">1) "one"</span><br><span class="line">2) "two"</span><br><span class="line">127.0.0.1:6379[1]> zrange myzset 0 -1 withscores</span><br><span class="line">1) "one"</span><br><span class="line">2) "1"</span><br><span class="line">3) "two"</span><br><span class="line">4) "2"</span><br></pre></td></tr></table></figure></li><li><p><strong>zrem</strong>: 删除 zset 集合中指定的元素 <code>zrem key member [member ...]</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379[1]> zrange myzset 0 -1 withscores</span><br><span class="line">1) "one"</span><br><span class="line">2) "1"</span><br><span class="line">3) "two"</span><br><span class="line">4) "2"</span><br><span class="line">127.0.0.1:6379[1]> zrem myzset two</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379[1]> zrange myzset 0 -1 withscores</span><br><span class="line">1) "one"</span><br><span class="line">2) "1"</span><br></pre></td></tr></table></figure></li><li><p><strong>zrank</strong>: 正序方式,获取指定元素的索引下标 <code>zrank key member</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379[1]> zrange myzset 0 -1</span><br><span class="line">1) "one"</span><br><span class="line">2) "two"</span><br><span class="line">127.0.0.1:6379[1]> zrank myzset two</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379[1]> zrank myzset one</span><br><span class="line">(integer) 0</span><br></pre></td></tr></table></figure></li><li><p><strong>zrevrank</strong>: 逆序方式获取指定元素的索引下标, rev–reverse <code>zrevrank key member</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379[1]> zrange myzset 0 -1</span><br><span class="line">1) "one"</span><br><span class="line">2) "two"</span><br><span class="line">127.0.0.1:6379[1]> zrevrank myzset one</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379[1]> zrevrank myzset two</span><br><span class="line">(integer) 0</span><br></pre></td></tr></table></figure></li><li><p><strong>zincrby</strong>: <strong>添加元素及其序号到 zset 中,或改变 zset 中元素的序号</strong> 若 zset 中已存在 元素 member,该元素的 score(序号) 增加 incrment(指定值) 否则向该集合中添加该元素,其score(序号) 的值为 increment <code>zincrby key increment member</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379[1]> zrange myzset 0 -1 withscores</span><br><span class="line">1) "one"</span><br><span class="line">2) "1"</span><br><span class="line">127.0.0.1:6379[1]> zincrby myzset 2 two</span><br><span class="line">"2"</span><br><span class="line">127.0.0.1:6379[1]> zrange myzset 0 -1 withscores</span><br><span class="line">1) "one"</span><br><span class="line">2) "1"</span><br><span class="line">3) "two"</span><br><span class="line">4) "2"</span><br><span class="line">127.0.0.1:6379[1]> zincrby myzset 2 one</span><br><span class="line">"3"</span><br><span class="line">127.0.0.1:6379[1]> zrange myzset 0 -1 withscores</span><br><span class="line">1) "two"</span><br><span class="line">2) "2"</span><br><span class="line">3) "one"</span><br><span class="line">4) "3"</span><br><span class="line">可以有相同序号</span><br><span class="line">127.0.0.1:6379[1]> zincrby myzset 1 two</span><br><span class="line">"3"</span><br><span class="line">127.0.0.1:6379[1]> zrange myzset 0 -1 withscores</span><br><span class="line">1) "one"</span><br><span class="line">2) "3"</span><br><span class="line">3) "two"</span><br><span class="line">4) "3"</span><br></pre></td></tr></table></figure></li><li><p><strong>zrangebyscore</strong>: 从 zset 集合<strong>指定范围</strong>中根据 score 顺序获取元素,可以限制获取次数 <code>zrangebyscore key min max [WITHSCORES] [LIMIT offset count]</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379[1]> zadd myzset1 0 name 1 age 2 gender 3 like</span><br><span class="line">(integer) 4</span><br><span class="line">127.0.0.1:6379[1]> zrange myzset1 0 -1 withscores</span><br><span class="line">1) "name"</span><br><span class="line">2) "0"</span><br><span class="line">3) "age"</span><br><span class="line">4) "1"</span><br><span class="line">5) "gender"</span><br><span class="line">6) "2"</span><br><span class="line">7) "like"</span><br><span class="line">8) "3"</span><br><span class="line">127.0.0.1:6379[1]> zrangebyscore myzset1 1 3 withscores</span><br><span class="line">1) "age"</span><br><span class="line">2) "1"</span><br><span class="line">3) "gender"</span><br><span class="line">4) "2"</span><br><span class="line">5) "like"</span><br><span class="line">6) "3"</span><br></pre></td></tr></table></figure></li><li><p><strong>zrevrange</strong>: 从 zset 集合中倒序 (score倒序) 获取元素 <code>zrevrange key start stop [WITHSCORES]</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379[1]> zrange myzset1 0 -1 withscores</span><br><span class="line">1) "name"</span><br><span class="line">2) "0"</span><br><span class="line">3) "age"</span><br><span class="line">4) "1"</span><br><span class="line">5) "gender"</span><br><span class="line">6) "2"</span><br><span class="line">7) "like"</span><br><span class="line">8) "3"</span><br><span class="line">127.0.0.1:6379[1]> zrevrange myzset1 0 -1 withscores</span><br><span class="line">1) "like"</span><br><span class="line">2) "3"</span><br><span class="line">3) "gender"</span><br><span class="line">4) "2"</span><br><span class="line">5) "age"</span><br><span class="line">6) "1"</span><br><span class="line">7) "name"</span><br><span class="line">8) "0"</span><br><span class="line">127.0.0.1:6379[1]> zrevrange myzset1 1 3 withscores</span><br><span class="line">1) "gender"</span><br><span class="line">2) "2"</span><br><span class="line">3) "age"</span><br><span class="line">4) "1"</span><br><span class="line">5) "name"</span><br><span class="line">6) "0"</span><br></pre></td></tr></table></figure></li><li><p><strong>zcount</strong>: 返回集合中 <strong>score</strong> 在给定区间的数量 <code>zcount key min max</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379[1]> zadd myzset1 3 follow</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379[1]> zrange myzset1 0 -1 withscores</span><br><span class="line"> 1) "name"</span><br><span class="line"> 2) "0"</span><br><span class="line"> 3) "age"</span><br><span class="line"> 4) "1"</span><br><span class="line"> 5) "gender"</span><br><span class="line"> 6) "2"</span><br><span class="line"> 7) "follow"</span><br><span class="line"> 8) "3"</span><br><span class="line"> 9) "like"</span><br><span class="line">10) "3"</span><br><span class="line">127.0.0.1:6379[1]> zcount myzset1 2 3</span><br><span class="line">(integer) 3</span><br></pre></td></tr></table></figure></li><li><p><strong>zcard</strong>: 返回 zset 集合中所有元素个数 <code>zcard key</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379[1]> zrange myzset1 0 -1 withscores</span><br><span class="line"> 1) "name"</span><br><span class="line"> 2) "0"</span><br><span class="line"> 3) "age"</span><br><span class="line"> 4) "1"</span><br><span class="line"> 5) "gender"</span><br><span class="line"> 6) "2"</span><br><span class="line"> 7) "follow"</span><br><span class="line"> 8) "3"</span><br><span class="line"> 9) "like"</span><br><span class="line">10) "3"</span><br><span class="line">127.0.0.1:6379[1]> zcard myzset1</span><br><span class="line">(integer) 5</span><br></pre></td></tr></table></figure></li><li><p><strong>zremrangebyrank</strong>: 删除集合中排名 (下标) 在给定区间的元素 <code>zremrangebyrank key start stop</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379[1]> zremrangebyrank myzset1 3 3</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379[1]> zrange myzset1 0 -1 withscores</span><br><span class="line">1) "name"</span><br><span class="line">2) "0"</span><br><span class="line">3) "age"</span><br><span class="line">4) "1"</span><br><span class="line">5) "gender"</span><br><span class="line">6) "2"</span><br><span class="line">7) "like"</span><br><span class="line">8) "3"</span><br></pre></td></tr></table></figure></li><li><p><strong>zremrangebyscore</strong>: 删除集合中顺序 (score 值排序)在给定区间的元素 <code>zremrangebyscore key min max</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379[1]> zrange myzset1 0 -1 withscores</span><br><span class="line">1) "name"</span><br><span class="line">2) "0"</span><br><span class="line">3) "age"</span><br><span class="line">4) "1"</span><br><span class="line">5) "gender"</span><br><span class="line">6) "2"</span><br><span class="line">7) "like"</span><br><span class="line">8) "3"</span><br><span class="line">127.0.0.1:6379[1]> zremrangebyscore myzset1 2 3</span><br><span class="line">(integer) 2</span><br><span class="line">127.0.0.1:6379[1]> zrange myzset1 0 -1</span><br><span class="line">1) "name"</span><br><span class="line">2) "age"</span><br></pre></td></tr></table></figure></li></ol></li></ul>]]></content>
<tags>
<tag> Redis </tag>
</tags>
</entry>
<entry>
<title>Windows中pyspider安装以及错误处理</title>
<link href="/2020/12/24/windows-zhong-pyspider-an-zhuang-yi-ji-cuo-wu-chu-li/"/>
<url>/2020/12/24/windows-zhong-pyspider-an-zhuang-yi-ji-cuo-wu-chu-li/</url>
<content type="html"><![CDATA[<h3 id="Windows中Pyspider安装以及错误处理"><a href="#Windows中Pyspider安装以及错误处理" class="headerlink" title="Windows中Pyspider安装以及错误处理"></a>Windows中Pyspider安装以及错误处理</h3><h4 id="安装-pyspider-过程中出现错误-ERROR-Command-errored-out-with-exit-status-10-python-setup-py-egg-info-Check-the-logs-for-full-command-output"><a href="#安装-pyspider-过程中出现错误-ERROR-Command-errored-out-with-exit-status-10-python-setup-py-egg-info-Check-the-logs-for-full-command-output" class="headerlink" title="安装 pyspider 过程中出现错误 ERROR: Command errored out with exit status 10: python setup.py egg_info Check the logs for full command output."></a>安装 pyspider 过程中出现错误 <code>ERROR: Command errored out with exit status 10: python setup.py egg_info Check the logs for full command output.</code></h4><ul><li><p><strong>报错信息</strong></p><p><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/pycurl_error.png" alt></p></li><li><p>观察发现 <code>pycurl</code> 模块下载出错 导致无法继续下载</p></li></ul><h4 id="单独安装-pycurl"><a href="#单独安装-pycurl" class="headerlink" title="单独安装 pycurl"></a><strong>单独安装 pycurl</strong></h4><ul><li><p>下载地址 <a href="https://www.lfd.uci.edu/~gohlke/pythonlibs/#pycurl" target="_blank" rel="noopener">https://www.lfd.uci.edu/~gohlke/pythonlibs/#pycurl</a></p></li><li><p>一定要单独下载后本地安装对应版本</p><p><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/install_pycurl.png" alt></p></li><li><p><strong>pip 下载本地文件</strong></p><p><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/pip_pycurl.png" alt></p></li><li><p>搜索 <code>pycurl</code> 下载 合适的版本 一定要与 python 版本符合</p><ul><li><p><strong>版本不符报错</strong></p><p><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/pycurl_verror.png" alt></p></li><li><p><strong>查看 python 版本</strong></p><p><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/python_version.png" alt></p></li><li><p><code>pycurl</code> : 名称 <code>pycurl‑7.43.0.4‑cp37‑cp37m‑win_amd64.whl</code>, <strong>cp</strong> 为 对应python版本</p></li></ul></li></ul><h4 id="async-错误"><a href="#async-错误" class="headerlink" title="async 错误"></a><strong>async 错误</strong></h4><ul><li><p><strong>错误图片</strong></p><p><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/async_error.png" alt></p></li><li><p><strong>错误原因</strong></p><ul><li>python3 中,已经将async 设置为关键字,而pyspider 版本更新缓慢并没有在此处修改,所以导致pyspider 不能正常启动。</li></ul></li><li><p>修改 pyspider 源码将 async 改为其它非关键字 (<strong>只修改 async 关键字的,其他含有这些字符的不需要修改</strong>)</p><ul><li><p><strong>需要修改的源码所在位置</strong></p><p><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/replace_loc.png" alt></p></li><li><p><strong>修改源码</strong></p><p><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/replace_arync.png" alt></p></li></ul></li></ul><h4 id="Wsgidav-错误"><a href="#Wsgidav-错误" class="headerlink" title="Wsgidav 错误"></a><strong>Wsgidav 错误</strong></h4><ul><li><p><strong>错误图片</strong></p><p><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/wsgidav_error.png" alt></p></li><li><p><strong>错误原因</strong></p><ul><li>WsgiDav 新版本 已将更改源码 <code>'domaincontroller': NeedAuthController(app),</code> 为 <code>'http_authenticator': {'HTTPAuthenticator': NeedAuthController(app),},</code></li></ul></li><li><p><strong>修改源码</strong></p><ul><li><p><strong>修改</strong></p><p><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/webdav_replace.png" alt></p></li></ul></li></ul><h4 id="werkzeug-wsgi-错误"><a href="#werkzeug-wsgi-错误" class="headerlink" title="werkzeug.wsgi 错误"></a><strong>werkzeug.wsgi 错误</strong></h4><ul><li><p><strong>错误图片</strong></p><p><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/werkzeug_error.png" alt></p></li><li><p><strong>错误原因</strong></p><ul><li><code>werkzeug</code> 1.0.0 版本及以上,没有<em>DispatcherMiddleware</em>方法</li><li><code>Pyspider</code> 需要 <code>werkzeug</code>库版本在 <strong>0.15–1.0(不允许为1.0)</strong></li></ul></li><li><p><strong>重新下载 <code>werkzeug</code> 版本</strong></p><ul><li><p><code>pip install --upgrade werkzeug==0.16.1</code></p><p><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/install_werkzeug.png" alt></p></li></ul></li></ul><h4 id="io-Loop-错误"><a href="#io-Loop-错误" class="headerlink" title="io_Loop 错误"></a><strong>io_Loop 错误</strong></h4><ul><li><strong>报错</strong> : <code>TypeError: initialize() got an unexpected keyword argument ‘io_loop’</code></li><li><strong>错误原因</strong> : python环境中,默认tornado版本是最新的5.0,在4.0之后就废弃了io_loop参数</li><li><strong>重新更新 <code>tornado</code> 版本</strong> : <code>pip install --upgrade tornado==4.5.1</code></li></ul><h4 id="成功启动"><a href="#成功启动" class="headerlink" title="*成功启动 *"></a>*<em>成功启动 *</em></h4><ul><li><p><code>pyspider all</code> 启动成功</p><p><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/pyspider_success.png" alt></p></li></ul>]]></content>
<tags>
<tag> pyspider </tag>
</tags>
</entry>
<entry>
<title>Python 的垃圾回收机制</title>
<link href="/2020/12/18/python-de-la-ji-hui-shou-ji-zhi/"/>
<url>/2020/12/18/python-de-la-ji-hui-shou-ji-zhi/</url>
<content type="html"><![CDATA[<h3 id="Python-的内存回收机制"><a href="#Python-的内存回收机制" class="headerlink" title="Python 的内存回收机制"></a>Python 的内存回收机制</h3><ul><li><p>垃圾回收机制,就是我们常说的<strong>GC(Garbage collection)</strong>。高级编程语言,如java,c#等,都采用了垃圾回收机制,减轻程序员对于内存的处理压力。但这并不代表内存泄漏等问题可以被完全杜绝</p></li><li><p>就python而言,主要采用的是常见的引用<strong><em>计数机制</em></strong>,同时结合<strong><em>标记-清除</em></strong>和<strong><em>分代收集</em></strong>两种机制。</p></li></ul><h4 id="引用计数"><a href="#引用计数" class="headerlink" title="引用计数"></a><strong>引用计数</strong></h4><ul><li>python中一切皆对象,<em>每一个Python对象都有一个引用计数器</em>,用于记录有多少其他对象指向(引用)这个对象。那什么时候引用计数增加,什么时候引用计数减少呢?</li></ul><h5 id="引用计数增加"><a href="#引用计数增加" class="headerlink" title="引用计数增加"></a><strong>引用计数增加</strong></h5><ol><li>对象被创建:<code>x = “test”</code></li><li>另外的别名被创建:<code>y = x</code></li><li>对象被作为参数传递给函数(新的本地引用): <code>foobar(x)</code></li><li>对象成为容器对象的一个元素:<code>myList = [x, ‘xyz’]</code></li></ol><h5 id="引用计数减少"><a href="#引用计数减少" class="headerlink" title="引用计数减少"></a><strong>引用计数减少</strong></h5><ol><li>一个本地引用离开了其作用范围。如fooc()函数结束时,func函数中的局部变量(全局变量不会)</li><li>对象的别名被显式销毁:<code>del y</code></li><li>对象的一个别名被赋值给其他对象:<code>x = 123</code></li><li>对象被从一个窗口对象中移除:<code>myList.remove(x)</code></li><li>窗口对象本身被销毁:<code>del myList</code></li></ol><h5 id="当引用计数变为0时,会回收对象释放内存空间。"><a href="#当引用计数变为0时,会回收对象释放内存空间。" class="headerlink" title="当引用计数变为0时,会回收对象释放内存空间。"></a><strong>当引用计数变为0时,会回收对象释放内存空间。</strong></h5><h5 id="引用计数法有很明显的优点:"><a href="#引用计数法有很明显的优点:" class="headerlink" title="引用计数法有很明显的优点:"></a><strong>引用计数法有很明显的优点:</strong></h5><ul><li><strong>实现简单</strong></li><li><strong>实时性</strong>:一旦没有引用,内存就直接释放了。不用像其他机制等到特定时机。实时性还带来一个好处:处理回收内存的时间分摊到了平时。</li></ul><h5 id="非常显著的的缺点:"><a href="#非常显著的的缺点:" class="headerlink" title="非常显著的的缺点:"></a><strong>非常显著的的缺点:</strong></h5><ul><li><strong>维护引用计数消耗资源</strong>,维护引用计数的次数和引用赋值成正比,而不像mark and sweep等基本与回收的内存数量有关。</li><li><strong>无法解决循环引用的问题</strong>。A和B相互引用而再没有外部引用A与B中的任何一个,它们的引用计数都为1,但显然应该被回收。</li><li>为解决上面2个问题,python又引入了<strong>两种GC机制</strong></li></ul><h4 id="标记-清除"><a href="#标记-清除" class="headerlink" title="标记-清除"></a><strong>标记-清除</strong></h4><ul><li>此GC机制,主要是为了<strong>解决循环引用问题</strong>。</li><li>标记清除(Mark—Sweep)算法是一种基于<strong>追踪回收</strong>(tracing GC)技术实现的垃圾回收算法。它分为两个阶段:<ol><li>第一阶段是<strong>标记阶段</strong>,GC会把所有的『活动对象』打上标记,</li><li>第二阶段是<strong>把那些没有标记的对象『非活动对象』进行回收</strong>。那么GC又是如何判断哪些是活动对象哪些是非活动对象的呢?</li></ol></li><li>对象之间通过引用(指针)连在一起,构成一个<strong>有向图</strong>,对象构成这个有向图的节点,而引用关系构成这个有向图的边。从根对象(root object)出发,沿着有向边遍历对象,可达的(reachable)对象标记为活动对象,不可达的对象就是要被清除的非活动对象。根对象就是全局变量、调用栈、寄存器</li><li>标记清除算法作为python的辅助垃圾收集技术<strong>主要处理</strong>的是一些<strong>容器对象</strong>,比如list、dict、tuple,instance等,因为对于字符串、数值对象是不可能造成循环引用问题。</li></ul><h4 id="分代回收"><a href="#分代回收" class="headerlink" title="分代回收"></a><strong>分代回收</strong></h4><ul><li>分代回收是一种<strong>以空间换时间</strong>的操作方式,</li><li>Python将内存根据对象的存活时间划分为不同的集合,每个集合称为一个代,Python将内存分为了3“代”,分别为<em>年轻代</em>(第0代)、<em>中年代</em>(第1代)、<em>老年代</em>(第2代),他们对应的是3个链表,它们的垃圾收集频率与对象的存活时间的增大而减小。<ul><li>新创建的对象都会分配在年轻代,年轻代链表的总数达到上限时,Python垃圾收集机制就会被触发,把那些可以被回收的对象回收掉,而那些不会回收的对象就会被移到中年代去,依此类推,</li><li>老年代中的对象是存活时间最久的对象,甚至是存活于整个系统的生命周期内。同时,分代回收是建立在标记清除技术基础之上。分代回收同样作为Python的辅助垃圾收集技术处理那些容器对象。</li></ul></li></ul>]]></content>
<tags>
<tag> Python 面试 </tag>
</tags>
</entry>
<entry>
<title>HeroKu 搭建学习使用的 V2ray</title>
<link href="/2020/10/17/heroku-da-jian-xue-xi-shi-yong-de-v2ray/"/>
<url>/2020/10/17/heroku-da-jian-xue-xi-shi-yong-de-v2ray/</url>
<content type="html"><![CDATA[<h3 id="HeroKu-搭建学习使用-V2ray-Websocket"><a href="#HeroKu-搭建学习使用-V2ray-Websocket" class="headerlink" title="HeroKu 搭建学习使用 V2ray Websocket"></a>HeroKu 搭建学习使用 V2ray Websocket</h3><h4 id="HeroKu-介绍-以及-涉及链接"><a href="#HeroKu-介绍-以及-涉及链接" class="headerlink" title="HeroKu 介绍 以及 涉及链接"></a>HeroKu 介绍 以及 涉及链接</h4><ul><li><p>Heroku是一个支持多种编程语言的云平台即服务。在2010年被Salesforce.com收购。</p></li><li><p>HeroKu 另外提供了免费版本用于小开发者操作.今天我们便是使用免费版本进行 V2ray 搭建.</p></li><li><p>HeroKu 网速较慢,但是足够用于学习搜索,不适合做为翻墙看视频之类的,希望合理使用,方便学习</p></li><li><p><a href="https://dashboard.heroku.com/login" target="_blank" rel="noopener">HeroKu</a> 地址 <code>https://dashboard.heroku.com/login</code></p></li><li><p><a href="https://github.com/bclswl0827/v2ray-heroku" target="_blank" rel="noopener">V2Ray Heroku GitHub 链接</a> </p></li><li><p><a href="https://liuliwanjia.lanzous.com/b06lz9gsb" target="_blank" rel="noopener">V2RayN 客户端下载链接</a></p></li></ul><h4 id="HeroKu-注册及登录"><a href="#HeroKu-注册及登录" class="headerlink" title="HeroKu 注册及登录"></a>HeroKu 注册及登录</h4><h5 id="注册"><a href="#注册" class="headerlink" title="注册"></a>注册</h5><ul><li><p>HeroKu 是国外网站国内 不太稳定,最好提前准备好 翻墙、google邮箱、雅虎邮箱…</p></li><li><p>注册流程</p><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/heroKu_register.png" style="zoom: 67%;"></li></ul><h5 id="登录"><a href="#登录" class="headerlink" title="登录"></a>登录</h5><ul><li><p>输入账号名密码</p><p><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/HeroKu_login.png" alt></p></li></ul><h5 id="一步搭建"><a href="#一步搭建" class="headerlink" title="一步搭建"></a>一步搭建</h5><ul><li><p>登陆测试成功后,从 <a href="https://github.com/bclswl0827/v2ray-heroku" target="_blank" rel="noopener">V2Ray Heroku GitHub 链接</a> 内跳转到创建 APP 页面</p><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/HeroKu_Github.png" style="zoom:80%;"></li><li><p>在 创建 App 页面进行配置 App 信息</p><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/HeroKu_creatapp.png" style="zoom:67%;"></li><li><p>创建完成后 点击 <strong>Deploy App</strong></p><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/HeroKu_01.png" style="zoom: 50%;"></li><li><p>等待下方创建完成后点击 <strong>Manage App</strong></p></li><li><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/HeroKu_02.png" style="zoom:50%;"><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/HeroKu_03.png" style="zoom:50%;"></li><li><p>在管理页面找到 <strong>settings</strong> 设置页面,向下找到 <strong>Domains</strong> 记下自己的 App 链接</p><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/HeroKu-04.png" style="zoom:50%;"><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/HeroKu_05.png" style="zoom:50%;"></li></ul><h5 id="V2rayN-设置"><a href="#V2rayN-设置" class="headerlink" title="V2rayN 设置"></a>V2rayN 设置</h5><ul><li><p>下载 <a href="https://liuliwanjia.lanzous.com/b06lz9gsb" target="_blank" rel="noopener">V2RayN 客户端</a>,安装完成后进行设置</p></li><li><p>设置流程, 打开 V2rayN</p><ol><li>添加服务器</li></ol><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/HeroKu_V2_01.png" style="zoom: 67%;"><ol start="2"><li>配置服务器, 地址为在 settings 下 记录的 <strong>Domains</strong> URL, 端口为 <strong>443</strong>, 用户 ID 为最开始记录的 <strong>UUID</strong>, 传输协议为 <strong>vs</strong>, 底层传输安全为 <strong>tls</strong></li></ol><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/HeroKu_V2_02.png" style="zoom:50%;"><ol start="3"><li>设为活动服务器并通过 ping 测速</li></ol><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/HeroKu_V2_03.png" style="zoom:50%;"><h4 id="改变链接为直连模式"><a href="#改变链接为直连模式" class="headerlink" title="改变链接为直连模式"></a>改变链接为直连模式</h4><ul><li><p>使用时通常我们并不需要全局代理,仅仅是在使用网页时对代理有需求,那么我们可以通过浏览器插件与 V2ray 设置实现</p><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/HeroKu_V2_04.png" style="zoom:67%;"></li><li><p>在浏览器的 <strong>应用商店</strong> 内可以下载 <strong>SwitchyOmega</strong> ,对其进行配置 (火狐可以直接下载配置 , 谷歌浏览器需要打开全局代理下载后配置)</p><p><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/proxy.png" alt></p></li></ul></li></ul>]]></content>
<tags>
<tag> HeroKu Server </tag>
</tags>
</entry>
<entry>
<title>Git_pull_error</title>
<link href="/2020/10/10/git-pull-error/"/>
<url>/2020/10/10/git-pull-error/</url>
<content type="html"><![CDATA[<h4 id="Git-pull-文件时出现错误"><a href="#Git-pull-文件时出现错误" class="headerlink" title="Git pull 文件时出现错误"></a>Git pull 文件时出现错误</h4><h5 id="错误报告-“error-RPC-failed-curl-18-transfer-closed-with-outstanding-read-data-remaining”"><a href="#错误报告-“error-RPC-failed-curl-18-transfer-closed-with-outstanding-read-data-remaining”" class="headerlink" title="错误报告 : “error: RPC failed; curl 18 transfer closed with outstanding read data remaining”"></a>错误报告 : “error: RPC failed; curl 18 transfer closed with outstanding read data remaining”</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">error: RPC failed; curl 18 transfer closed with outstanding read data remaining</span><br><span class="line">fatal: The remote end hung up unexpectedly</span><br><span class="line">fatal: early EOF</span><br><span class="line">fatal: index-pack failed</span><br></pre></td></tr></table></figure><h5 id="错误原因"><a href="#错误原因" class="headerlink" title="错误原因"></a>错误原因</h5><h5 id="1-网络下载速度太慢"><a href="#1-网络下载速度太慢" class="headerlink" title="1.网络下载速度太慢"></a>1.<strong>网络下载速度太慢</strong></h5><ul><li><p>错误应对 : 修改下载速度</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">git config --global http.lowSpeedLimit 0</span><br><span class="line">git config --global http.lowSpeedTime 999999</span><br></pre></td></tr></table></figure></li></ul><h5 id="2-缓存太小下载文件过大导致缓存溢出"><a href="#2-缓存太小下载文件过大导致缓存溢出" class="headerlink" title="2. 缓存太小下载文件过大导致缓存溢出"></a>2. <strong>缓存太小下载文件过大导致缓存溢出</strong></h5><ul><li><p>查看缓冲区大小,单位为 b</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git config --list</span><br></pre></td></tr></table></figure></li></ul><ul><li><p>错误应对 : 全局修改 postBuffer 的默认大小,增加缓冲</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git config --global http.postBuffer 524288000</span><br></pre></td></tr></table></figure></li></ul>]]></content>
<tags>
<tag> Git </tag>
</tags>
</entry>
<entry>
<title>Git-commands-02</title>
<link href="/2020/09/05/git-commands-02/"/>
<url>/2020/09/05/git-commands-02/</url>
<content type="html"><![CDATA[<h2 id="Git-常用命令及使用情况-2"><a href="#Git-常用命令及使用情况-2" class="headerlink" title="Git 常用命令及使用情况 2"></a>Git 常用命令及使用情况 2</h2><h3 id="Git-常用命令"><a href="#Git-常用命令" class="headerlink" title="Git 常用命令"></a>Git 常用命令</h3><ul><li><h4 id="tag-常用于发布版本"><a href="#tag-常用于发布版本" class="headerlink" title="tag 常用于发布版本"></a>tag 常用于发布版本</h4><ul><li><h5 id="默认在-HEAD-上创建一个标签"><a href="#默认在-HEAD-上创建一个标签" class="headerlink" title="默认在 HEAD 上创建一个标签"></a>默认在 HEAD 上创建一个标签</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git tag tag_name</span><br></pre></td></tr></table></figure></li><li><h5 id="制定一个-commit-id-创建一个标签"><a href="#制定一个-commit-id-创建一个标签" class="headerlink" title="制定一个 commit id 创建一个标签"></a>制定一个 commit id 创建一个标签</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git tag tag_name commit_id</span><br></pre></td></tr></table></figure></li><li><h5 id="创建带有说明的标签,用-a-指定标签名,-m-指定说明文字"><a href="#创建带有说明的标签,用-a-指定标签名,-m-指定说明文字" class="headerlink" title="创建带有说明的标签,用 -a 指定标签名,-m 指定说明文字"></a>创建带有说明的标签,用 -a 指定标签名,-m 指定说明文字</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git tag -a tag_name -m "说明文字"</span><br></pre></td></tr></table></figure></li><li><h5 id="查看所有标签-标签都是按照字母排序的"><a href="#查看所有标签-标签都是按照字母排序的" class="headerlink" title="查看所有标签 (标签都是按照字母排序的)"></a>查看所有标签 (标签都是按照字母排序的)</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git tag</span><br></pre></td></tr></table></figure></li><li><h5 id="查看单个标签具体信息"><a href="#查看单个标签具体信息" class="headerlink" title="查看单个标签具体信息"></a>查看单个标签具体信息</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git show <tag_name></span><br></pre></td></tr></table></figure></li><li><h5 id="推送一个本地标签"><a href="#推送一个本地标签" class="headerlink" title="推送一个本地标签"></a>推送一个本地标签</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git push origin <tag_name></span><br></pre></td></tr></table></figure></li><li><h5 id="推送全部未推送过的本地标签"><a href="#推送全部未推送过的本地标签" class="headerlink" title="推送全部未推送过的本地标签"></a>推送全部未推送过的本地标签</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git push origin --tags</span><br></pre></td></tr></table></figure></li><li><h5 id="删除本地标签-由于创建的标签只能存储在本地,不会自动推送至远程,所以错误的标签可以在本地安全的删除"><a href="#删除本地标签-由于创建的标签只能存储在本地,不会自动推送至远程,所以错误的标签可以在本地安全的删除" class="headerlink" title="删除本地标签 (由于创建的标签只能存储在本地,不会自动推送至远程,所以错误的标签可以在本地安全的删除)"></a>删除本地标签 (由于创建的标签只能存储在本地,不会自动推送至远程,所以错误的标签可以在本地安全的删除)</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git tag -d tag_name</span><br></pre></td></tr></table></figure></li><li><h5 id="删除一个远程标签-先删除本地标签,再删除远程标签"><a href="#删除一个远程标签-先删除本地标签,再删除远程标签" class="headerlink" title="删除一个远程标签 (先删除本地标签,再删除远程标签)"></a>删除一个远程标签 (先删除本地标签,再删除远程标签)</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git push origin :refs/tags/<tag_name></span><br></pre></td></tr></table></figure></li></ul></li><li><h4 id="删除文件-rm"><a href="#删除文件-rm" class="headerlink" title="删除文件 rm"></a>删除文件 rm</h4><ul><li><h5 id="删除暂存区和工作区的文件"><a href="#删除暂存区和工作区的文件" class="headerlink" title="删除暂存区和工作区的文件"></a>删除暂存区和工作区的文件</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git rm filename</span><br></pre></td></tr></table></figure></li><li><h5 id="只删除暂存区文件,不删除工作区文件-又称为-取消追踪文件,git-不再提交此文件,远程仓库也删除此文件"><a href="#只删除暂存区文件,不删除工作区文件-又称为-取消追踪文件,git-不再提交此文件,远程仓库也删除此文件" class="headerlink" title="只删除暂存区文件,不删除工作区文件 (又称为 取消追踪文件,git 不再提交此文件,远程仓库也删除此文件)"></a>只删除暂存区文件,不删除工作区文件 (又称为 取消追踪文件,git 不再提交此文件,远程仓库也删除此文件)</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git rm --cached filename</span><br></pre></td></tr></table></figure></li></ul></li></ul><h3 id="版本切换-amp-重设-amp-撤销-详解"><a href="#版本切换-amp-重设-amp-撤销-详解" class="headerlink" title="版本切换 & 重设 & 撤销 详解"></a>版本切换 & 重设 & 撤销 详解</h3><ul><li><h4 id="checkout-可以撤销工作区的文件-可以作用于-commit-或者文件"><a href="#checkout-可以撤销工作区的文件-可以作用于-commit-或者文件" class="headerlink" title="checkout : 可以撤销工作区的文件, 可以作用于 commit 或者文件"></a>checkout : 可以撤销工作区的文件, 可以作用于 commit 或者文件</h4><ul><li><h5 id="恢复暂存区文件到工作区"><a href="#恢复暂存区文件到工作区" class="headerlink" title="恢复暂存区文件到工作区"></a>恢复暂存区文件到工作区</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git checkout <filename></span><br></pre></td></tr></table></figure></li><li><h5 id="恢复暂存区的所有文件到工作区"><a href="#恢复暂存区的所有文件到工作区" class="headerlink" title="恢复暂存区的所有文件到工作区"></a>恢复暂存区的所有文件到工作区</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git checkout .</span><br></pre></td></tr></table></figure></li><li><h5 id="回滚到最近的的一次提交,如果修改某些文件后,没有提交到暂存区,此时回滚是回到上一次提交,如果已经提交到仓库,这个回滚无效,只能回滚到提交的版本"><a href="#回滚到最近的的一次提交,如果修改某些文件后,没有提交到暂存区,此时回滚是回到上一次提交,如果已经提交到仓库,这个回滚无效,只能回滚到提交的版本" class="headerlink" title="回滚到最近的的一次提交,如果修改某些文件后,没有提交到暂存区,此时回滚是回到上一次提交,如果已经提交到仓库,这个回滚无效,只能回滚到提交的版本"></a>回滚到最近的的一次提交,如果修改某些文件后,没有提交到暂存区,此时回滚是回到上一次提交,如果已经提交到仓库,这个回滚无效,只能回滚到提交的版本</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">$ git checkout HEAD</span><br><span class="line">$ git checkout HEAD --filename</span><br></pre></td></tr></table></figure></li><li><h5 id="回滚到最近一次提交的上一个版本"><a href="#回滚到最近一次提交的上一个版本" class="headerlink" title="回滚到最近一次提交的上一个版本"></a>回滚到最近一次提交的上一个版本</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git checkout HEAD^</span><br></pre></td></tr></table></figure></li><li><h5 id="回滚到最近一次提交的上两个版本"><a href="#回滚到最近一次提交的上两个版本" class="headerlink" title="回滚到最近一次提交的上两个版本"></a>回滚到最近一次提交的上两个版本</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git checkout HEAD^^</span><br></pre></td></tr></table></figure></li><li><h5 id="切换分支"><a href="#切换分支" class="headerlink" title="切换分支"></a>切换分支</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git checkout branchname</span><br></pre></td></tr></table></figure></li><li><p>切换到某个指定的 commit 版本</p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git checkout <commit_id></span><br></pre></td></tr></table></figure></li><li><p>切换到指定的 tag</p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git checkout <tag></span><br></pre></td></tr></table></figure></li></ul></li><li><h4 id="reset-可以撤销工作区-暂存区的文件-可以作用于-commit-或者文件"><a href="#reset-可以撤销工作区-暂存区的文件-可以作用于-commit-或者文件" class="headerlink" title="reset : 可以撤销工作区/暂存区的文件, 可以作用于 commit 或者文件"></a>reset : 可以撤销工作区/暂存区的文件, 可以作用于 commit 或者文件</h4><ul><li><h5 id="当你用-reset-回滚到了某个版本后,那么在下一次-git-提交时,之前该版本后面的版本会被作为垃圾删掉"><a href="#当你用-reset-回滚到了某个版本后,那么在下一次-git-提交时,之前该版本后面的版本会被作为垃圾删掉" class="headerlink" title="当你用 reset 回滚到了某个版本后,那么在下一次 git 提交时,之前该版本后面的版本会被作为垃圾删掉."></a>当你用 reset 回滚到了某个版本后,那么在下一次 git 提交时,之前该版本后面的版本会被作为垃圾删掉.</h5></li><li><h5 id="当我们回退到一个旧版本后,此时再用-git-log-查看提交记录,会发现之前的新版本记录没有了。如果第二天,你又想恢复到新版本怎么办?找不到新版本的-commit-id-怎么办?"><a href="#当我们回退到一个旧版本后,此时再用-git-log-查看提交记录,会发现之前的新版本记录没有了。如果第二天,你又想恢复到新版本怎么办?找不到新版本的-commit-id-怎么办?" class="headerlink" title="当我们回退到一个旧版本后,此时再用 git log 查看提交记录,会发现之前的新版本记录没有了。如果第二天,你又想恢复到新版本怎么办?找不到新版本的 commit_id 怎么办?"></a>当我们回退到一个旧版本后,此时再用 git log 查看提交记录,会发现之前的新版本记录没有了。如果第二天,你又想恢复到新版本怎么办?找不到新版本的 commit_id 怎么办?</h5><ul><li>用 git reflog 查看历史命令,这样就可以看到之前新版本的 commit_id ,然后 git reset –hard commit_id 就可以回到之前的新版本代码</li></ul></li><li><h5 id="git-reset-hard-soft-mixed-merge-keep-lt-commit-gt-或HEAD"><a href="#git-reset-hard-soft-mixed-merge-keep-lt-commit-gt-或HEAD" class="headerlink" title="git reset [--hard|soft|mixed|merge|keep] [<commit>或HEAD]"></a><code>git reset [--hard|soft|mixed|merge|keep] [<commit>或HEAD]</code></h5><h5 id="将当前的分支重设-reset-到指定的-或者-HEAD-默认,如果不显示指定-,默认是-HEAD-,即最新的一次提交-,并且根据-mode-有可能更新索引和工作目录。mode-的取值可以是-hard、soft、mixed、merged、keep"><a href="#将当前的分支重设-reset-到指定的-或者-HEAD-默认,如果不显示指定-,默认是-HEAD-,即最新的一次提交-,并且根据-mode-有可能更新索引和工作目录。mode-的取值可以是-hard、soft、mixed、merged、keep" class="headerlink" title="将当前的分支重设(reset)到指定的 或者 HEAD (默认,如果不显示指定 ,默认是 HEAD ,即最新的一次提交),并且根据 [mode] 有可能更新索引和工作目录。mode 的取值可以是 hard、soft、mixed、merged、keep ."></a>将当前的分支重设(reset)到指定的 <commit> 或者 HEAD (默认,如果不显示指定 <commit>,默认是 HEAD ,即最新的一次提交),并且根据 [mode] 有可能更新索引和工作目录。mode 的取值可以是 hard、soft、mixed、merged、keep .</commit></commit></h5></li><li><h5 id="从暂存区撤销特定文件-但不改变工作区-会取消这个文件的暂存-而不覆盖任何更改"><a href="#从暂存区撤销特定文件-但不改变工作区-会取消这个文件的暂存-而不覆盖任何更改" class="headerlink" title="从暂存区撤销特定文件,但不改变工作区,会取消这个文件的暂存,而不覆盖任何更改"></a>从暂存区撤销特定文件,但不改变工作区,会取消这个文件的暂存,而不覆盖任何更改</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git reset <filename></span><br></pre></td></tr></table></figure></li><li><h5 id="重置暂存区的最近一次提交,但工作区文件不变"><a href="#重置暂存区的最近一次提交,但工作区文件不变" class="headerlink" title="重置暂存区的最近一次提交,但工作区文件不变"></a>重置暂存区的最近一次提交,但工作区文件不变</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$ git reset </span><br><span class="line"><==></span><br><span class="line">$ git reset HEAD (默认)</span><br></pre></td></tr></table></figure></li><li><h5 id="重置工作区与暂存区,回退到最近一次提交的版本内容"><a href="#重置工作区与暂存区,回退到最近一次提交的版本内容" class="headerlink" title="重置工作区与暂存区,回退到最近一次提交的版本内容"></a>重置工作区与暂存区,回退到最近一次提交的版本内容</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git reset --hard</span><br></pre></td></tr></table></figure></li><li><h5 id="重置暂存区与工作区-回退到最近一次提交的上一个版本"><a href="#重置暂存区与工作区-回退到最近一次提交的上一个版本" class="headerlink" title="重置暂存区与工作区,回退到最近一次提交的上一个版本"></a>重置暂存区与工作区,回退到最近一次提交的上一个版本</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git reset --hard HEAD^</span><br></pre></td></tr></table></figure></li><li><h5 id="mixed-将当前分支的指针指向为指定的-commit-该提交之后的提交都会被移除-同时重置暂存区-但工作区不变"><a href="#mixed-将当前分支的指针指向为指定的-commit-该提交之后的提交都会被移除-同时重置暂存区-但工作区不变" class="headerlink" title="mixed : 将当前分支的指针指向为指定的 commit (该提交之后的提交都会被移除), 同时重置暂存区,但工作区不变"></a>mixed : 将当前分支的指针指向为指定的 commit (该提交之后的提交都会被移除), 同时重置暂存区,但工作区不变</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$ git reset <commit></span><br><span class="line"><==></span><br><span class="line">$ git reset --mixed <commit></span><br></pre></td></tr></table></figure></li><li><h5 id="soft-将当前分支的指针指向为指定的-commit-该提交之后的提交都会被移除-但保持暂存区和工作区不变"><a href="#soft-将当前分支的指针指向为指定的-commit-该提交之后的提交都会被移除-但保持暂存区和工作区不变" class="headerlink" title="soft : 将当前分支的指针指向为指定的 commit (该提交之后的提交都会被移除), 但保持暂存区和工作区不变"></a>soft : 将当前分支的指针指向为指定的 commit (该提交之后的提交都会被移除), 但保持暂存区和工作区不变</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git reset --soft <commit></span><br></pre></td></tr></table></figure></li><li><h5 id="hard-将当前分支的指针指向为指定的-commit-该提交之后的提交都会被移除-同时重置暂存区、工作区"><a href="#hard-将当前分支的指针指向为指定的-commit-该提交之后的提交都会被移除-同时重置暂存区、工作区" class="headerlink" title="hard : 将当前分支的指针指向为指定的 commit (该提交之后的提交都会被移除), 同时重置暂存区、工作区"></a>hard : 将当前分支的指针指向为指定的 commit (该提交之后的提交都会被移除), 同时重置暂存区、工作区</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git reset --hard <commit></span><br></pre></td></tr></table></figure></li></ul></li><li><h4 id="revert-只能作用于-commit-撤销某个已经提交的快照(和-reset-重置到某个指定版本不一样)」。它是在提交记录最后面加上一个撤销了更改的新提交,而不是从项目历史中移除这个提交,这避免了-Git-丢失项目历史"><a href="#revert-只能作用于-commit-撤销某个已经提交的快照(和-reset-重置到某个指定版本不一样)」。它是在提交记录最后面加上一个撤销了更改的新提交,而不是从项目历史中移除这个提交,这避免了-Git-丢失项目历史" class="headerlink" title="revert : 只能作用于 commit,撤销某个已经提交的快照(和 reset 重置到某个指定版本不一样)」。它是在提交记录最后面加上一个撤销了更改的新提交,而不是从项目历史中移除这个提交,这避免了 Git 丢失项目历史"></a>revert : 只能作用于 commit,撤销某个已经提交的快照(和 reset 重置到某个指定版本不一样)」。它是在提交记录最后面加上一个撤销了更改的新提交,而不是从项目历史中移除这个提交,这避免了 Git 丢失项目历史</h4><ul><li><h5 id="生成一个撤销最近的一次提交的新提交"><a href="#生成一个撤销最近的一次提交的新提交" class="headerlink" title="生成一个撤销最近的一次提交的新提交"></a>生成一个撤销最近的一次提交的新提交</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git revert HEAD</span><br></pre></td></tr></table></figure></li><li><h5 id="生成一个撤销最近一次提交的上一次提交的新提交"><a href="#生成一个撤销最近一次提交的上一次提交的新提交" class="headerlink" title="生成一个撤销最近一次提交的上一次提交的新提交"></a>生成一个撤销最近一次提交的上一次提交的新提交</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git revert HEAD^</span><br></pre></td></tr></table></figure></li><li><h5 id="生成一个撤销最近一次提交的上两次提交的新提交"><a href="#生成一个撤销最近一次提交的上两次提交的新提交" class="headerlink" title="生成一个撤销最近一次提交的上两次提交的新提交"></a>生成一个撤销最近一次提交的上两次提交的新提交</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git revert HEAD^^</span><br></pre></td></tr></table></figure></li><li><h5 id="生成一个撤销最近一次提交的上-n-次提交的新提交"><a href="#生成一个撤销最近一次提交的上-n-次提交的新提交" class="headerlink" title="生成一个撤销最近一次提交的上 n 次提交的新提交"></a>生成一个撤销最近一次提交的上 n 次提交的新提交</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git revert HEAD~num</span><br></pre></td></tr></table></figure></li><li><h5 id="生成一个撤销指定提交版本的新提交"><a href="#生成一个撤销指定提交版本的新提交" class="headerlink" title="生成一个撤销指定提交版本的新提交"></a>生成一个撤销指定提交版本的新提交</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git revert <commit_id></span><br></pre></td></tr></table></figure></li><li><h5 id="生成一个撤销指定提交版本的新提交,执行时不打开默认编辑器,直接使用-Git-自动生成的提交信息"><a href="#生成一个撤销指定提交版本的新提交,执行时不打开默认编辑器,直接使用-Git-自动生成的提交信息" class="headerlink" title="生成一个撤销指定提交版本的新提交,执行时不打开默认编辑器,直接使用 Git 自动生成的提交信息"></a>生成一个撤销指定提交版本的新提交,执行时不打开默认编辑器,直接使用 Git 自动生成的提交信息</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git revert <commit_id> --no-edit</span><br></pre></td></tr></table></figure></li></ul></li></ul><h3 id="cherry-pick-将指定的提交-commit-应用于当前分支-可用于恢复不小心撤销-revert-reset-的提交"><a href="#cherry-pick-将指定的提交-commit-应用于当前分支-可用于恢复不小心撤销-revert-reset-的提交" class="headerlink" title="cherry-pick : 将指定的提交 commit 应用于当前分支 (可用于恢复不小心撤销 (revert/reset) 的提交)"></a>cherry-pick : 将指定的提交 commit 应用于当前分支 (可用于恢复不小心撤销 (revert/reset) 的提交)</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$ git cherry-pick <commit_id></span><br><span class="line">$ git cherry-pick <commit_id> <commit_id></span><br><span class="line">$ git cherry-pick <commit_id>^..<commit_id></span><br></pre></td></tr></table></figure>]]></content>
<tags>
<tag> Git </tag>
</tags>
</entry>
<entry>
<title>Git-command</title>
<link href="/2020/08/29/git-command/"/>
<url>/2020/08/29/git-command/</url>
<content type="html"><![CDATA[<h2 id="Git-常用命令以及使用情况"><a href="#Git-常用命令以及使用情况" class="headerlink" title="Git 常用命令以及使用情况"></a>Git 常用命令以及使用情况</h2><h3 id="1-配置-Git"><a href="#1-配置-Git" class="headerlink" title="1. 配置 Git"></a>1. 配置 Git</h3><ul><li><h4 id="配置全局用户"><a href="#配置全局用户" class="headerlink" title="配置全局用户"></a>配置全局用户</h4> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">$ git config --global user.name "用户名"</span><br><span class="line">$ git config --global user.email "git账号"</span><br></pre></td></tr></table></figure></li><li><h4 id="配置别名-非必要"><a href="#配置别名-非必要" class="headerlink" title="配置别名 (非必要)"></a>配置别名 (非必要)</h4> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">$ git config --global co checkout</span><br><span class="line">$ git config --global ss status</span><br><span class="line">$ git config --global cm commit</span><br><span class="line">$ git config --global br branch</span><br><span class="line">$ git config --global rg reflog</span><br></pre></td></tr></table></figure></li><li><h4 id="删除全局配置"><a href="#删除全局配置" class="headerlink" title="删除全局配置"></a>删除全局配置</h4> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">$ git config --global --unset (别名配置)</span><br><span class="line">$ git config --global --unset user.xxx</span><br></pre></td></tr></table></figure></li></ul><h3 id="2-查看-Git-信息"><a href="#2-查看-Git-信息" class="headerlink" title="2. 查看 Git 信息"></a>2. 查看 Git 信息</h3><ul><li><h4 id="查看系统配置"><a href="#查看系统配置" class="headerlink" title="查看系统配置"></a>查看系统配置</h4> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git config --list</span><br></pre></td></tr></table></figure></li><li><h4 id="查看用户配置"><a href="#查看用户配置" class="headerlink" title="查看用户配置"></a>查看用户配置</h4> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ cat ~/.gitconfig</span><br></pre></td></tr></table></figure></li><li><h4 id="查看当前项目的-git-配置"><a href="#查看当前项目的-git-配置" class="headerlink" title="查看当前项目的 git 配置"></a>查看当前项目的 git 配置</h4> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ cat .git/config</span><br></pre></td></tr></table></figure></li><li><h4 id="查看暂存区文件"><a href="#查看暂存区文件" class="headerlink" title="查看暂存区文件"></a>查看暂存区文件</h4> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git ls-files</span><br></pre></td></tr></table></figure></li><li><h4 id="查看本地-git-命令历史"><a href="#查看本地-git-命令历史" class="headerlink" title="查看本地 git 命令历史"></a>查看本地 git 命令历史</h4><h5 id="显示的是一个-HEAD-指向发生改变的时间列表,"><a href="#显示的是一个-HEAD-指向发生改变的时间列表," class="headerlink" title="(显示的是一个 HEAD 指向发生改变的时间列表,"></a>(显示的是一个 HEAD 指向发生改变的时间列表,</h5><h5 id="在切换分支、git-commit-提交、git-reset-撤销-commit时-HEAD-指向改变,可以使用-git-reset-head-02b5293-恢复"><a href="#在切换分支、git-commit-提交、git-reset-撤销-commit时-HEAD-指向改变,可以使用-git-reset-head-02b5293-恢复" class="headerlink" title="在切换分支、git commit 提交、git reset 撤销 commit时 HEAD 指向改变,可以使用 git reset --head 02b5293 恢复"></a>在切换分支、<code>git commit</code> 提交、<code>git reset</code> 撤销 commit时 HEAD 指向改变,可以使用 <code>git reset --head 02b5293</code> 恢复</h5><h5 id="在-git-checkout-lt-filename-gt-撤销、git-stash-存储文件时-HEAD-不会改变,这些修改也没有被提交,不可恢复"><a href="#在-git-checkout-lt-filename-gt-撤销、git-stash-存储文件时-HEAD-不会改变,这些修改也没有被提交,不可恢复" class="headerlink" title="在 git checkout -- <filename> 撤销、git stash 存储文件时 HEAD 不会改变,这些修改也没有被提交,不可恢复"></a>在 <code>git checkout -- <filename></code> 撤销、<code>git stash</code> 存储文件时 HEAD 不会改变,这些修改也没有被提交,不可恢复</h5><h5 id="git-reflog-会被定期清理"><a href="#git-reflog-会被定期清理" class="headerlink" title="git reflog 会被定期清理)"></a>git reflog 会被定期清理)</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git reflog</span><br></pre></td></tr></table></figure></li><li><h4 id="查看所有-git-命令"><a href="#查看所有-git-命令" class="headerlink" title="查看所有 git 命令"></a>查看所有 git 命令</h4> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git --help -a</span><br></pre></td></tr></table></figure></li><li><h4 id="查看当前-HERD-指向"><a href="#查看当前-HERD-指向" class="headerlink" title="查看当前 HERD 指向"></a>查看当前 HERD 指向</h4> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ cat .git/HEAD</span><br></pre></td></tr></table></figure></li><li><h4 id="git-查看多页文件时-D-向下翻一行-F-向下翻页-B-向上翻页-Q-退出"><a href="#git-查看多页文件时-D-向下翻一行-F-向下翻页-B-向上翻页-Q-退出" class="headerlink" title="git 查看多页文件时, D 向下翻一行, F 向下翻页, B 向上翻页, Q 退出"></a>git 查看多页文件时, <strong>D</strong> 向下翻一行, <strong>F</strong> 向下翻页, <strong>B</strong> 向上翻页, <strong>Q</strong> 退出</h4></li><li><h4 id="git-查看提交历史"><a href="#git-查看提交历史" class="headerlink" title="git 查看提交历史"></a>git 查看提交历史</h4><ul><li>git 中一条分支就是一个指针,新建一条分支就是基于当前指针新建一个指针</li><li>切换分支 : 将HEAD指向某条分支 (指针)</li><li>切换至某个 commit : 将 HEAD 指向某个 commit</li><li><code>git log --graph</code> 图形化符号含义<ul><li><code>*</code> : 一次 commit</li><li><code>|</code> : 分支前进</li><li><code>/</code> : 分叉</li><li><code>\</code> : 分之合入</li><li><code>|/</code> : 新分支<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">$ git log --online --> 将 commit 记录 逐行显示</span><br><span class="line"> --grep="关键字" --> 查找 commit 提交注释 中与关键字有关的记录</span><br><span class="line"> --graph --> 记录图形化显示</span><br><span class="line"> --all --> 将所有记录详细显示出来</span><br><span class="line"> --author "username" --> 查找此作者的提交记录</span><br><span class="line"> --reverse --> 将 commit 提交记录倒序显示</span><br><span class="line"> -num --> 显示最近 num 次提交 git log -10</span><br><span class="line"> -p --> 显示每次提交引入的差异 (按 补丁 的格式输出)</span><br><span class="line"> --before = 1 day/1 week/1 "2020-08-29" --> 查找规定时间之前的记录 (一天, 一周)</span><br><span class="line"> --after = "2020-8-23" --> 查找规定时间之后的操作</span><br><span class="line"> --stat --> 显示每次更新的文件修改信息统计,会列出具体文件列表</span><br><span class="line"> --abbrev-commit --> 仅显示 SHA-1 的前几个字符,而非所有 40 个字符</span><br><span class="line"> --pretty=format:"xxx" --> 可以定制要显示的记录格式</span><br></pre></td></tr></table></figure></li></ul></li></ul></li></ul><h3 id="3-Git-常用命令"><a href="#3-Git-常用命令" class="headerlink" title="3. Git 常用命令"></a>3. Git 常用命令</h3><ul><li><h4 id="查看工作区和暂存区的状态"><a href="#查看工作区和暂存区的状态" class="headerlink" title="查看工作区和暂存区的状态"></a>查看工作区和暂存区的状态</h4> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git status</span><br></pre></td></tr></table></figure></li><li><h4 id="add-将工作区的文件提交到暂存区"><a href="#add-将工作区的文件提交到暂存区" class="headerlink" title="add 将工作区的文件提交到暂存区"></a><code>add</code> 将工作区的文件提交到暂存区</h4><ul><li><h5 id="添加指定文件到暂存区-追踪新增的指定文件"><a href="#添加指定文件到暂存区-追踪新增的指定文件" class="headerlink" title="添加指定文件到暂存区 (追踪新增的指定文件)"></a>添加指定文件到暂存区 (追踪新增的指定文件)</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git add [file1] [file2] ...</span><br></pre></td></tr></table></figure></li><li><h5 id="添加指定文件到暂存区,包括子目录"><a href="#添加指定文件到暂存区,包括子目录" class="headerlink" title="添加指定文件到暂存区,包括子目录"></a>添加指定文件到暂存区,包括子目录</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git add [dir]</span><br></pre></td></tr></table></figure></li><li><h5 id="添加当前目录的所有文件到暂存区(追踪所有新增的文件)"><a href="#添加当前目录的所有文件到暂存区(追踪所有新增的文件)" class="headerlink" title="添加当前目录的所有文件到暂存区(追踪所有新增的文件)"></a>添加当前目录的所有文件到暂存区(追踪所有新增的文件)</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$ git add .</span><br><span class="line"> <==></span><br><span class="line">$ git add -a</span><br></pre></td></tr></table></figure></li><li><h5 id="删除工作区-暂存区的文件"><a href="#删除工作区-暂存区的文件" class="headerlink" title="删除工作区/暂存区的文件"></a>删除工作区/暂存区的文件</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git rm [file1] [file2] ...</span><br></pre></td></tr></table></figure></li><li><h5 id="停止追踪指定文件,但该文件会保留在工作区"><a href="#停止追踪指定文件,但该文件会保留在工作区" class="headerlink" title="停止追踪指定文件,但该文件会保留在工作区"></a>停止追踪指定文件,但该文件会保留在工作区</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git rm --cached [file]</span><br></pre></td></tr></table></figure></li><li><h5 id="改名工作区-暂存区的文件"><a href="#改名工作区-暂存区的文件" class="headerlink" title="改名工作区/暂存区的文件"></a>改名工作区/暂存区的文件</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git mv [old_filename] [new_filename]</span><br></pre></td></tr></table></figure></li></ul></li><li><h4 id="commit-将暂存区文件提交到本地仓库"><a href="#commit-将暂存区文件提交到本地仓库" class="headerlink" title="commit 将暂存区文件提交到本地仓库"></a><code>commit</code> 将暂存区文件提交到本地仓库</h4><ul><li><h5 id="将暂存区的文件提交到本地仓库并添加提交说明"><a href="#将暂存区的文件提交到本地仓库并添加提交说明" class="headerlink" title="将暂存区的文件提交到本地仓库并添加提交说明"></a>将暂存区的文件提交到本地仓库并添加提交说明</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git commit -m "本次提交说明"</span><br></pre></td></tr></table></figure></li><li><h5 id="跳过验证继续提交"><a href="#跳过验证继续提交" class="headerlink" title="跳过验证继续提交"></a>跳过验证继续提交</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">$ git commit --no-verify</span><br><span class="line">$ git commit -n</span><br></pre></td></tr></table></figure></li><li><h5 id="add-和-commit-合并,便捷写法-未追踪的文件无法提交"><a href="#add-和-commit-合并,便捷写法-未追踪的文件无法提交" class="headerlink" title="add 和 commit 合并,便捷写法 (未追踪的文件无法提交)"></a>add 和 commit 合并,便捷写法 (未追踪的文件无法提交)</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git commit -am "本次提交说明"</span><br></pre></td></tr></table></figure></li><li><h5 id="编辑器会弹出上一次提交的信息,可以在这里修改提交信息-既可以修改上次提交的文件内容,也可以修改上次提交的说明"><a href="#编辑器会弹出上一次提交的信息,可以在这里修改提交信息-既可以修改上次提交的文件内容,也可以修改上次提交的说明" class="headerlink" title="编辑器会弹出上一次提交的信息,可以在这里修改提交信息 (既可以修改上次提交的文件内容,也可以修改上次提交的说明)"></a>编辑器会弹出上一次提交的信息,可以在这里修改提交信息 (既可以修改上次提交的文件内容,也可以修改上次提交的说明)</h5><h5 id="如果暂存区有内容,这个新的-commit-会把任何修改内容和上一个-commit-的内容结合起来。如果暂存区没有内容,那么这个操作就只会把上次的-commit-消息重写一遍。-永远不要修复一个已经推送到公共仓库中的提交,会拒绝推送到仓库"><a href="#如果暂存区有内容,这个新的-commit-会把任何修改内容和上一个-commit-的内容结合起来。如果暂存区没有内容,那么这个操作就只会把上次的-commit-消息重写一遍。-永远不要修复一个已经推送到公共仓库中的提交,会拒绝推送到仓库" class="headerlink" title="如果暂存区有内容,这个新的 commit 会把任何修改内容和上一个 commit 的内容结合起来。如果暂存区没有内容,那么这个操作就只会把上次的 commit 消息重写一遍。[永远不要修复一个已经推送到公共仓库中的提交,会拒绝推送到仓库]"></a>如果暂存区有内容,这个新的 commit 会把任何修改内容和上一个 commit 的内容结合起来。如果暂存区没有内容,那么这个操作就只会把上次的 commit 消息重写一遍。[永远不要修复一个已经推送到公共仓库中的提交,会拒绝推送到仓库]</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git commit --amend</span><br></pre></td></tr></table></figure></li><li><h5 id="修复提交,同时修改提交信息"><a href="#修复提交,同时修改提交信息" class="headerlink" title="修复提交,同时修改提交信息"></a>修复提交,同时修改提交信息</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git commit --amend -m "本次提交说明"</span><br></pre></td></tr></table></figure></li><li><h5 id="加入-–no-edit-标记会修复提交但不修改提交信息,编辑器不会弹出上一次提交的信息"><a href="#加入-–no-edit-标记会修复提交但不修改提交信息,编辑器不会弹出上一次提交的信息" class="headerlink" title="加入 –no-edit 标记会修复提交但不修改提交信息,编辑器不会弹出上一次提交的信息"></a>加入 –no-edit 标记会修复提交但不修改提交信息,编辑器不会弹出上一次提交的信息</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git commit --amend --no-edit</span><br></pre></td></tr></table></figure></li></ul></li><li><h4 id="将本地分支与远程分支关联"><a href="#将本地分支与远程分支关联" class="headerlink" title="将本地分支与远程分支关联"></a>将本地分支与远程分支关联</h4> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git push -u origin BranchName</span><br></pre></td></tr></table></figure></li><li><h4 id="将本地仓库文件推送到远程分支-如果远程仓库没有这个分支,会新建一个同名远程分支,省略分支名代表两者同名-分支推送顺序-lt-来源地-gt-lt-目的地-gt"><a href="#将本地仓库文件推送到远程分支-如果远程仓库没有这个分支,会新建一个同名远程分支,省略分支名代表两者同名-分支推送顺序-lt-来源地-gt-lt-目的地-gt" class="headerlink" title="将本地仓库文件推送到远程分支,如果远程仓库没有这个分支,会新建一个同名远程分支,省略分支名代表两者同名 (分支推送顺序 [<来源地> : <目的地>])"></a>将本地仓库文件推送到远程分支,如果远程仓库没有这个分支,会新建一个同名远程分支,省略分支名代表两者同名 (分支推送顺序 [<来源地> : <目的地>])</h4> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">$ git push <远程主机名> <本地分支名>:<远程分支名></span><br><span class="line">$ git push origin BranchName</span><br></pre></td></tr></table></figure><ul><li><h5 id="省略本地分支名,表示删除指定的远程分支-lt-gt-推送一个空的本地分支到远程分支"><a href="#省略本地分支名,表示删除指定的远程分支-lt-gt-推送一个空的本地分支到远程分支" class="headerlink" title="省略本地分支名,表示删除指定的远程分支 <==> 推送一个空的本地分支到远程分支"></a>省略本地分支名,表示删除指定的远程分支 <==> 推送一个空的本地分支到远程分支</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$ git push origin :master</span><br><span class="line"> <==></span><br><span class="line">$ git push origin --delete master</span><br></pre></td></tr></table></figure></li><li><h5 id="不管是否存在对应的远程分支,将本地的所有分支都推送到远程主机"><a href="#不管是否存在对应的远程分支,将本地的所有分支都推送到远程主机" class="headerlink" title="不管是否存在对应的远程分支,将本地的所有分支都推送到远程主机"></a>不管是否存在对应的远程分支,将本地的所有分支都推送到远程主机</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git push --all origin</span><br></pre></td></tr></table></figure></li><li><h5 id="建立当前分支和远程分支的追踪关系-如果当前分支与远程分支之间存在追踪关系-推送时则可以省略分支和-u"><a href="#建立当前分支和远程分支的追踪关系-如果当前分支与远程分支之间存在追踪关系-推送时则可以省略分支和-u" class="headerlink" title="建立当前分支和远程分支的追踪关系 (如果当前分支与远程分支之间存在追踪关系, 推送时则可以省略分支和 -u)"></a>建立当前分支和远程分支的追踪关系 (如果当前分支与远程分支之间存在追踪关系, 推送时则可以省略分支和 -u)</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"># 不存在追踪</span><br><span class="line">$ git push -u origin master </span><br><span class="line"># 存在 追踪</span><br><span class="line">$ git push</span><br></pre></td></tr></table></figure></li><li><h5 id="如果远程主机的版本比本地版本更新,推送时-Git-会报错,要求先在本地做-git-pull-合并差异,然后再推送到远程主机。这时,如果你一定要推送,可以使用-–-force-选项-尽量避免使用"><a href="#如果远程主机的版本比本地版本更新,推送时-Git-会报错,要求先在本地做-git-pull-合并差异,然后再推送到远程主机。这时,如果你一定要推送,可以使用-–-force-选项-尽量避免使用" class="headerlink" title="如果远程主机的版本比本地版本更新,推送时 Git 会报错,要求先在本地做 git pull 合并差异,然后再推送到远程主机。这时,如果你一定要推送,可以使用 –-force 选项 (尽量避免使用)"></a>如果远程主机的版本比本地版本更新,推送时 Git 会报错,要求先在本地做 git pull 合并差异,然后再推送到远程主机。这时,如果你一定要推送,可以使用 –-force 选项 (尽量避免使用)</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git push --force origin | git push -f origin</span><br></pre></td></tr></table></figure></li></ul></li></ul><ul><li><h4 id="拉取远程分支的代码到本地镜像仓库"><a href="#拉取远程分支的代码到本地镜像仓库" class="headerlink" title="拉取远程分支的代码到本地镜像仓库"></a>拉取远程分支的代码到本地镜像仓库</h4> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git pull</span><br></pre></td></tr></table></figure><ul><li><h5 id="拉取并合并项目其他人的一个分支-lt-gt-fetch-merge"><a href="#拉取并合并项目其他人的一个分支-lt-gt-fetch-merge" class="headerlink" title="拉取并合并项目其他人的一个分支 <==> fetch + merge"></a>拉取并合并项目其他人的一个分支 <==> fetch + merge</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">$ git pull origin BranchName</span><br><span class="line"> <==></span><br><span class="line">$ git fetch origin BranchName</span><br><span class="line">$ git merge origin/BranchName</span><br></pre></td></tr></table></figure></li></ul></li><li><h4 id="合并分支"><a href="#合并分支" class="headerlink" title="合并分支"></a>合并分支</h4><ol><li><h5 id="默认-fast-forward-,HEAD-指针直接指向被合并的分支"><a href="#默认-fast-forward-,HEAD-指针直接指向被合并的分支" class="headerlink" title="默认 fast-forward ,HEAD 指针直接指向被合并的分支"></a>默认 fast-forward ,HEAD 指针直接指向被合并的分支</h5><h5 id="在当前分支的提交历史中添加进被合并分支的提交历史"><a href="#在当前分支的提交历史中添加进被合并分支的提交历史" class="headerlink" title="在当前分支的提交历史中添加进被合并分支的提交历史"></a>在当前分支的提交历史中添加进被合并分支的提交历史</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git merge BranchName</span><br></pre></td></tr></table></figure></li><li><h5 id="禁止快进式合并"><a href="#禁止快进式合并" class="headerlink" title="禁止快进式合并"></a>禁止快进式合并</h5><h5 id="生成一个新的提交-,会让当前分支的提交历史不会那么乱"><a href="#生成一个新的提交-,会让当前分支的提交历史不会那么乱" class="headerlink" title="[生成一个新的提交],会让当前分支的提交历史不会那么乱"></a>[生成一个新的提交],会让当前分支的提交历史不会那么乱</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git merge --no-ff</span><br></pre></td></tr></table></figure></li><li><h5 id="squash:-不会生成新的提交-,会将被合并分支多次提交的内容直接存到工作区和暂存区,由开发者手动去提交,这样当前分支最终只会多出一条提交记录,不会掺杂被合并分支的提交历史"><a href="#squash:-不会生成新的提交-,会将被合并分支多次提交的内容直接存到工作区和暂存区,由开发者手动去提交,这样当前分支最终只会多出一条提交记录,不会掺杂被合并分支的提交历史" class="headerlink" title="squash:[不会生成新的提交],会将被合并分支多次提交的内容直接存到工作区和暂存区,由开发者手动去提交,这样当前分支最终只会多出一条提交记录,不会掺杂被合并分支的提交历史"></a>squash:[不会生成新的提交],会将被合并分支多次提交的内容直接存到工作区和暂存区,由开发者手动去提交,这样当前分支最终只会多出一条提交记录,不会掺杂被合并分支的提交历史</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git merge --squash</span><br></pre></td></tr></table></figure></li></ol></li><li><h4 id="查看本地分支"><a href="#查看本地分支" class="headerlink" title="查看本地分支"></a>查看本地分支</h4> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$ git branch </span><br><span class="line"> <==> </span><br><span class="line">$ git branch -l</span><br></pre></td></tr></table></figure><ul><li><h5 id="查看本地分支对应的远程分支"><a href="#查看本地分支对应的远程分支" class="headerlink" title="查看本地分支对应的远程分支"></a>查看本地分支对应的远程分支</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git branch -vv</span><br></pre></td></tr></table></figure></li><li><h5 id="查看远程分支"><a href="#查看远程分支" class="headerlink" title="查看远程分支"></a>查看远程分支</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git branch -r</span><br></pre></td></tr></table></figure></li><li><h5 id="查看所有分支-包括本地分支与远程分支"><a href="#查看所有分支-包括本地分支与远程分支" class="headerlink" title="查看所有分支 (包括本地分支与远程分支)"></a>查看所有分支 (包括本地分支与远程分支)</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git branch -a</span><br></pre></td></tr></table></figure><ul><li><h6 id="查看所有分支包括最新的提交信息"><a href="#查看所有分支包括最新的提交信息" class="headerlink" title="查看所有分支包括最新的提交信息"></a>查看所有分支包括最新的提交信息</h6> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git branch -av</span><br></pre></td></tr></table></figure></li></ul></li><li><h5 id="新建分支,在别的分支下新建一个分支,新分支会复制当前分支的内容,-注意:如果当前分支有修改,但是没有提交到仓库,此时修改的内容是不会被复制到新分支的"><a href="#新建分支,在别的分支下新建一个分支,新分支会复制当前分支的内容,-注意:如果当前分支有修改,但是没有提交到仓库,此时修改的内容是不会被复制到新分支的" class="headerlink" title="新建分支,在别的分支下新建一个分支,新分支会复制当前分支的内容,(注意:如果当前分支有修改,但是没有提交到仓库,此时修改的内容是不会被复制到新分支的)"></a>新建分支,在别的分支下新建一个分支,新分支会复制当前分支的内容,(注意:如果当前分支有修改,但是没有提交到仓库,此时修改的内容是不会被复制到新分支的)</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git branch BranchName</span><br></pre></td></tr></table></figure></li><li><h5 id="切换分支-切换分支时,本地工作区,仓库都会相应切换到对应分支的内容"><a href="#切换分支-切换分支时,本地工作区,仓库都会相应切换到对应分支的内容" class="headerlink" title="切换分支 (切换分支时,本地工作区,仓库都会相应切换到对应分支的内容)"></a>切换分支 (切换分支时,本地工作区,仓库都会相应切换到对应分支的内容)</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git checkout BranchName</span><br></pre></td></tr></table></figure><ul><li><h6 id="创建并切换到新建分支"><a href="#创建并切换到新建分支" class="headerlink" title="创建并切换到新建分支"></a>创建并切换到新建分支</h6> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git checkout -b BranchName</span><br></pre></td></tr></table></figure></li><li><h6 id="基于-master-分支新建一个分支,并切换到该分支"><a href="#基于-master-分支新建一个分支,并切换到该分支" class="headerlink" title="基于 master 分支新建一个分支,并切换到该分支"></a>基于 master 分支新建一个分支,并切换到该分支</h6> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git checkout -b BranchName master</span><br></pre></td></tr></table></figure></li></ul></li><li><h5 id="删除本地分支,会阻止删除包含未合并并更改的分支"><a href="#删除本地分支,会阻止删除包含未合并并更改的分支" class="headerlink" title="删除本地分支,会阻止删除包含未合并并更改的分支"></a>删除本地分支,会阻止删除包含未合并并更改的分支</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git branch -d BranchName</span><br></pre></td></tr></table></figure><ul><li><h6 id="强制删除一个本地分支,即使有未合并并更改的分支"><a href="#强制删除一个本地分支,即使有未合并并更改的分支" class="headerlink" title="强制删除一个本地分支,即使有未合并并更改的分支"></a>强制删除一个本地分支,即使有未合并并更改的分支</h6> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git branch -D BranchName</span><br></pre></td></tr></table></figure></li></ul></li><li><h5 id="修改当前分支名"><a href="#修改当前分支名" class="headerlink" title="修改当前分支名"></a>修改当前分支名</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git branch -m BranchName</span><br></pre></td></tr></table></figure></li></ul></li><li><h4 id="临时将工作区文件修改保存至堆栈,用于后续恢复当前工作区内容-以便于新建一个分支进行操作而不被当前分支影响"><a href="#临时将工作区文件修改保存至堆栈,用于后续恢复当前工作区内容-以便于新建一个分支进行操作而不被当前分支影响" class="headerlink" title="临时将工作区文件修改保存至堆栈,用于后续恢复当前工作区内容 (以便于新建一个分支进行操作而不被当前分支影响)"></a>临时将工作区文件修改保存至堆栈,用于后续恢复当前工作区内容 (以便于新建一个分支进行操作而不被当前分支影响)</h4><h4 id="如果文件没有提交到暂存区-(使用-git-add-追踪新的文件)-,使用该命令会提示-No-local-changes-to-save-,无法将修改保存到堆栈中"><a href="#如果文件没有提交到暂存区-(使用-git-add-追踪新的文件)-,使用该命令会提示-No-local-changes-to-save-,无法将修改保存到堆栈中" class="headerlink" title="如果文件没有提交到暂存区[(使用 git add . 追踪新的文件)],使用该命令会提示 No local changes to save ,无法将修改保存到堆栈中"></a>如果文件没有提交到暂存区[(使用 git add . 追踪新的文件)],使用该命令会提示 No local changes to save ,无法将修改保存到堆栈中</h4><ol><li><h5 id="将所有未提交的修改(提交到暂存区)保存至堆栈中"><a href="#将所有未提交的修改(提交到暂存区)保存至堆栈中" class="headerlink" title="将所有未提交的修改(提交到暂存区)保存至堆栈中"></a>将所有未提交的修改(提交到暂存区)保存至堆栈中</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git stash</span><br></pre></td></tr></table></figure></li><li><h5 id="给本次存储加个备注,以防时间久了忘了"><a href="#给本次存储加个备注,以防时间久了忘了" class="headerlink" title="给本次存储加个备注,以防时间久了忘了"></a>给本次存储加个备注,以防时间久了忘了</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git status save "临时存储"</span><br></pre></td></tr></table></figure></li><li><h5 id="查看存储记录"><a href="#查看存储记录" class="headerlink" title="查看存储记录"></a>查看存储记录</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git stash list</span><br></pre></td></tr></table></figure></li><li><h5 id="在-Windows-上和-PowerShell-中,需要加双引号"><a href="#在-Windows-上和-PowerShell-中,需要加双引号" class="headerlink" title="在 Windows 上和 PowerShell 中,需要加双引号"></a>在 Windows 上和 PowerShell 中,需要加双引号</h5><ul><li><h6 id="在-Windows-上和-PowerShell-中,需要加双引号-1"><a href="#在-Windows-上和-PowerShell-中,需要加双引号-1" class="headerlink" title="在 Windows 上和 PowerShell 中,需要加双引号"></a>在 Windows 上和 PowerShell 中,需要加双引号</h6> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git stash apply "stash@{index}"</span><br></pre></td></tr></table></figure></li><li><h6 id="恢复的同时把-stash-记录也删了"><a href="#恢复的同时把-stash-记录也删了" class="headerlink" title="恢复的同时把 stash 记录也删了"></a>恢复的同时把 stash 记录也删了</h6> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git stash pop "stash@{index}"</span><br></pre></td></tr></table></figure></li><li><h6 id="删除-stash-记录"><a href="#删除-stash-记录" class="headerlink" title="删除 stash 记录"></a>删除 stash 记录</h6> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git stash drop "stash@{index}"</span><br></pre></td></tr></table></figure></li><li><h5 id="删除所有存储的进度"><a href="#删除所有存储的进度" class="headerlink" title="删除所有存储的进度"></a>删除所有存储的进度</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git stash clear</span><br></pre></td></tr></table></figure></li></ul></li></ol><ul><li><h5 id="你接到一个修复紧急-bug-的任务时候,一般都是先创建一个新的-bug-分支来修复它,然后合并,最后删除。"><a href="#你接到一个修复紧急-bug-的任务时候,一般都是先创建一个新的-bug-分支来修复它,然后合并,最后删除。" class="headerlink" title="你接到一个修复紧急 bug 的任务时候,一般都是先创建一个新的 bug 分支来修复它,然后合并,最后删除。"></a>你接到一个修复紧急 bug 的任务时候,一般都是先创建一个新的 bug 分支来修复它,然后合并,最后删除。</h5><h5 id="但是,如果当前你正在开发功能中,短时间还无法完成,无法直接提交到仓库,这时候可以先把当前工作区的内容-git-stash-一下,然后去修复-bug,修复后,再-git-stash-pop,恢复之前的工作内容。"><a href="#但是,如果当前你正在开发功能中,短时间还无法完成,无法直接提交到仓库,这时候可以先把当前工作区的内容-git-stash-一下,然后去修复-bug,修复后,再-git-stash-pop,恢复之前的工作内容。" class="headerlink" title="但是,如果当前你正在开发功能中,短时间还无法完成,无法直接提交到仓库,这时候可以先把当前工作区的内容 git stash 一下,然后去修复 bug,修复后,再 git stash pop,恢复之前的工作内容。"></a>但是,如果当前你正在开发功能中,短时间还无法完成,无法直接提交到仓库,这时候可以先把当前工作区的内容 git stash 一下,然后去修复 bug,修复后,再 git stash pop,恢复之前的工作内容。</h5></li></ul></li><li><h4 id="diff"><a href="#diff" class="headerlink" title="diff"></a>diff</h4><ul><li><h5 id="查看工作区和暂存区单个文件的对比"><a href="#查看工作区和暂存区单个文件的对比" class="headerlink" title="查看工作区和暂存区单个文件的对比"></a>查看工作区和暂存区单个文件的对比</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git diff filename</span><br></pre></td></tr></table></figure></li><li><h5 id="查看工作区和暂存区所有文件的对比"><a href="#查看工作区和暂存区所有文件的对比" class="headerlink" title="查看工作区和暂存区所有文件的对比"></a>查看工作区和暂存区所有文件的对比</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git diff</span><br></pre></td></tr></table></figure></li><li><h5 id="查看工作区和暂存区所有文件的对比,并显示出所有有差异的文件列表"><a href="#查看工作区和暂存区所有文件的对比,并显示出所有有差异的文件列表" class="headerlink" title="查看工作区和暂存区所有文件的对比,并显示出所有有差异的文件列表"></a>查看工作区和暂存区所有文件的对比,并显示出所有有差异的文件列表</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">$ git diff --stat</span><br><span class="line"># 1.你修改了某个文件,但是没有提交到暂存区,这时候会有对比的内容</span><br><span class="line"># 一旦提交到暂存区,就不会有对比的内容(因为暂存区已经更新)</span><br><span class="line"># 2.如果你新建了一个文件,但是没有提交到暂存区,这时候 diff 是没有结果的</span><br></pre></td></tr></table></figure></li><li><h5 id="查看远程分支和本地分支的对比"><a href="#查看远程分支和本地分支的对比" class="headerlink" title="查看远程分支和本地分支的对比"></a>查看远程分支和本地分支的对比</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git diff origin/branchname..branchname</span><br></pre></td></tr></table></figure></li><li><h5 id="查看两个本地分支所有的对比"><a href="#查看两个本地分支所有的对比" class="headerlink" title="查看两个本地分支所有的对比"></a>查看两个本地分支所有的对比</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git diff branchname..branchname</span><br></pre></td></tr></table></figure></li></ul></li><li><h3 id="remote"><a href="#remote" class="headerlink" title="remote"></a>remote</h3><ul><li><h4 id="查看所有远程主机"><a href="#查看所有远程主机" class="headerlink" title="查看所有远程主机"></a>查看所有远程主机</h4> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git remote</span><br></pre></td></tr></table></figure></li><li><h4 id="查看关联的远程仓库的详细信息"><a href="#查看关联的远程仓库的详细信息" class="headerlink" title="查看关联的远程仓库的详细信息"></a>查看关联的远程仓库的详细信息</h4> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git remote -v</span><br></pre></td></tr></table></figure></li><li><h4 id="删除远程仓库的-“关联”"><a href="#删除远程仓库的-“关联”" class="headerlink" title="删除远程仓库的 “关联”"></a>删除远程仓库的 “关联”</h4> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git remote rm ProjectName</span><br></pre></td></tr></table></figure></li><li><h4 id="设置远程仓库的-“关联”"><a href="#设置远程仓库的-“关联”" class="headerlink" title="设置远程仓库的 “关联”"></a>设置远程仓库的 “关联”</h4> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git remote set-url origin <newurl></span><br></pre></td></tr></table></figure></li></ul></li></ul>]]></content>
<tags>
<tag> Git </tag>
</tags>
</entry>
<entry>
<title>Git-reset使用掉坑</title>
<link href="/2020/08/27/git-reset-shi-yong-diao-keng/"/>
<url>/2020/08/27/git-reset-shi-yong-diao-keng/</url>
<content type="html"><![CDATA[<h1 id="Git-的一次掉坑经历-深坑"><a href="#Git-的一次掉坑经历-深坑" class="headerlink" title="Git 的一次掉坑经历 (深坑)"></a>Git 的一次掉坑经历 (深坑)</h1><ol><li><h3 id="2020-08-24-作死开始"><a href="#2020-08-24-作死开始" class="headerlink" title="2020.08.24 作死开始"></a>2020.08.24 作死开始</h3><h4 id="早九点,最近发现-git-跟-pycharm-混用让自己-git-的学习过程出了点问题,尝试多次无果后,决定去网上求助大神。"><a href="#早九点,最近发现-git-跟-pycharm-混用让自己-git-的学习过程出了点问题,尝试多次无果后,决定去网上求助大神。" class="headerlink" title="早九点,最近发现 git 跟 pycharm 混用让自己 git 的学习过程出了点问题,尝试多次无果后,决定去网上求助大神。"></a>早九点,最近发现 git 跟 pycharm 混用让自己 git 的学习过程出了点问题,尝试多次无果后,决定去网上求助大神。</h4></li><li><h3 id="愚蠢作死"><a href="#愚蠢作死" class="headerlink" title="愚蠢作死"></a>愚蠢作死</h3><h4 id="出于对网友们的信任,找到一片看起来靠谱的博客后果断一遍无脑操作,复制粘贴,在-回车-的时候-看到-git-reset-hard-,顿觉大事不妙-由于之前使用-git-最近一次是在去年学习时使用,疫情期间,全程看书,丝毫没有将代码-git-add-与-git-commit-的觉悟"><a href="#出于对网友们的信任,找到一片看起来靠谱的博客后果断一遍无脑操作,复制粘贴,在-回车-的时候-看到-git-reset-hard-,顿觉大事不妙-由于之前使用-git-最近一次是在去年学习时使用,疫情期间,全程看书,丝毫没有将代码-git-add-与-git-commit-的觉悟" class="headerlink" title="出于对网友们的信任,找到一片看起来靠谱的博客后果断一遍无脑操作,复制粘贴,在 回车 的时候 看到 git reset --hard ,顿觉大事不妙 (由于之前使用 git 最近一次是在去年学习时使用,疫情期间,全程看书,丝毫没有将代码 git add 与 git commit 的觉悟)"></a>出于对网友们的信任,找到一片看起来靠谱的博客后果断一遍无脑操作,复制粘贴,在 回车 的时候 看到 <code>git reset --hard</code> ,顿觉大事不妙 (由于之前使用 git 最近一次是在去年学习时使用,疫情期间,全程看书,丝毫没有将代码 git add 与 git commit 的觉悟)</h4></li><li><h3 id="“皇上”驾崩了"><a href="#“皇上”驾崩了" class="headerlink" title="“皇上”驾崩了"></a>“皇上”驾崩了</h3><h4 id="没了没了,全都没了。回过神的自己看着近一年-兢兢业业-“耕种”-的结果,全都丢了,真删了个干净,空空如也。"><a href="#没了没了,全都没了。回过神的自己看着近一年-兢兢业业-“耕种”-的结果,全都丢了,真删了个干净,空空如也。" class="headerlink" title="没了没了,全都没了。回过神的自己看着近一年 兢兢业业 “耕种” 的结果,全都丢了,真删了个干净,空空如也。"></a>没了没了,全都没了。回过神的自己看着近一年 兢兢业业 “耕种” 的结果,全都丢了,真删了个干净,空空如也。</h4></li><li><h3 id="慌了"><a href="#慌了" class="headerlink" title="慌了"></a>慌了</h3><h4 id="当时自己告诉自己镇静,网上慢慢找恢复办法,虽然告诉自己别慌,自己也觉得自己没慌,但事后就后悔,非常后悔,当时绝对是慌了,尽管对网上的办法保持警惕,但还是胡乱操作了许多,更乱了"><a href="#当时自己告诉自己镇静,网上慢慢找恢复办法,虽然告诉自己别慌,自己也觉得自己没慌,但事后就后悔,非常后悔,当时绝对是慌了,尽管对网上的办法保持警惕,但还是胡乱操作了许多,更乱了" class="headerlink" title="当时自己告诉自己镇静,网上慢慢找恢复办法,虽然告诉自己别慌,自己也觉得自己没慌,但事后就后悔,非常后悔,当时绝对是慌了,尽管对网上的办法保持警惕,但还是胡乱操作了许多,更乱了"></a>当时自己告诉自己镇静,网上慢慢找恢复办法,虽然告诉自己别慌,自己也觉得自己没慌,但事后就后悔,非常后悔,当时绝对是慌了,尽管对网上的办法保持警惕,但还是胡乱操作了许多,更乱了</h4><h4 id="尝试了回退,但是最近一次-git-commit-太过于遥远,仅恢复了-30-文件,心还在滴血-几千个代码文件、几十个笔记文件啊"><a href="#尝试了回退,但是最近一次-git-commit-太过于遥远,仅恢复了-30-文件,心还在滴血-几千个代码文件、几十个笔记文件啊" class="headerlink" title="尝试了回退,但是最近一次 git commit 太过于遥远,仅恢复了 30% 文件,心还在滴血 (几千个代码文件、几十个笔记文件啊)"></a>尝试了回退,但是最近一次 git commit 太过于遥远,仅恢复了 30% 文件,心还在滴血 (几千个代码文件、几十个笔记文件啊)</h4></li><li><h3 id="一篇有用的-Blog-https-www-cnblogs-com-hope-markup-p-6683522-html"><a href="#一篇有用的-Blog-https-www-cnblogs-com-hope-markup-p-6683522-html" class="headerlink" title="一篇有用的 Blog : https://www.cnblogs.com/hope-markup/p/6683522.html"></a>一篇有用的 Blog : <a href="https://www.cnblogs.com/hope-markup/p/6683522.html" target="_blank" rel="noopener">https://www.cnblogs.com/hope-markup/p/6683522.html</a></h3><h4 id="绝望的时候,找到了这篇博客-救我于水火-git-fsck-lost-found-这个命令-执行后在-git-lost-found-other-中找到了大堆的-文件-这里面包含了所有的没有被commit的文件-根据博客作者所说-find-git-objects-type-f-xargs-ls-lt-sed-60q-这个命令,你就可以找到最近被你add到本地仓库的-60个文件-可以自定义查看多少个"><a href="#绝望的时候,找到了这篇博客-救我于水火-git-fsck-lost-found-这个命令-执行后在-git-lost-found-other-中找到了大堆的-文件-这里面包含了所有的没有被commit的文件-根据博客作者所说-find-git-objects-type-f-xargs-ls-lt-sed-60q-这个命令,你就可以找到最近被你add到本地仓库的-60个文件-可以自定义查看多少个" class="headerlink" title="绝望的时候,找到了这篇博客 (救我于水火) , git fsck --lost-found 这个命令 执行后在 .git/lost-found/other 中找到了大堆的 文件 (这里面包含了所有的没有被commit的文件), 根据博客作者所说 find .git/objects -type f | xargs ls -lt | sed 60q 这个命令,你就可以找到最近被你add到本地仓库的 60个文件 (可以自定义查看多少个)"></a>绝望的时候,找到了这篇博客 (救我于水火) , <code>git fsck --lost-found</code> 这个命令 执行后在 <strong>.git/lost-found/other</strong> 中找到了大堆的 文件 (这里面包含了所有的没有被commit的文件), 根据博客作者所说 <code>find .git/objects -type f | xargs ls -lt | sed 60q</code> 这个命令,你就可以找到最近被你add到本地仓库的 <strong>60</strong>个文件 (可以自定义查看多少个)</h4></li><li><h3 id="开始恢复-悲惨的"><a href="#开始恢复-悲惨的" class="headerlink" title="开始恢复 (悲惨的)"></a>开始恢复 (悲惨的)</h3><h4 id="由于我丢失的文件太多,看着-other-文件夹下生成那浩浩荡荡-六七千个-文件-文件名是-hash-值,由于-git-reset-时将索引树一起重置了,没办法根据索引定义文件名-,完全不知道哪个是哪个"><a href="#由于我丢失的文件太多,看着-other-文件夹下生成那浩浩荡荡-六七千个-文件-文件名是-hash-值,由于-git-reset-时将索引树一起重置了,没办法根据索引定义文件名-,完全不知道哪个是哪个" class="headerlink" title="由于我丢失的文件太多,看着 other 文件夹下生成那浩浩荡荡 六七千个 文件 (文件名是 hash 值,由于 git reset 时将索引树一起重置了,没办法根据索引定义文件名),完全不知道哪个是哪个"></a>由于我丢失的文件太多,看着 other 文件夹下生成那浩浩荡荡 六七千个 文件 (文件名是 hash 值,由于 git reset 时将索引树一起重置了,没办法根据索引定义文件名),完全不知道哪个是哪个</h4><h4 id="尽管当时内心十分清楚的告诉自己写个脚本,写个脚本就可以快速恢复,但是对于自己不清楚的文件内容,自己毫无头绪-一定要把里面悬空文件多复制几份,然后对复制后的文件进行修改,保证损失环境的-无变化"><a href="#尽管当时内心十分清楚的告诉自己写个脚本,写个脚本就可以快速恢复,但是对于自己不清楚的文件内容,自己毫无头绪-一定要把里面悬空文件多复制几份,然后对复制后的文件进行修改,保证损失环境的-无变化" class="headerlink" title="尽管当时内心十分清楚的告诉自己写个脚本,写个脚本就可以快速恢复,但是对于自己不清楚的文件内容,自己毫无头绪 (一定要把里面悬空文件多复制几份,然后对复制后的文件进行修改,保证损失环境的 无变化)"></a>尽管当时内心十分清楚的告诉自己写个脚本,写个脚本就可以快速恢复,但是对于自己不清楚的文件内容,自己毫无头绪 (一定要把里面悬空文件多复制几份,然后对复制后的文件进行修改,保证损失环境的 无变化)</h4></li><li><h3 id="一点点寻找"><a href="#一点点寻找" class="headerlink" title="一点点寻找"></a>一点点寻找</h3><h4 id="文件二进制可以识别出大部分无意义的文件,于是我拿出了做搁置很久的-010-Editor-查看几个后发现太过缓慢;灵光一现然后打开-VS-Code-下载插件-查看-16进制。"><a href="#文件二进制可以识别出大部分无意义的文件,于是我拿出了做搁置很久的-010-Editor-查看几个后发现太过缓慢;灵光一现然后打开-VS-Code-下载插件-查看-16进制。" class="headerlink" title="文件二进制可以识别出大部分无意义的文件,于是我拿出了做搁置很久的 010 Editor 查看几个后发现太过缓慢;灵光一现然后打开 VS Code 下载插件 查看 16进制。"></a>文件二进制可以识别出大部分无意义的文件,于是我拿出了做搁置很久的 <strong>010 Editor</strong> 查看几个后发现太过缓慢;灵光一现然后打开 VS Code 下载插件 查看 16进制。</h4><h4 id="这样一个个文件看过去,估计自己看完眼睛也已经废掉了-,于是开始总结经验"><a href="#这样一个个文件看过去,估计自己看完眼睛也已经废掉了-,于是开始总结经验" class="headerlink" title="这样一个个文件看过去,估计自己看完眼睛也已经废掉了 ,于是开始总结经验"></a>这样一个个文件看过去,估计自己看完眼睛也已经废掉了 ,于是开始总结经验</h4></li><li><h3 id="发现经验"><a href="#发现经验" class="headerlink" title="发现经验"></a>发现经验</h3><h4 id="经过看了几百个文件后,了解到-other-文件夹下文件类型大概分为-Png-89504E47-、Jpg-FFD8FF-、txt-txt文件十六进制没有固定格式-、Gif-47494638-、zip-504B0304-…"><a href="#经过看了几百个文件后,了解到-other-文件夹下文件类型大概分为-Png-89504E47-、Jpg-FFD8FF-、txt-txt文件十六进制没有固定格式-、Gif-47494638-、zip-504B0304-…" class="headerlink" title="经过看了几百个文件后,了解到 other 文件夹下文件类型大概分为 Png (89504E47)、Jpg (FFD8FF)、txt (txt文件十六进制没有固定格式)、Gif (47494638)、zip (504B0304) …"></a>经过看了几百个文件后,了解到 other 文件夹下文件类型大概分为 Png (89504E47)、Jpg (FFD8FF)、txt (txt文件十六进制没有固定格式)、Gif (47494638)、zip (504B0304) …</h4><h4 id="好吧略有点多,试了试写以十六进制脚本查看用来区分,-自己太菜,写出来对于各种txt文件-md文件-无法有效区分"><a href="#好吧略有点多,试了试写以十六进制脚本查看用来区分,-自己太菜,写出来对于各种txt文件-md文件-无法有效区分" class="headerlink" title="好吧略有点多,试了试写以十六进制脚本查看用来区分,(自己太菜,写出来对于各种txt文件,md文件, 无法有效区分)"></a>好吧略有点多,试了试写以十六进制脚本查看用来区分,(自己太菜,写出来对于各种txt文件,md文件, 无法有效区分)</h4></li><li><h3 id="改变思路"><a href="#改变思路" class="headerlink" title="改变思路"></a>改变思路</h3><h4 id="通过直接-二进制读取-文件将文件名-与-二进制读取每个文件前几个-二进制数据-发现各种文件后缀的二进制文件,鉴于里面笔记较多,决定半手动处理-庆幸自己学习的时候有良好的代码习惯,代码文件读取出来前几个字节文件基本相同,节省大量区分时间"><a href="#通过直接-二进制读取-文件将文件名-与-二进制读取每个文件前几个-二进制数据-发现各种文件后缀的二进制文件,鉴于里面笔记较多,决定半手动处理-庆幸自己学习的时候有良好的代码习惯,代码文件读取出来前几个字节文件基本相同,节省大量区分时间" class="headerlink" title="通过直接 二进制读取 文件将文件名 与 二进制读取每个文件前几个 二进制数据 发现各种文件后缀的二进制文件,鉴于里面笔记较多,决定半手动处理 (庆幸自己学习的时候有良好的代码习惯,代码文件读取出来前几个字节文件基本相同,节省大量区分时间)"></a>通过直接 二进制读取 文件将文件名 与 二进制读取每个文件前几个 二进制数据 发现各种文件后缀的二进制文件,鉴于里面笔记较多,决定半手动处理 (庆幸自己学习的时候有良好的代码习惯,代码文件读取出来前几个字节文件基本相同,节省大量区分时间)</h4> <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> os</span><br><span class="line"><span class="keyword">import</span> shutil</span><br><span class="line"> </span><br><span class="line"> </span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">check_file</span><span class="params">(file_path)</span>:</span></span><br><span class="line"> file_names = os.listdir(file_path)</span><br><span class="line"> <span class="keyword">for</span> file_name <span class="keyword">in</span> file_names:</span><br><span class="line"> <span class="keyword">with</span> open(os.path.join(file_path, file_name), <span class="string">"rb"</span>) <span class="keyword">as</span> f:</span><br><span class="line"> date = f.readline(<span class="number">10</span>)</span><br><span class="line"> f.close()</span><br><span class="line"> <span class="comment"># if str(date) == str(b' b'\xff\xd8\xff\xe0\x00\x10JFIF''):</span></span><br><span class="line"> <span class="comment"># os.remove(os.path.join(file_path, file_name))</span></span><br><span class="line"> <span class="comment"># os.rename(os.path.join(file_path, file_name), os.path.join(file_path, file_name) + ".jpg")</span></span><br><span class="line"> <span class="comment"># shutil.move(os.path.join(file_path, file_name) + ".jpg", "E://save/png") </span></span><br><span class="line"></span><br><span class="line"> print(file_name, date)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">check_file(<span class="string">"e:\\新建文件夹\\other"</span>)</span><br></pre></td></tr></table></figure></li><li><h3 id="分类,修复"><a href="#分类,修复" class="headerlink" title="分类,修复"></a>分类,修复</h3><h4 id="通过以上代码将-Png、Jpg、Gif-文件统统删除,直接少了一半无用文件,舒爽"><a href="#通过以上代码将-Png、Jpg、Gif-文件统统删除,直接少了一半无用文件,舒爽" class="headerlink" title="通过以上代码将 Png、Jpg、Gif 文件统统删除,直接少了一半无用文件,舒爽"></a>通过以上代码将 Png、Jpg、Gif 文件统统删除,直接少了一半无用文件,舒爽</h4><h4 id="通过对-exe-文件重命名-并且-移动到准备好的分类的各种文件下,方便统一整理"><a href="#通过对-exe-文件重命名-并且-移动到准备好的分类的各种文件下,方便统一整理" class="headerlink" title="通过对 exe 文件重命名, 并且 移动到准备好的分类的各种文件下,方便统一整理"></a>通过对 exe 文件重命名, 并且 移动到准备好的分类的各种文件下,方便统一整理</h4><h4 id="对其他不同种类文件添加后缀,移动到文件夹"><a href="#对其他不同种类文件添加后缀,移动到文件夹" class="headerlink" title="对其他不同种类文件添加后缀,移动到文件夹"></a>对其他不同种类文件添加后缀,移动到文件夹</h4><h4 id="对于剩余所有文件放入一个文件夹"><a href="#对于剩余所有文件放入一个文件夹" class="headerlink" title="对于剩余所有文件放入一个文件夹"></a>对于剩余所有文件放入一个文件夹</h4><h4 id="删除无用的-图片文件、压缩文件、exe-文件等"><a href="#删除无用的-图片文件、压缩文件、exe-文件等" class="headerlink" title="删除无用的 图片文件、压缩文件、exe 文件等"></a>删除无用的 图片文件、压缩文件、exe 文件等</h4></li><li><h3 id="最后剩余的有用的"><a href="#最后剩余的有用的" class="headerlink" title="最后剩余的有用的"></a>最后剩余的有用的</h3><h4 id="根据自己定义的写代码的规范,将不同学习阶段的代码区分,按顺序统一命名,将笔记提取出来,按内容重新整理"><a href="#根据自己定义的写代码的规范,将不同学习阶段的代码区分,按顺序统一命名,将笔记提取出来,按内容重新整理" class="headerlink" title="根据自己定义的写代码的规范,将不同学习阶段的代码区分,按顺序统一命名,将笔记提取出来,按内容重新整理"></a>根据自己定义的写代码的规范,将不同学习阶段的代码区分,按顺序统一命名,将笔记提取出来,按内容重新整理</h4></li><li><h3 id="搞完收工"><a href="#搞完收工" class="headerlink" title="搞完收工"></a>搞完收工</h3><h4 id="真的心塞,以后对于网上的代码一定要先搞懂作用再使用-培养好自己良好的写代码规范-合理使用-git"><a href="#真的心塞,以后对于网上的代码一定要先搞懂作用再使用-培养好自己良好的写代码规范-合理使用-git" class="headerlink" title="真的心塞,以后对于网上的代码一定要先搞懂作用再使用; 培养好自己良好的写代码规范;合理使用 git"></a>真的心塞,以后对于网上的代码一定要先搞懂作用再使用; 培养好自己良好的写代码规范;合理使用 git</h4><h4 id="最后希望大家都不要掉到这个坑里,一定要记得修改任何东西之前做好保存,留好备份"><a href="#最后希望大家都不要掉到这个坑里,一定要记得修改任何东西之前做好保存,留好备份" class="headerlink" title="最后希望大家都不要掉到这个坑里,一定要记得修改任何东西之前做好保存,留好备份"></a>最后希望大家都不要掉到这个坑里,一定要记得修改任何东西之前做好保存,留好备份</h4></li></ol>]]></content>
<tags>
<tag> Git </tag>
</tags>
</entry>
<entry>
<title>Getting-start-with-Git</title>
<link href="/2020/08/22/getting-start-with-git/"/>
<url>/2020/08/22/getting-start-with-git/</url>
<content type="html"><![CDATA[<h1 id="Git-入门"><a href="#Git-入门" class="headerlink" title="Git 入门"></a>Git 入门</h1><h3 id="在-Git-中设置用户名-Windows-系统"><a href="#在-Git-中设置用户名-Windows-系统" class="headerlink" title="在 Git 中设置用户名 (Windows 系统)"></a>在 Git 中设置用户名 (Windows 系统)</h3><p><code>Git 使用用户名把提交与身份相关联,Git 用户名 不等于 GitHub 用户名</code></p><h3 id="git-config"><a href="#git-config" class="headerlink" title="git config"></a><code>git config</code></h3><ul><li><p>参数</p><ul><li>默认对当前目录下仓库进行操作, 配置保存在 “<strong><em>此目录路径/目录名/.git/config</em></strong>“</li><li>–global : 全局参数, 使用这个参数配置此计算机所有Git仓库的内容,也可以在仓库单独指定用户名和密码, 配置保存在 “<strong><em>C:\Users\用户名.gitconfig</em></strong>“</li><li>–system : 必需在命令前使用 <strong>sudo</strong>, 针对此系统下所有用户</li><li>–list : 显示当前 Git 配置</li><li>-e [–global] : 编辑当前仓库下 (–global 全局) Git 的配置文件,当前目录下没有配置文件,返回 <code>fatal: not in a git directory</code> 错误 </li></ul></li><li><p>配置优先级</p><ul><li>当前仓库配置 > 本地全局仓库配置 > 所有用户仓库配置</li></ul></li></ul><h3 id="为计算机上的每个仓库设置-Git-用户名"><a href="#为计算机上的每个仓库设置-Git-用户名" class="headerlink" title="为计算机上的每个仓库设置 Git 用户名"></a>为计算机上的每个仓库设置 Git 用户名</h3><ol><li>打开 Git Bash</li><li>设置 Git 用户名 和 Email (Git Email 与 GitHub Email 一致时才被认为是同一用户,否则为另一用户创建了仓库进行操作) <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">$ git config --global user.name "Git 用户名"</span><br><span class="line">$ git config --global user.email "Git Email"</span><br></pre></td></tr></table></figure></li><li>确认/查看自己的 Git 用户名 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">$ git config --global user.name</span><br><span class="line"> demon_liu</span><br></pre></td></tr></table></figure></li></ol><h3 id="为一个仓库设置-Git-用户名"><a href="#为一个仓库设置-Git-用户名" class="headerlink" title="为一个仓库设置 Git 用户名"></a>为一个仓库设置 Git 用户名</h3><ol><li>打开 Git Bash</li><li>将当前工作目录更改为您想要在其中配置与 Git 提交关联的名称的本地仓库.</li><li>设置 Git 用户名 和 Email <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">$ git config user.name "Git 用户名"</span><br><span class="line">$ git config user.email "Git Email"</span><br></pre></td></tr></table></figure></li><li>确认/查看此仓库自己的 Git 用户名 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">$ git config user.name</span><br><span class="line"> demon_liu</span><br></pre></td></tr></table></figure></li></ol><h3 id="连接-GitHub"><a href="#连接-GitHub" class="headerlink" title="连接 GitHub"></a>连接 GitHub</h3><h3 id="关联-ssh-key-通过-SSH-加密传输"><a href="#关联-ssh-key-通过-SSH-加密传输" class="headerlink" title="关联 ssh-key 通过 SSH 加密传输"></a>关联 ssh-key 通过 SSH 加密传输</h3><ol><li>打开 Git Bash , 以下命令创建 SSH Key, <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ ssh-keygen -t rsa -C "Git Email"</span><br></pre></td></tr></table></figure></li><li>C://User/用户名/.ssh 下生成两个文件<ul><li>id_rsa : 私钥,保密</li><li>id_rsa.pub : 公钥</li></ul></li><li>在 GitHub –> Setting –> SSH and GPG Keys 页面 选择 “New SSH key”<br> 填上你的 标题 在 Key 文本框粘贴 公钥 (id_rsa.pub) 的内容<br> <img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/SSH.png" alt="SSH"><br> <img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/Add_ssh.png" alt="ADD_SSH"></li><li>测试 ssh key 是否成功。 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">$ ssh -T [email protected]</span><br><span class="line"> Hi 用户名! You've successfully authenticated, but GitHub does not provide shell access.</span><br></pre></td></tr></table></figure></li></ol><h3 id="创建-Git-代码库"><a href="#创建-Git-代码库" class="headerlink" title="创建 Git 代码库"></a>创建 Git 代码库</h3><h4 id="1-在当前目录新建一个-Git-代码库-并初始化"><a href="#1-在当前目录新建一个-Git-代码库-并初始化" class="headerlink" title="1. 在当前目录新建一个 Git 代码库, 并初始化"></a>1. 在当前目录新建一个 Git 代码库, 并初始化</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git init</span><br></pre></td></tr></table></figure><h4 id="2-新建一个目录作为-Git-代码库-,并初始化"><a href="#2-新建一个目录作为-Git-代码库-,并初始化" class="headerlink" title="2. 新建一个目录作为 Git 代码库 ,并初始化"></a>2. 新建一个目录作为 Git 代码库 ,并初始化</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git init [目录名]</span><br></pre></td></tr></table></figure><h3 id="将本地分支的更新,推送到远程主机-git-push"><a href="#将本地分支的更新,推送到远程主机-git-push" class="headerlink" title="将本地分支的更新,推送到远程主机 [git push]"></a>将本地分支的更新,推送到远程主机 [git push]</h3><ul><li>参数 <code>git push <远程主机名> <本地分支名>:<远程分支名></code><ul><li>远程命令</li><li>分支名称</li><li>新分支名称</li></ul></li><li>本地的master分支推送到origin主机的master分支。如果master不存在,则会被新建。//原文出自【易百教程】,商业转载请联系作者获得授权,非商业请保留原文链接:<a href="https://www.yiibai.com/git/git_push.html" target="_blank" rel="noopener">https://www.yiibai.com/git/git_push.html</a> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git push origin master</span><br></pre></td></tr></table></figure></li><li>删除指定的远程分支 (省略本地分支名) <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$ git push origin :master</span><br><span class="line"><==></span><br><span class="line">$ git push orgin --delete master</span><br></pre></td></tr></table></figure></li></ul><h3 id="克隆仓库-git-clone"><a href="#克隆仓库-git-clone" class="headerlink" title="克隆仓库 git clone"></a>克隆仓库 <code>git clone</code></h3><ul><li>操作命令 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git clone [仓库 SSH / HTTPs]</span><br></pre></td></tr></table></figure></li><li>克隆过程<ol><li>在 此路径下 创建 被克隆文件夹名的文件夹</li><li>初始化此文件夹为 Git 仓库</li><li>创建名为 origin 的远程仓库,指向用于克隆的 URL</li><li>将所有的仓库文件和提交下载到那里</li><li>检出默认分支(通常称为 master)</li></ol></li></ul><h3 id="获取远程仓库更改-从另一个存储库下载对象和引用"><a href="#获取远程仓库更改-从另一个存储库下载对象和引用" class="headerlink" title="获取远程仓库更改,从另一个存储库下载对象和引用"></a>获取远程仓库更改,从另一个存储库下载对象和引用</h3><ul><li><code>git fetch</code> 相当于是从远程获取最新版本到本地,不会自动合并<ul><li>从远程的origin的master主分支下载最新的版本到origin/master分支上 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git fetch origin master</span><br></pre></td></tr></table></figure></li><li>进行合并 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git merge origin/master</span><br></pre></td></tr></table></figure></li></ul></li><li>更新本地工作<ul><li>两步更新<ul><li>获取远程仓库更新 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ gitfetch <远端名称></span><br></pre></td></tr></table></figure></li><li>将在线更新与您的本地工作进行合并 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git merge <远端名称>/<分支名称></span><br></pre></td></tr></table></figure></li></ul></li><li>一步: 获取在线更新并将其与您的本地工作进行合并 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git pull <远端名称> <分支名称></span><br></pre></td></tr></table></figure></li></ul></li><li><code>git pull [options] [<repository> [<refspec>…]]</code> 取回远程主机某个分支的更新,再与本地的指定分支合并<ul><li><code>git pull <远程主机名> <远程分支名>:<本地分支名></code><ul><li>取回origin主机的next分支,与本地的master分支合并 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git pull origin next:master</span><br></pre></td></tr></table></figure></li></ul></li></ul></li></ul><h3 id="分支"><a href="#分支" class="headerlink" title="分支"></a>分支</h3><h4 id="1-git-branch"><a href="#1-git-branch" class="headerlink" title="1. git branch"></a>1. <code>git branch</code></h4><ul><li>列出所有远程分支 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git branch -r</span><br></pre></td></tr></table></figure></li><li>列出所有本地分支 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git branch</span><br></pre></td></tr></table></figure></li><li>列出所有本地分支和远程分支 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git branch -a</span><br></pre></td></tr></table></figure></li><li>新建一个分支 [-b 新建的同时切换到新建分支] <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git branch [-b] <分支名称></span><br></pre></td></tr></table></figure></li><li>删除分支 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git branch -d <分支名称></span><br></pre></td></tr></table></figure></li><li>删除远程分支 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">$ git push origin --delete <分支名称></span><br><span class="line">$ git branch -dr <remote/branch></span><br></pre></td></tr></table></figure></li></ul><h4 id="2-切换分支-git-checkout"><a href="#2-切换分支-git-checkout" class="headerlink" title="2. 切换分支 git checkout"></a>2. 切换分支 <code>git checkout</code></h4><ul><li>切换到上一个分支 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git checkout -</span><br></pre></td></tr></table></figure></li><li>切换到指定分支,并更新工作区 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git checkout <分支名称></span><br></pre></td></tr></table></figure></li></ul><h3 id="Git-的-基本操作-增、删、改、提、查"><a href="#Git-的-基本操作-增、删、改、提、查" class="headerlink" title="Git 的 基本操作 (增、删、改、提、查)"></a>Git 的 基本操作 (增、删、改、提、查)</h3><h4 id="1-增"><a href="#1-增" class="headerlink" title="1. 增"></a>1. 增</h4><ul><li><p><code>git add <path></code> 将文件内容添加到索引(将修改添加到暂存区)</p><ul><li>添加文件/文件目录到指定暂存区 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git add [file]/[dir]</span><br></pre></td></tr></table></figure></li><li>添加当前目录下所有文件到暂存区 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git add .</span><br></pre></td></tr></table></figure></li></ul></li></ul><h4 id="2-删"><a href="#2-删" class="headerlink" title="2. 删"></a>2. 删</h4><ul><li><code>git rm <path></code> 从工作区和索引中删除文件,git rm 来删除文件,同时还会将这个删除操作记录下来;而使用 rm 来删除文件,仅仅是删除了物理文件,没有将其从 git 的记录中剔除//原文出自【易百教程】,商业转载请联系作者获得授权,非商业请保留原文链接:<a href="https://www.yiibai.com/git/git_rm.html" target="_blank" rel="noopener">https://www.yiibai.com/git/git_rm.html</a><ul><li>删除文件 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git rm [filename]</span><br></pre></td></tr></table></figure></li><li>删除文件夹 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git rm -r [dirname]</span><br></pre></td></tr></table></figure></li></ul></li></ul><h4 id="3-改"><a href="#3-改" class="headerlink" title="3. 改"></a>3. 改</h4><ul><li><code>git mv</code> 移动或重命名文件,目录或符号链接<ul><li>重命名文件 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git mv [sourcefile] [newfile]</span><br></pre></td></tr></table></figure></li><li>移动文件到新目录 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git mv [sourcefile] [newdir]</span><br></pre></td></tr></table></figure></li></ul></li></ul><h4 id="4-提交代码"><a href="#4-提交代码" class="headerlink" title="4. 提交代码"></a>4. 提交代码</h4><ul><li><code>git commint</code> 将更改记录(提交)到存储库. 将索引的当前内容与描述更改的用户和日志消息一起存储在新的提交中.<ul><li>将文件暂存到仓库 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git commit -m [message]</span><br></pre></td></tr></table></figure></li><li>提交暂存文件到仓库 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git commit [filename] [] -m [message]</span><br></pre></td></tr></table></figure></li><li>提交工作区所有改变到仓库 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git commit -a</span><br></pre></td></tr></table></figure></li><li>提交时显示改变 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git commit -f</span><br></pre></td></tr></table></figure></li></ul></li></ul><h4 id="5-查"><a href="#5-查" class="headerlink" title="5. 查"></a>5. 查</h4><ul><li>查看所有有改变的文件 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git status</span><br></pre></td></tr></table></figure></li><li>显示所有分支的历史版本 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git log</span><br></pre></td></tr></table></figure></li><li>显示commit历史,以及每次commit发生变更的文件 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git log --stat</span><br></pre></td></tr></table></figure></li><li>显示暂存区和工作区的差异 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ git diff</span><br></pre></td></tr></table></figure></li></ul>]]></content>
<tags>
<tag> Git </tag>
</tags>
</entry>
<entry>
<title>pyechart_local_service</title>
<link href="/2020/08/22/pyechart-local-service/"/>
<url>/2020/08/22/pyechart-local-service/</url>
<content type="html"><![CDATA[<h1 id="pyecharts-assets"><a href="#pyecharts-assets" class="headerlink" title="pyecharts_assets"></a>pyecharts_assets</h1><p><a href="https://assets.pyecharts.org/" target="_blank" rel="noopener">全部来自于官方文档,主要用于方便自己查找</a></p><h2 id="解决-pyecharts-在生成-Html-时由于网络问题无法在网页显示-生成图片"><a href="#解决-pyecharts-在生成-Html-时由于网络问题无法在网页显示-生成图片" class="headerlink" title="解决 pyecharts 在生成 Html 时由于网络问题无法在网页显示 生成图片"></a>解决 pyecharts 在生成 Html 时由于网络问题无法在网页显示 生成图片</h2><h3 id="pyecharts-assets-提供了-pyecharts-的静态资源文件。可通过-localhost-server-或者-notebook-server-启动本地服务。"><a href="#pyecharts-assets-提供了-pyecharts-的静态资源文件。可通过-localhost-server-或者-notebook-server-启动本地服务。" class="headerlink" title="pyecharts-assets 提供了 pyecharts 的静态资源文件。可通过 localhost-server 或者 notebook-server 启动本地服务。"></a>pyecharts-assets 提供了 pyecharts 的静态资源文件。可通过 localhost-server 或者 notebook-server 启动本地服务。</h3><ul><li>Localhost-Server</li></ul><ol><li><p>首先将项目下载到本地</p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"># 通过 git clone</span><br><span class="line">$ git clone https://github.com/pyecharts/pyecharts-assets.git</span><br><span class="line"> </span><br><span class="line"># 或者直接下载压缩包</span><br><span class="line">$ wget https://github.com/pyecharts/pyecharts-assets/archive/master.zip</span><br></pre></td></tr></table></figure></li><li><p>启动服务器</p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">$ cd pyecharts-assets</span><br><span class="line">$ python -m http.server</span><br></pre></td></tr></table></figure></li><li><p>设置 host</p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"># 只需要在顶部声明 CurrentConfig.ONLINE_HOST 即可</span><br><span class="line">from pyecharts.globals import CurrentConfig</span><br><span class="line"> </span><br><span class="line">CurrentConfig.ONLINE_HOST = "http://127.0.0.1:8000/assets/"</span><br><span class="line"> </span><br><span class="line"># 接下来所有图形的静态资源文件都会来自刚启动的服务器</span><br><span class="line">from pyecharts.charts import Bar</span><br><span class="line">bar = Bar()</span><br></pre></td></tr></table></figure></li></ol><ul><li>Notebook-Server</li></ul><ol><li><p>安装拓展插件</p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">$ cd pyecharts-assets</span><br><span class="line"># 安装并激活插件</span><br><span class="line">$ jupyter nbextension install assets</span><br><span class="line">$ jupyter nbextension enable assets/main</span><br></pre></td></tr></table></figure></li><li><p>设置 host</p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"># 只需要在顶部声明 CurrentConfig.ONLINE_HOST 即可</span><br><span class="line">from pyecharts.globals import CurrentConfig, OnlineHostType</span><br><span class="line"></span><br><span class="line"># OnlineHostType.NOTEBOOK_HOST 默认值为 http://localhost:8888/nbextensions/assets/</span><br><span class="line">CurrentConfig.ONLINE_HOST = OnlineHostType.NOTEBOOK_HOST</span><br><span class="line"></span><br><span class="line"># 接下来所有图形的静态资源文件都会来自刚启动的服务器</span><br><span class="line">from pyecharts.charts import Bar</span><br><span class="line">bar = Bar()</span><br></pre></td></tr></table></figure></li></ol><p><strong>未解决问题 : 需要注意的是对于词云图 任然存在显示白屏问题*</strong></p>]]></content>
<tags>
<tag> Pyecharts </tag>
</tags>
</entry>
<entry>
<title>Chrome-Network</title>
<link href="/2020/08/17/chrome-network/"/>
<url>/2020/08/17/chrome-network/</url>
<content type="html"><![CDATA[<h1 id="Google-DevTools-开发者工具"><a href="#Google-DevTools-开发者工具" class="headerlink" title="Google DevTools 开发者工具"></a>Google DevTools 开发者工具</h1><h2 id="打开网络面板"><a href="#打开网络面板" class="headerlink" title="打开网络面板"></a>打开网络面板</h2><p><img src="!%5B%5D(https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/all.png)" alt></p><h3 id="1-打开-DevTools-控制台面板"><a href="#1-打开-DevTools-控制台面板" class="headerlink" title="1. 打开 DevTools 控制台面板"></a>1. 打开 DevTools 控制台面板</h3><h3 id="2-单击网络选项卡。将打开“网络”面板。"><a href="#2-单击网络选项卡。将打开“网络”面板。" class="headerlink" title="2. 单击网络选项卡。将打开“网络”面板。"></a>2. 单击网络选项卡。将打开“网络”面板。</h3><h3 id="3-“网络”面板为空。这是因为DevTools仅在网络活动打开时记录日志,并且自您打开DevTools以来未发生任何网络活动。"><a href="#3-“网络”面板为空。这是因为DevTools仅在网络活动打开时记录日志,并且自您打开DevTools以来未发生任何网络活动。" class="headerlink" title="3. “网络”面板为空。这是因为DevTools仅在网络活动打开时记录日志,并且自您打开DevTools以来未发生任何网络活动。"></a>3. “网络”面板为空。这是因为DevTools仅在网络活动打开时记录日志,并且自您打开DevTools以来未发生任何网络活动。</h3><h3 id="4-刷新页面"><a href="#4-刷新页面" class="headerlink" title="4. 刷新页面"></a>4. 刷新页面</h3><h2 id="记录网络活动"><a href="#记录网络活动" class="headerlink" title="记录网络活动"></a>记录网络活动</h2><h3 id="1-重新加载页面-“Network”-面板将所有网络活动记录在“-网络日志”中"><a href="#1-重新加载页面-“Network”-面板将所有网络活动记录在“-网络日志”中" class="headerlink" title="1. 重新加载页面, “Network” 面板将所有网络活动记录在“ 网络日志”中"></a>1. 重新加载页面, “Network” 面板将所有网络活动记录在“ 网络日志”中</h3><ul><li><p>网络日志 (Web Log) 的每一行代表一个资源。默认情况下,资源按时间顺序列出。最重要的资源通常是主要的HTML文档。最底层的资源是最后请求的资源。(只要打开DevTools,它将在网络日志中记录网络活动。)<br><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/network.png" alt="network_01"><br><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/weblog.png" alt></p><ol><li>name: 提示通过将鼠标悬停在“名称”列中的单元格上,可以查看资源的完整URL。</li><li>Status : 状态 HTTP 响应代码 </li><li>Type : 资源类型</li><li>Initiator : 单击“Initiator”列中的链接,将您带到引起资源请求的源代码 (网址)。</li><li>Size : 请求大小</li><li>Time : 请求花了多长时间</li><li>Waterfall :请求不同阶段的图形表示。将鼠标悬停可以查看细目</li><li>Domain: 右键单击“网络日志”表的标题,然后选择“Domain”。现在显示每个资源的域。</li></ol></li><li><p>(Throttling) 模拟较慢的网络连接 (用于构建站点的计算机的网络连接可能比用户的移动设备的网络连接快。<br>通过限制页面,您可以更好地了解页面在移动设备上加载需要多长时间。)<br> <img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/Online.png" alt="Online"><br> <img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/Reload.png" alt="Reload"></p><ol><li>单击“节流”下拉菜单,默认情况下将其设置为“Online”</li><li>选择 “Slow 3G”</li><li>长按重新加载 (Reload),然后选择“清空缓存和硬重新加载 (Empty Cache And Hard Reload)”. (在重复访问时,浏览器通常会从其缓存中提供一些文件,从而加快了页面加载速度。空缓存和硬重载会强制浏览器访问所有资源的网络。当您想了解首次访问者如何体验页面加载时,这很有用。)</li><li>仅当打开DevTools时,“清空缓存和硬重装”工作流程才可用。</li></ol></li><li><p>检查资源的详细信息 (单击资源以了解有关它的更多信息)<br> <img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/detail.png" alt="detail"></p><ol><li>点击getstarted.html。显示“Headers”选项卡。使用此选项卡检查HTTP标头。</li><li>单击预览 (Preview) 选项卡。显示了HTML的基本呈现。 (当API以HTML返回错误代码并且比HTML源代码更容易阅读呈现的HTML或检查图像时,此选项卡非常有用)</li><li>单击响应 (Response) 选项卡。显示了HTML源代码。</li><li>缩小文件后,单击“响应”选项卡底部的“格式 (Format)”按钮会重新格式化文件的内容,以提高可读性。</li><li>单击时间 (Timing) 选项卡。显示了此资源的网络活动的细分。</li><li>单击关闭 (x) 以再次查看网络日志。</li></ol></li><li><p>(Search)搜索网络标题和响应 (当您需要在HTTP标头和所有资源的响应中搜索某个字符串或正则表达式时,请使用“搜索 (Search) ”窗格。)</p><ol><li>点击搜索。搜索窗格将在网络日志的左侧/上侧打开。</li><li>单击结果以查看它。如果在标题 (headers) 中找到查询,则“标题”选项卡将打开。如果在内容 (content) 中找到查询,则“响应”选项卡将打开。</li></ol></li><li><p>筛选 (Filter) 资源 (沙漏按钮, DevTools提供了许多工作流,用于过滤与手头任务无关的资源。按字符串,正则表达式或属性过滤)</p><ol><li>在“过滤器”文本框中键入png。仅显示包含文本png的文件。在这种情况下,唯一与过滤器匹配的文件是PNG图像。</li><li>键入/.*.[cj]s+$/。DevTools会过滤出任何文件名不以j或c结尾且后跟1个或多个s字符的资源。</li><li>输入-main.css。DevTools过滤掉main.css。如果任何其他文件与该模式匹配,它们也将被过滤掉。</li><li>在“过滤器”文本框中键入domain:raw.githubusercontent.com。DevTools过滤出任何URL与此域不匹配的资源。</li><li>按资源类型过滤<ul><li>单击CSS。其他所有文件类型均被过滤掉。</li><li>要同时查看脚本,请按住Control或Command(Mac),然后单击JS。</li><li>单击全部删除过滤器,然后再次查看所有资源</li></ul></li></ol></li><li><p>(Clear) 清空加载信息,刷新后重新加载</p></li><li><p>(Stop recording network log) 暂时停止更新网络日志</p></li><li><p>(Disable cache) : 禁用缓存</p></li><li><p>(Preserve log) : 保留日志</p></li><li><p>(Network Setting)</p><ol><li>Show overview : 显示总览,以时间轴来显示加载过程</li><li>Capture screenshots : 截图<ul><li>截图 (屏幕快照使您可以看到页面在加载过程中的外观。)<ol><li>点击 Capture Screenshots (屏幕快照)</li><li>通过“清空缓存和硬重装”工作流程再次重装页面。如果您需要有关如何执行此操作的提示,请参阅模拟较慢的连接。屏幕截图窗格提供了缩略图的缩略图,该缩略图显示了页面在加载过程中各个点的外观。</li><li>单击第一个缩略图。DevTools会向您显示当时正在发生的网络活动。</li><li>再次单击“ Capture Screenshots”以关闭“屏幕截图”窗格。</li><li>重新加载页面</li></ol></li></ul></li><li>Use large request rows : 使用大图来显示 request 的 Web Log</li><li>Group by frame : 按帧分组</li></ol></li><li><p>阻止请求 (页面的某些资源不可用时,页面的外观和行为如何?它会完全失败,还是仍然有些功能?使用阻止请求以判断)<br> <img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/Command.png" alt="Command_menu"><br> <img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/blocking.png" alt="blocking"></p><ol><li>按Control + Shift + P或Command + Shift + P(Mac)打开“命令 (Command Menu)”菜单。</li><li>键入阻止 (>block),选择显示请求阻止 (Show Request blocking),然后按Enter</li><li>点击 Add Pattern</li><li>选择类 main.css</li><li>点击 Add</li><li>重新加载页面。正如预期的那样,页面的样式有些混乱,因为其主要样式表已被阻止。请注意网络日志中的main.css行。红色文本表示资源已被阻止。</li><li>取消选中启用请求阻止复选框</li></ol></li></ul>]]></content>
<tags>
<tag> Chrome DevTools </tag>
</tags>
</entry>
<entry>
<title>Chrome-Elements</title>
<link href="/2020/08/15/chrome-elements/"/>
<url>/2020/08/15/chrome-elements/</url>
<content type="html"><![CDATA[<h1 id="Google-DevTools-开发者工具"><a href="#Google-DevTools-开发者工具" class="headerlink" title="Google DevTools 开发者工具"></a>Google DevTools 开发者工具</h1><h2 id="打开-Chrome-开发者工具"><a href="#打开-Chrome-开发者工具" class="headerlink" title="打开 Chrome 开发者工具"></a>打开 Chrome 开发者工具</h2><h3 id="1-在-Chrome-菜单中选择-更多工具-gt-开发者工具"><a href="#1-在-Chrome-菜单中选择-更多工具-gt-开发者工具" class="headerlink" title="1. 在 Chrome 菜单中选择 更多工具->开发者工具"></a>1. 在 Chrome 菜单中选择 <strong>更多工具->开发者工具</strong></h3><h3 id="2-在页面元素上右键点击,选择检查-Windows-下快捷键-F12"><a href="#2-在页面元素上右键点击,选择检查-Windows-下快捷键-F12" class="headerlink" title="2. 在页面元素上右键点击,选择检查 (Windows 下快捷键 F12)"></a>2. 在页面元素上右键点击,选择<strong>检查</strong> (Windows 下快捷键 F12)</h3><h3 id="3-使用快捷键-Ctrl-Shift-I-Windows-或-Cmd-Opt-I-Mac"><a href="#3-使用快捷键-Ctrl-Shift-I-Windows-或-Cmd-Opt-I-Mac" class="headerlink" title="3. 使用快捷键 Ctrl + Shift + I(Windows) 或 Cmd + Opt + I (Mac)"></a>3. 使用快捷键 <strong>Ctrl + Shift + I</strong>(Windows) 或 <strong>Cmd + Opt + I</strong> (Mac)</h3><h3 id="1-元素面板-Elements"><a href="#1-元素面板-Elements" class="headerlink" title="1. 元素面板 (Elements)"></a>1. 元素面板 (Elements)</h3><ul><li><p>使用元素面板自由操作 <strong>DOM</strong> 和 <strong>CSS</strong> 来迭代布局和设计页面</p><ol><li><p>检查和调整页面</p><ul><li>摘要 (TL;DR)<ol><li>在 <strong><em>Elements 面板</em></strong>中检查和实时编辑 <strong>DOM 树中</strong>的任何元素<ul><li>双击选定元素即可对 DOM 节点实时编辑更改</li></ul></li><li>在 <strong><em>Styles 窗格</em></strong>中查看和更改应用到任何选定元素的 CSS 规则<ul><li>在 Styles 窗格中实时编辑样式属性名称和值。所有样式均可修改,除了灰色部分(与 User Agent 样式表一样)。</li><li>要编辑名称或值,请点击它,进行更改,然后按 Tab 或 Enter 保存更改。</li></ul></li><li>在 <strong><em>Computed 窗格</em></strong>中查看和修改选定元素的框模型<ul><li>Computed 窗格检查和编辑当前元素的框模型参数。 框模型中的所有值均可修改,只需点击它们即可。</li><li>同轴矩形包含当前元素 padding、border 和 margin 属性的 top、bottom、left、right 值。</li><li>对于位置为非静态的元素,还会显示 position 矩形,包含 top、right、bottom 和 left 属性的值。</li><li>对于 position: fixed 和 position: absolute 元素,中心域包含选定元素实际的 offsetWidth × offsetHeight 像素尺寸。所有值都可以通过双击修改,就像 Styles 窗格中的属性值一样。 不过,无法保证这些更改能够生效,因为这要取决于具体的元素定位详情。</li></ul></li><li>在 <strong><em>Sources 面板</em></strong>中查看在本地对页面所做的修改<ul><li>粉色背景的线表示移除,绿色背景的线表示添加。</li><li>查看对页面所做的实时编辑的历史记录<ol><li>在 Styles 窗格中,点击您修改的文件。DevTools 会将您带到 Sources 面板。</li><li>右键点击文件</li><li>选择 Local modifications</li></ol></li><li>检查所做的修改<ol><li>展开顶级文件名查看做出修改的时间 做出修改的时间。</li><li>展开第二级项目查看修改相应的不同(前和后)。</li></ol></li></ul></li></ol></li><li>撤销更改<ul><li>未设置永久制作 : 每次重新加载页面时,所有实时编辑都会丢失</li><li>设置永久制作,撤销更改 : <ol><li>使用 Ctrl+Z (Windows) 或 Cmd+Z (Mac) 通过 Elements 面板快速撤消对 DOM 或样式所做的细微更改。</li><li>撤消对文件所做的所有本地修改,请打开 Sources 面板,然后选择文件名旁的 revert。</li></ol></li></ul></li></ul></li><li><p>使用 Styles 可以修改与元素关联的 CSS</p><ul><li>摘要 (TL;DR)<ul><li>利用 Styles 窗格,可以在本地以尽可能多的方法更改 CSS,包括修改现有样式、添加新样式,以及为样式添加规则。</li><li>如果希望样式可以保持(不会在重新加载后消失),则需要将其保存到开发工作区中</li></ul></li></ul><ol><li><p>检查应用到元素的样式: <code>选择一个元素以检查其样式。 Styles 窗格可以显示应用到选定元素的 CSS 规则,优先级从高到低:</code></p><ul><li><p>顶端为 element.style。这些样式要么直接使用样式属性(例如 </p><p style="color:green">)直接应用到元素,要么在 DevTools 中应用。</p></li><li><p>下方是与元素匹配的任何 CSS 规则。例如,在下面的屏幕截图中,选定元素从 tools.css 中定义的规则接收 line-height:24px。</p></li><li><p>再下方是继承的样式,其中包括与选定元素的祖先实体匹配的任何可继承样式规则。 例如,在下面的屏幕截图中,选定元素从 user agent stylesheet 继承 display:list-item。</p></li><li><p>样式: 标签与其下方带编号的项目对应</p><p> <img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/style.png" alt="style"></p><ol><li>与元素匹配的选择器的关联样式。</li><li>User Agent 样式表清晰标记,并且在网页上经常被 CSS 替换。</li><li>已被级联规则替换的规则将显示为带删除线的文本。</li><li>继承的样式将在“Inherited from <NODE>”标头下显示为一组。点击标头中的 DOM 节点可以导航到其在 DOM 树视图中的位置。</NODE></li><li>灰色的条目不是已定义的规则,而是在运行时计算的规则。</li></ol></li></ul></li><li><p>检查受选择器影响的元素: <code>在 Styles 窗格中将鼠标悬停在 CSS 选择器上可以查看受该选择器影响的所有元素——(此功能仅突出显示视口中的元素;视口以外的其他元素也可能受选择器影响。)</code><br> <img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/mouse_stay.png" alt="mouse_stay"></p></li><li><p>添加、启用和停用 CSS 类</p><ul><li><p>点击 .cls 按钮可以查看与当前选定元素关联的所有 CSS 类。 从这里,您可以执行以下操作:</p><ol><li>启用或停用当前与元素关联的类</li><li>向元素添加新类。</li></ol><p><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/style_css.png" alt="style_css"></p></li></ul></li><li><p>编辑现有属性名称或值</p><ul><li>点击 CSS 属性名称或值可以对其进行编辑</li><li>在名称或值突出显示时,按 <strong>Tab</strong> 可以向<strong>前</strong>移动到下一个属性、名称或选择器。</li><li>按 <strong>Shift+Tab</strong> 可以向<strong>后</strong>移动。</li><li>编辑数字式 CSS 属性值时,可以使用下面的键盘快捷键增大和减小值:<ol><li>使用向上键和向下键能够以 1 为增量增大和减小值(如果当前值介于 -1 和 1 之间,则增量为 0.1)。</li><li>使用 Alt+向上键和 Alt+向下键能够以 0.1 为增量增大和减小值。</li><li>使用 Shift+向上键能够以 10 为增量增大值;使用 Shift+向下键能够以 10 为增量减小值。</li><li>使用 Shift+Page Up(Windows、Linux)或 Shift+Function+向上键 (Mac) 能够以 100 为增量增大值。使用 Shift+Page Down(Windows、Linux)或 Shift+Function+向下键 (Mac) 能够以 100 为增量减小值。</li></ol></li></ul></li><li><p>添加新属性声明 <code>点击可编辑 CSS 规则中的空白处可以创建一个新声明。 键入,或者将 CSS 粘贴到 Styles 窗格中。属性及其值将解析并输入到正确的字段中。——( 要启用或停用样式声明,请选中或取消选中旁边的复选框。)</code></p></li><li><p>添加样式规则 <code>点击 (New Style Rule 按钮 图标为 右上角 + ) 按钮可以添加一个新的 CSS 规则。点击并按住按钮可以选择要向哪一个样式表添加规则。</code></p></li><li><p>添加或移除动态样式(伪类) <code>可以在元素上手动设置动态伪类选择器(例如 :active、:focus、:hover 和 :visited)</code></p><ul><li>设置动态状态的两种方式<ol><li>在 Elements 面板内右键点击某个元素,然后从菜单中选择目标伪类 (:active、:focus、:hover 和 :visited) ,将其启用或停用。<br> <img src="!%5B%5D(https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/20200815110607.png)" alt="force_state1"></li><li>在 Elements 面板中选择元素,然后在 Styles 窗格中点击 :hov 按钮,使用复选框启用或停用当前选定元素的选择器。<br> <img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/20200815110529.png" alt="force_state"></li></ol></li></ul></li><li><p>向样式规则添加背景色或颜色</p><ul><li><p>向样式规则添加 color 和 background-color 声明的快捷方式</p><ol><li><p>样式规则的右下角有一个由三个点组成的图标。您需要将鼠标悬停到样式规则上才能看到这个图标。</p></li><li><p>将鼠标悬停到此图标上可以调出用于添加 字体color 声明 或 background-color 声明 的按钮。点击这些按钮之一可以将声明添加到样式规则中。</p><p> <img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/20200815110717.png" alt="add_color"></p></li></ol></li></ul></li><li><p>使用 Color Picker 修改颜色 <code>在 Styles 窗格中查找一个定义颜色的 CSS 声明(例如 color: blue)。 声明值的左侧有一个带颜色的小正方形。 正方形的颜色与声明值匹配。 点击小正方形可以打开 Color Picker。</code><br> <img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/select_color.png" alt="select_color"></p><ul><li><p>与 Color Picker 交互:</p><ol><li><p>取色器 <code>点击取色器按钮将其启用,在实时页面上将鼠标悬停到某种颜色上,然后点击,将当前选定的声明值设置为您悬停的颜色。</code></p></li><li><p>当前颜色。当前值的可视表示。</p></li><li><p>当前值。当前颜色的十六进制、RGBA 或 HSL 表示。</p></li><li><p>调色板。 <code>提供多种调色板</code></p><ul><li>Page Colors。一组从页面的 CSS 自动生成的颜色。</li><li>Material Design。一组符合 Material Design 规范的颜色。</li><li>Custom。您选择的任意一组颜色。DevTools 可以保存您的自定义调色板(甚至在不同的页面间),直至您将其删除。</li><li>修改自定义调色板<ul><li>按加号按钮可以将当前颜色添加到调色板中。 点击并按住颜色可以将其拖动到不同的位置,或者将其拖动到垃圾桶图标进行删除。 右键点击颜色并选择 Remove color 可以将其删除。 选择 Remove all to the right 可以删除当前选定颜色右侧的所有颜色。 右键点击调色板区域中的任何位置,然后选择 Clear template 可以删除模板的所有颜色。</li></ul></li></ul></li><li><p>着色和阴影选择器。</p></li><li><p>色调选择器。</p></li><li><p>不透明度选择器。</p></li><li><p>颜色值选择器。点击可以在 RGBA、HSL 和十六进制之间切换。</p></li><li><p>调色板选择器。点击可以选择不同的模板。</p><p><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/color_picker.png" alt="color_picker"></p></li></ol></li></ul></li><li><p>查看和编辑 CSS 自定义属性(CSS 变量)</p><ul><li>自定义属性通常在 :root 选择器中定义。 要查看在 :root 中定义的自定义属性,请检查 <html> 元素。</html></li></ul></li><li><p>编辑 Sass、Less 或 Stylus <code>借助 CSS 源映射,DevTools 可以将生成的文件自动映射到源代码文件,这样,您可以在 Sources 面板中实时编辑这些文件并查看结果,而不用离开 DevTools 或刷新页面。</code></p></li><li><p>预处理器工作流 <code>检查样式由生成的 CSS 文件提供的元素时,Elements 面板会显示原始的源文件而不是生成的 CSS 文件的链接。</code></p><ul><li>跳转到源文件<ol><li>点击链接,在 Sources 面板中打开(可编辑)源文件。</li><li>Ctrl + 点击(或 Cmd + 点击)任何 CSS 属性名称或值,打开源文件并跳转到相应行。</li></ol></li></ul></li><li><p>启用/停用 CSS 源映射和自动重新加载 <code>默认情况下,CSS 源映射处于启用状态</code></p><ul><li>启用 CSS 源映射和 CSS 重新重新加载<ol><li>打开 DevTools 的 Settings 面板并点击 General。</li><li>开启 Enable CSS source maps 和 Auto-reload generated CSS。</li></ol></li></ul></li></ol></li><li><p>通过 DOM 更新实时修改页面的内容和结构。</p><ul><li>摘要 (TL;DR)<ol><li>DOM 定义您的页面结构。每一个 DOM 节点都是一个页面元素,例如,标题节点或段落节点。</li><li>通过渲染的 DOM 实时编辑页面的内容和结构。</li><li>无法在 Elements 面板中通过 DOM 更改修改源文件。重新加载页面会清空任何 DOM 树修改。</li><li>使用 DOM 断点留意 DOM 更改。</li></ol></li></ul><ol><li><p>检查元素</p><ul><li>右键点击页面上的任何元素并选择 检查 (Inspect)。</li><li>按 Ctrl + Shift + C (Windows) 或 Cmd + Shift + C (Mac),在 Inspect Element 模式下打开 DevTools,然后将鼠标悬停到某个元素上。 DevTools 会在 Elements 面板中自动突出显示您悬停的元素。点击元素可以退出检查模式,同时保持元素在 Elements 面板中处于突出显示状态。</li><li>点击 Inspect Element 按钮 转到 Inspect Element 模式,然后点击元素。</li></ul></li><li><p>DOM 导航</p><ul><li>折叠的节点的旁边会有一个<strong>向右箭头</strong></li><li>展开的节点的旁边会有一个<strong>向下箭头</strong></li><li>使用鼠标<ul><li>点击一次可以突出显示节点。</li><li>要展开节点,请双击节点上的任何地方,或者点击节点旁边的箭头。</li><li>要折叠节点,请点击节点旁边的箭头。</li></ul></li><li>使用键盘<ul><li>按向上箭头键可以选择当前节点上方的节点。</li><li>按向下箭头可以选择当前节点下方的节点。</li><li>按向右箭头键可以展开折叠的节点。再按一次可以移动到(已展开)节点的第一个子级。 您可以使用此方法快速导航到深度嵌套的节点。</li></ul></li></ul></li><li><p>面包屑导航记录导航</p><ul><li><p>Elements 面板的底部是面包屑导航记录。</p></li><li><p>当前选定的节点将以蓝色突出显示。左侧是当前节点的父级。 再左侧是父级的父级。以此类推,一直到 DOM 树。</p></li><li><p>在结构中向上导航会移动突出显示焦点:</p></li><li><p>DevTools 会在记录中显示尽可能多的项目。如果状态栏无法显示全部记录,将在记录截断的地方显示一个省略号 (…)。点击省略号可以显示隐藏的元素:</p><p><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/DOM_tree.png" alt="DOM_tree"></p></li></ul></li><li><p>显示 more actions 菜单</p><ul><li>右键点击节点</li><li>选择节点,然后点击节点前方的 “…” 按钮 (more action 按钮))。按钮仅在当前选定的元素上显示。</li></ul></li><li><p>编辑 DOM 节点和属性</p><ul><li>直接在节点名称或属性上双击。</li><li>突出显示节点,按 Enter,然后按 Tab, 直到选中名称或属性。</li><li>鼠标右键打开 more actions 菜单 并选择 Add Attribute 或 Edit Attribute。 Edit Attribute 取决于上下文;您点击的部分决定要编辑的内容。</li></ul></li><li><p>以 HTML 形式编辑 DOM 节点及其子级</p><ul><li>打开 HTML 形式编辑器<ol><li>鼠标右键打开 more actions 菜单并选择 Edit Attribute。</li><li>按 F2 (Windows / Linux) 或 Fn+F2 (Mac)。</li></ol></li><li>保存更改<ul><li>按 Ctrl+Enter (Windows / Linux) 或 Cmd+Enter (Mac) 保存更改。</li></ul></li><li>按 Esc 可以退出编辑器而不保存。</li></ul></li><li><p>移动 DOM 节点 <code>点击、按住并拖动节点可将其移动。</code></p></li><li><p>删除 DOM 节点</p><ul><li>鼠标右键打开 more actions 菜单并选择 Delete Node。</li><li>选择节点并按 Delete 键。</li><li>如果意外删除了节点,按 Ctrl + Z(Mac 上为 Cmd + Z)可以撤消您的上一步操作。</li></ul></li><li><p>滚动到视图</p><ul><li>悬停或选择 DOM 节点时,渲染的节点将在视口中突出显示</li><li>节点滚动到屏幕以外,在节点位于当前视口上方时您将在视口顶部看到提示,而在节点位于当前视口下方时,您将在底部看到提示</li></ul></li><li><p>设置 DOM 断点以调试复杂的 JavaScript 应用</p><ul><li>将 DOM 断点设置为在元素属性修改时触发<ol><li>子树修改触发 <code>添加、移除或移动子元素时将触发子树修改断点</code> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">//在 main-content 元素上设置子树修改,以下代码将触发断点:</span><br><span class="line">var element = document.getElementById('main-content');</span><br><span class="line">//modify the element's subtree.</span><br><span class="line">var mySpan = document.createElement('span');</span><br><span class="line">element.appendChild( mySpan );</span><br></pre></td></tr></table></figure></li><li>属性修改触发 <code>动态更改元素的属性 (class, id, name) 时将发生属性修改</code> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">var element = document.getElementById('main-content');</span><br><span class="line">// class attribute of element has been modified.</span><br><span class="line">element.className = 'active';</span><br></pre></td></tr></table></figure></li><li>节点移除 <code>从 DOM 中移除有问题的节点时将触发节点移除修改:</code> <pre><code>document.getElementById('main-content').remove();</code></pre></li></ol></li></ul></li><li><p>与 DOM 断点交互 <code>Elements 和 Sources 面板均包含一个用于管理 DOM 断点的窗格,每个断点都会列出元素标识符和断点类型。 (DOM Breakpoints)</code></p><ul><li>断点交互<ol><li>悬停在元素标识符上可以显示元素在页面上的相应位置(类似于在 Elements 面板中悬停在节点上)。</li><li>点击元素可以将其在 Elements 面板中选中。</li><li>切换复选框可以启用或停用断点。</li></ol></li><li>触发 DOM 断点时,断点将在 DOM Breakpoints 窗格中突出显示。 Call Stack 窗格将显示调试程序暂停的原因:</li></ul></li><li><p>查看元素事件侦听器 (Event Listeners 查看与 DOM 节点关联的 JavaScript 事件侦听器。)</p><ul><li>Event Listeners 窗格中的顶级项目将显示具有已注册侦听器的事件类型。</li><li>点击事件类型(例如 click)旁的箭头可以查看已注册事件处理程序的列表。 每个处理程序都由一个类似于 CSS 选择器的元素标识符标识,例如 document 或 button#call-to-action。如果已为相同元素注册多个处理程序,将重复列示元素。</li><li>点击元素标识符旁的展开箭头可以查看事件处理程序的属性。Event Listeners 窗格将列出每个侦听器的以下属性:<ul><li>handler : 包含一个回调函数。右键点击函数并选择 Show Function Definition 可以查看函数的定义位置(如果源代码可用)。</li><li>useCapture : 指示 addEventListener 上的 useCapture 标志是否设置的布尔值。</li></ul></li></ul><ol><li>查看祖先实体事件侦听器 <ul><li>如果启用 Ancestors 复选框,除了当前选定节点的事件侦听器外,还会显示其祖先实体的事件侦听器。</li><li>如果停用复选框,将仅显示当前选定节点的事件侦听器。</li></ul></li><li>查看框架侦听器<ul><li>启用 Framework listeners 复选框时,DevTools 会自动解析事件代码的框架或内容库封装部分,然后告诉您实际将事件绑定到代码中的位置。</li><li>如果停用 Framework listeners 复选框,事件侦听器代码很可能会在框架或内容库代码的其他地方解析。</li></ul></li></ol></li></ol></li></ol></li></ul>]]></content>
<tags>
<tag> Chrome DevTools </tag>
</tags>
</entry>
<entry>
<title>pyecharts-Funnel</title>
<link href="/2020/08/13/pyecharts-funnel/"/>
<url>/2020/08/13/pyecharts-funnel/</url>
<content type="html"><![CDATA[<h1 id="pyecharts-Bar"><a href="#pyecharts-Bar" class="headerlink" title="pyecharts_Bar"></a>pyecharts_Bar</h1><p><a href="http://pyecharts.org/#/zh-cn/" target="_blank" rel="noopener">全部来自于官方文档,主要用于方便自己查找</a></p><h2 id="Funnel:漏斗图"><a href="#Funnel:漏斗图" class="headerlink" title="Funnel:漏斗图"></a><code>Funnel:漏斗图</code></h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">x_data = []</span><br><span class="line">y_data = []</span><br><span class="line"></span><br><span class="line">data = [[x_data[i], y_data[i]] for i in range(len(x_data))</span><br><span class="line"></span><br><span class="line">(</span><br><span class="line"> Funnel(init_opts = opts.InitOpts(width="“, height=""))</span><br><span class="line"> .add(</span><br><span class="line"> series_name = "",</span><br><span class="line"> data_pair=data,</span><br><span class="line"> ...</span><br><span class="line"> )</span><br><span class="line"> .set_global_opts(title_opts=opts.TitleOpts(title="漏斗图主标题", subtitle="副标题"))</span><br><span class="line"> .render("xxx.html")</span><br><span class="line">)</span><br></pre></td></tr></table></figure><ul><li>add() <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">def add(</span><br><span class="line"> # 系列名称,用于 tooltip 的显示,legend 的图例筛选。</span><br><span class="line"> series_name: str,</span><br><span class="line"></span><br><span class="line"> # 系列数据项,格式为 [(key1, value1), (key2, value2)]</span><br><span class="line"> data_pair: Sequence,</span><br><span class="line"></span><br><span class="line"> # 是否选中图例</span><br><span class="line"> is_selected: bool = True,</span><br><span class="line"></span><br><span class="line"> # 系列 label 颜色</span><br><span class="line"> color: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 数据排序, 可以取 'ascending','descending','none'(表示按 data 顺序)</span><br><span class="line"> sort_: str = "descending",</span><br><span class="line"></span><br><span class="line"> # 数据图形间距</span><br><span class="line"> gap: Numeric = 0,</span><br><span class="line"></span><br><span class="line"> # 标签配置项,参考 `series_options.LabelOpts`</span><br><span class="line"> label_opts: Union[opts.LabelOpts, dict] = opts.LabelOpts(),</span><br><span class="line"></span><br><span class="line"> # 提示框组件配置项,参考 `series_options.TooltipOpts`</span><br><span class="line"> tooltip_opts: Union[opts.TooltipOpts, dict, None] = None,</span><br><span class="line"></span><br><span class="line"> # 图元样式配置项,参考 `series_options.ItemStyleOpts`</span><br><span class="line"> itemstyle_opts: Union[opts.ItemStyleOpts, dict, None] = None,</span><br><span class="line"> )</span><br></pre></td></tr></table></figure></li></ul>]]></content>
<tags>
<tag> Pyecharts </tag>
</tags>
</entry>
<entry>
<title>Python_PEP8编程风格规范</title>
<link href="/2020/08/12/python-pep8/"/>
<url>/2020/08/12/python-pep8/</url>
<content type="html"><![CDATA[<h1 id="PEP8-编程风格规范"><a href="#PEP8-编程风格规范" class="headerlink" title="PEP8 编程风格规范"></a>PEP8 编程风格规范</h1><h2 id="代码布局-Code-Lay-Out"><a href="#代码布局-Code-Lay-Out" class="headerlink" title="代码布局 (Code Lay-Out)"></a>代码布局 (Code Lay-Out)</h2><h3 id="1-缩进-Indentation"><a href="#1-缩进-Indentation" class="headerlink" title="1. 缩进 (Indentation)"></a>1. 缩进 (Indentation)</h3><ul><li>每个缩进级别 <strong>4个</strong> 空格</li><li>连续行所包装元素 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"># 同开始分界符(左括号)对齐</span><br><span class="line"> foo = long_function_name(var_one, var_two,</span><br><span class="line"> var_three, var_four)</span><br><span class="line"> </span><br><span class="line"> # 续行多缩进一级以同其他代码区别</span><br><span class="line"> def long_function_name(</span><br><span class="line"> var_one, var_two, var_three,</span><br><span class="line"> var_four):</span><br><span class="line"> print(var_one)</span><br><span class="line"> </span><br><span class="line"> # 悬挂缩进需要多缩进一级</span><br><span class="line"> foo = long_function_name(</span><br><span class="line"> var_one, var_two,</span><br><span class="line"> var_three, var_four)</span><br></pre></td></tr></table></figure><ul><li>Python隐式续行 <code>垂直对齐于圆括号、方括号和花括号</code></li><li>悬挂缩进 <code>第一行不应该包括参数,并且在续行中 (续行来说,4空格的规则可以不遵守) 需要再缩进一级以便清楚表示</code></li></ul></li><li>多行结束右圆/方/花括号可以单独一行书写,和上一行的缩进对齐;也可以和多行开始的第一行的第一个字符对齐<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">my_list = [</span><br><span class="line"> 1, 2, 3,</span><br><span class="line"> 4, 5, 6,</span><br><span class="line"> ]</span><br><span class="line">result = some_function_that_takes_arguments(</span><br><span class="line"> 'a', 'b', 'c',</span><br><span class="line"> 'd', 'e', 'f',</span><br><span class="line"> )</span><br><span class="line">my_list = [</span><br><span class="line"> 1, 2, 3,</span><br><span class="line"> 4, 5, 6,</span><br><span class="line">]</span><br></pre></td></tr></table></figure></li><li>推荐使用空格来进行缩进。Python 3不允许tab和空格混合使用。</li></ul><h3 id="2-每行最大长度-Maximum-Line-Length"><a href="#2-每行最大长度-Maximum-Line-Length" class="headerlink" title="2. 每行最大长度(Maximum Line Length)"></a>2. 每行最大长度(Maximum Line Length)</h3><ul><li>所有行都限制在 <strong>79</strong> 个字符以内</li><li>连续大段的文字(比如文档字符串(docstring)或注释),其结构上的限制更少,这些行应该被限制在72个字符长度内。</li><li>换行方式是利用Python圆括号、方括号和花括号中的隐式续行。长行可以通过在括号内换行来分成多行。应该最好加上反斜杠来区别续行。</li></ul><h3 id="3-二元运算符之前还是之后换行"><a href="#3-二元运算符之前还是之后换行" class="headerlink" title="3. 二元运算符之前还是之后换行?"></a>3. 二元运算符之前还是之后换行?</h3><ul><li>Kunth 风格: 尽管在段落中的公式总是在二元运算符之后换行,但显示公式时总是在二元运算符之前换行</li><li>如果项目约定,也可以在二元运算符之后换行</li></ul><h3 id="4-空行-支持control-L-换行符作为空格"><a href="#4-空行-支持control-L-换行符作为空格" class="headerlink" title="4. 空行 (支持control + L 换行符作为空格)"></a>4. 空行 (支持control + L 换行符作为空格)</h3><ul><li>使用 <strong>2个</strong> 空行来分隔最外层的 <strong>函数(function)</strong> 和 <strong>类(class)</strong> 定义</li><li>使用 <strong>1个</strong> 空行来分隔 <strong>类中的方法(method)</strong> 定义</li><li>额外的空行(尽量少) 来分隔一组相关的函数</li><li>在函数内使用空行(尽量少) 使代码逻辑更加清晰</li></ul><h3 id="5-源文件编码-Source-File-Encoding"><a href="#5-源文件编码-Source-File-Encoding" class="headerlink" title="5. 源文件编码(Source File Encoding)"></a>5. 源文件编码(Source File Encoding)</h3><ul><li>python 2 使用 ASCII</li><li>python 3 使用 UTF-8 </li><li>Python标准库中的所有<strong>标识符</strong>都必须只采用ASCII编码的标识符,在可行的条件下也应当使用英文词</li></ul><h3 id="6-模块引用-Imports"><a href="#6-模块引用-Imports" class="headerlink" title="6. 模块引用(Imports)"></a>6. 模块引用(Imports)</h3><ul><li><p>imports 分行写,不应都写在一行</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"># 分开写</span><br><span class="line">import os</span><br><span class="line"># 被允许的</span><br><span class="line">from subprocess import Popen, PIPE</span><br></pre></td></tr></table></figure></li><li><p>imports 写在代码文件的开头,位于模块(module)<strong>注释</strong>和<strong>文档字符串</strong>(docstring)之<strong>后</strong>,模块<strong>全局变量</strong>(globals)和<strong>常量</strong>(constants)声明之””.</p></li><li><p>imports 顺序 (不同的 imports 之间用空格隔开)</p><ol><li>标准库 imports </li><li>相关第三方 imports</li><li>本地应用/库的特定 imports</li></ol></li><li><p>推荐使用绝对(absolute) imports (易读且会报错)</p><p> <code>from mypkg import sibling</code></p></li><li><p>显式的相对imports也是一种可以接受的替代方式.特别是当处理复杂的包布局(package layouts)时,采用绝对imports会显得啰嗦.</p><p> <code>from .sibling import example</code></p></li><li><p>标准库代码应当一直使用绝对imports,避免复杂的包布局.<br>隐式的相对imports应该永不使用,并且Python 3中已经被去掉了.</p></li><li><p>如果从包含类的模块import的类与本地命名产生冲突,可以直接 import 模块</p></li><li><p>避免使用通配符进行 imports, 会产生许多不必要的麻烦</p></li></ul><h3 id="7-模块级的双下划线命名-Module-level-dunder-names"><a href="#7-模块级的双下划线命名-Module-level-dunder-names" class="headerlink" title="7. 模块级的双下划线命名(Module level dunder names)"></a>7. 模块级的双下划线命名(Module level dunder names)</h3><ul><li>模块及级下划线命名的<strong>变量</strong>,<strong>函数</strong>,应该在 文档的多行注释字符串之后</li></ul><h3 id="8-字符串的引用-String-Quotes"><a href="#8-字符串的引用-String-Quotes" class="headerlink" title="8. 字符串的引用(String Quotes)"></a>8. 字符串的引用(String Quotes)</h3><ul><li>当字符串中包含单引号时,采用双引号来表示字符串,可以避免使用反斜杠,代码也更易读</li><li>在一个项目中最好字符串都用单引号或者双引号</li></ul><h3 id="9-表达式和语句中的空格-Whitespace-In-Expressions-And-Statements"><a href="#9-表达式和语句中的空格-Whitespace-In-Expressions-And-Statements" class="headerlink" title="9. 表达式和语句中的空格(Whitespace In Expressions And Statements)"></a>9. 表达式和语句中的空格(Whitespace In Expressions And Statements)</h3><ul><li><strong>方括号</strong>、<strong>圆括号</strong>和<strong>花括号</strong>之后避免使用空格</li><li><strong>逗号</strong>,<strong>分号</strong>或<strong>冒号</strong>之前避免使用空格</li><li>切片操作时避免使用空格<ul><li>冒号和二元运算符是一样的,应该在其左右两边保留相同数量的空格;</li><li>扩展切片操作中,所有冒号的左右两边空格数都应该相等;</li><li>切片操作中的参数被省略时,应该也忽略空格.</li></ul></li><li>调用函数时传递参数list的括号之前避免使用空格</li><li>索引和切片操作的左括号之前避免使用空格</li><li>赋值(或其他)运算符周围使用多个空格来和其他语句对齐</li><li>避免任何行末的空格.</li><li>二元运算符的两边都使用一个空格:赋值运算符(=),增量赋值运算符(+=, -= etc.),比较运算符(==, <, >, !=, <>, <=, >=, in, not in, is, is not),布尔运算符(and, or, not).</li><li>优先级不同的运算符,则在优先级较低的操作符周围增加空白.(不要用超过1个空格,永远保持二元运算符两侧的空白数量一样.)</li><li>使用=符号来表示关键字参数或参数默认值时,不要在其周围使用空格.</li><li>函数注解中的:也遵循一般的:加空格的规则,在->两侧各使用一个空格.</li><li>在组合使用函数注解和参数默认值时,需要在=两侧各使用一个空格(只有当这个参数既有函数注解,又有默认值的时候).</li><li>复合语句(即将多行语句写在一行)一般是不鼓励使用的.</li><li>短小的if/for/while中的语句写在一行,多分支的不要使用</li></ul><h3 id="10-何时在末尾加逗号-When-to-use-trailing-commas"><a href="#10-何时在末尾加逗号-When-to-use-trailing-commas" class="headerlink" title="10. 何时在末尾加逗号(When to use trailing commas)"></a>10. 何时在末尾加逗号(When to use trailing commas)</h3><ul><li>定义单元素元组(tuple)时是必需的<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">FILES = ('setup.cfg',)</span><br></pre></td></tr></table></figure></li><li>在将来有可能扩展的列表末尾添加冗余的逗号,每一个元素写在单独的一行,并在行尾添加逗号,右括号单独占一行.<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">FILES = [</span><br><span class="line"> 'setup.cfg',</span><br><span class="line"> 'tox.ini',</span><br><span class="line"> ]</span><br></pre></td></tr></table></figure></li></ul><h3 id="11-注释-Comments"><a href="#11-注释-Comments" class="headerlink" title="11. 注释(Comments)"></a>11. 注释(Comments)</h3><ul><li>当代码有改动时,一定要优先更改注释使其保持最新.</li><li>注释应该是完整的多个句子.如果注释是一个短语或一个句子,其首字母应该大写,除非开头是一个以小写字母开头的标识符(永远不要更改标识符的大小写).</li><li>注释很短,结束的句号可以被忽略.块注释通常由一段或几段完整的句子组成,每个句子都应该以句号结束.</li><li>在句尾的句号后再加上2个空格.</li><li>尽量用英文写注释</li><li>块注释(Block Comments)<ul><li>一般写在对应代码之前,并且和对应代码有同样的缩进级别.块注释的每一行都应该以#和一个空格开头(除非该文本是在注释内缩进对齐的).</li></ul></li><li>行内注释(Inline Comments)<ul><li>尽量少用行内注释. <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">x = x + 1 # x自加</span><br></pre></td></tr></table></figure></li><li>行内注释是和代码语句写在一行内的注释.行内注释应该至少和代码语句之间有两个空格的间隔,并且以#和一个空格开始.</li></ul></li><li>文档字符串注释(Documentation Strings)<ul><li>所有的公共模块,函数,类和方法都应该有文档字符串.对于非公共方法,文档字符串不是必要的,但你应该留有注释说明该方法的功能,该注释应当出现在def的下一行.</li><li>多行文档字符串以单行”””结尾,不能有其他字符</li><li>对于仅有一行的文档字符串,结尾处的”””应该也写在这一行.</li></ul></li></ul><h3 id="12-命名约定-Naming-Conventions"><a href="#12-命名约定-Naming-Conventions" class="headerlink" title="12. 命名约定(Naming Conventions)"></a>12. 命名约定(Naming Conventions)</h3><ul><li><p>首要原则(Overriding Principle)</p><ul><li>对于用户可见的公共部分API,其命名应当表达出功能用途而不是其具体的实现细节.</li></ul></li><li><p>命名风格(Descriptive: Naming Styles)</p><ul><li>CapitalizedWords (也叫做CapWords或者CamelCase – 因为单词首字母大写看起来很像驼峰).也被称作StudlyCaps.<br>注意:当CapWords里包含缩写时,将缩写部分的字母都大写.HTTPServerError比HttpServerError要好.</li><li>mixedCase (注意:和CapitalizedWords不同在于其首字母小写!)</li><li>Capitalized_Words_With_Underscores (这种风格超丑!)</li><li>UPPER_CASE_WITH_UNDERSCORES(带下划线大写)</li><li>UPPERCASE(大写)</li><li>lower_case_with_underscores(带下划线小写)</li><li>lowercase(小写)</li><li>B (单个大写字母)</li><li>b (单个小写字母)</li><li>特殊形式<ul><li>_single_leading_underscore: 以单个下划线开头是”内部使用”的弱标志。</li><li>single_trailing_underscore_: 以单个下划线结尾用来避免和Python关键词产生冲突 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">class_="xxx"</span><br></pre></td></tr></table></figure></li><li><strong>double_leading_underscore: 以双下划线开头的风格命名类属性表示触发命名修饰(在FooBar类中,</strong>boo命名会被修饰成_FooBar__boo)</li><li><strong>double_leading_and_trailing_underscore</strong>: 以双下划线开头和结尾的命名风格表示“魔术”对象或属性,存在于用户控制的命名空间(user-controlled namespaces)里</li></ul></li></ul></li><li><p>命名约定(Prescriptive: Naming Conventions)</p><ul><li>需要避免的命名(Names To Avoid)<ul><li>字符’l’(L的小写的字母)</li><li>’O’(o大写的字母)</li><li>’I’(i的大写的字母)</li></ul></li><li>ASCII兼容性(ASCII Compatibility)</li><li>包和模块命名(Package And Module Names)<ul><li>模块命名应短小,且为全小写.若下划线能提高可读性,也可以在模块名中使用</li><li>包命名也应该短小,且为全小写,但不应使用下划线.</li><li>使用C或C++写的扩展模块有相应的Python模块提供更高级的接口时, C/C++模块名以下划线开头(例如,_sociket)</li></ul></li><li>类命名(Class Names)<ul><li>驼峰(CapWords)的命名约定</li></ul></li><li>类型变量命名(Type variable names)<ul><li>类型变量名称通常应使用简短的驼峰命名</li></ul></li><li>异常命名(Exception Names)<ul><li>类命名约定也适用与异常.不同的是,如果异常实际上是抛出错误的话,异常名前应该加上”Error”的前缀.</li></ul></li><li>函数命名(Function Names)<ul><li>函数命名应该都是小写,必要时使用下划线来提高可读性.</li></ul></li><li>全局变量命名(Global Variable Names)<ul><li>引用方式设计为from M import *的模块,应该使用<strong>all</strong>机制来避免import全局变量,或者采用下划线前缀的旧约定来命名全局变量,从而表明这些变量是“模块非公开的”.</li></ul></li><li>函数和方法参数(Function And Method Arguments)<ul><li>实例方法的第一参数永远都是self.</li><li>类方法的第一个参数永远都是cls.</li><li>在函数参数名和保留关键字冲突时,相对于使用缩写或拼写简化,使用以下划线结尾的命名一般更好.比如,class_比clss更好.</li></ul></li><li>方法命名和实例变量(Method Names And Instance Variables)<ul><li>使用函数命名的规则:小写单词,必要时使用下划线分开以提高可读性.</li><li>仅对于非公开方法和变量命名在开头使用一个下划线</li><li>避免和子类的命名冲突,使用两个下划线开头来触发Python的命名修饰机制.</li></ul></li></ul></li><li><p>常量(Constants)</p><ul><li>常量通常是在模块级别定义的,使用全部大写并用下划线将单词分开.如:MAX_OVERFLOW和TOTAL .</li></ul></li></ul><h3 id="13-公开和内部接口-Public-And-Internal-Interfaces"><a href="#13-公开和内部接口-Public-And-Internal-Interfaces" class="headerlink" title="13. 公开和内部接口(Public And Internal Interfaces)"></a>13. 公开和内部接口(Public And Internal Interfaces)</h3><ul><li>任何向后兼容性保证仅对公开接口适用</li><li>文档化的接口被认为是公开的,除非文档中明确申明了它们是临时的或者内部接口,不保证向后兼容性.所有文档中未提到的接口应该被认为是内部的.</li><li>为了更好审视公开接口和内部接口,模块应该在<strong>all属性中明确申明公开API是哪些.将</strong>all__设为空list表示该模块中没有公开API.</li></ul><h3 id="14-编程建议-Programming-Recommendations"><a href="#14-编程建议-Programming-Recommendations" class="headerlink" title="14. 编程建议(Programming Recommendations)"></a>14. 编程建议(Programming Recommendations)</h3><ul><li>代码应该以不影响其他Python实现(PyPy,Jython,IronPython,Cython,Psyco等)的方式编写.<ul><li>库中性能敏感的部分,用’’.join形式来代替.这会确保在所有不同的实现中字符串拼接是线性时间的.</li><li>不要依赖于 CPython 在字符串拼接时的优化实现</li></ul></li><li>与单例作比较,像None应该用is或is not,从不使用==操作符</li><li>用<strong>is not</strong>操作符而不是not … is.虽然这两个表达式是功能相同的,前一个是更可读的,是首选.</li><li>始终使用def语句来代替直接绑定了一个lambda表达式的赋值语句.</li><li>捕获异常时,尽可能使用明确的异常,而不是用一个空的except:语句.(限制使用空except语句)</li><li>绑定异常给一个名字时,最好使用 Python 2.6 里添加的明确的名字绑定语法:</li><li>对于所有try / except子句,将try子句限制为必需的绝对最小代码量.同样,这样可以避免屏蔽错误.</li><li>坚持使用return语句.函数内的return语句都应该返回一个表达式,或者None.如果一个return语句返回一个表达式,另一个没有返回值的应该用return None清晰的说明,并且在一个函数的结尾应该明确使用一个return语句(如果有返回值的话).</li><li>用字符串方法代替字符串模块.</li><li>用’’.startswith()和’’.endswith()代替字符串切片来检查前缀和后缀.startswith()和endswith()是更简洁的,不容易出错的.</li><li>对象类型的比较应该始终使用isinstance()而不是直接比较.</li><li>对于序列(字符串、列表、元组)来说,空的序列为False:</li><li>对于序列(字符串、列表、元组)来说,空的序列为False:</li><li>不要让字符串对尾随的空格有依赖.这样的尾随空格是视觉上无法区分的,一些编辑器(或者,reindent.py)会将其裁剪掉.</li><li>不要用==比较True和False.</li></ul>]]></content>
<tags>
<tag> PEP8 编程风格规范 </tag>
</tags>
</entry>
<entry>
<title>pyecharts-Liquid</title>
<link href="/2020/08/10/pyecharts-liquid/"/>
<url>/2020/08/10/pyecharts-liquid/</url>
<content type="html"><![CDATA[<h1 id="pyecharts-Liquid"><a href="#pyecharts-Liquid" class="headerlink" title="pyecharts_Liquid"></a>pyecharts_Liquid</h1><p><a href="http://pyecharts.org/#/zh-cn/" target="_blank" rel="noopener">全部来自于官方文档,主要用于方便自己查找</a></p><h2 id="Liquid:水球图"><a href="#Liquid:水球图" class="headerlink" title="Liquid:水球图"></a><code>Liquid:水球图</code></h2> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">c = (</span><br><span class="line"> Liquid("鼠标移动显示", [0.5, 0.7])</span><br><span class="line"> .add()</span><br><span class="line"> .set_global_opts(title_opts=opts.TitleOpts(title=""))</span><br><span class="line"> .render("xxxx")</span><br><span class="line">)</span><br></pre></td></tr></table></figure><ul><li>.add()<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line">class Liquid(</span><br><span class="line"> # 初始化配置项,参考 `global_options.InitOpts`</span><br><span class="line"> init_opts: opts.InitOpts = opts.InitOpts()</span><br><span class="line">)</span><br><span class="line">func pyecharts.charts.Liquid.add</span><br><span class="line"></span><br><span class="line">def add(</span><br><span class="line"> # 系列名称,用于 tooltip 的显示,legend 的图例筛选。</span><br><span class="line"> series_name: str,</span><br><span class="line"></span><br><span class="line"> # 系列数据,格式为 [value1, value2, ....]</span><br><span class="line"> data: Sequence,</span><br><span class="line"></span><br><span class="line"> # 水球外形,有' circle', 'rect', 'roundRect', 'triangle', 'diamond', 'pin', 'arrow' 可选。</span><br><span class="line"> # 默认 'circle'。也可以为自定义的 SVG 路径。</span><br><span class="line"> shape: str = "circle",</span><br><span class="line"></span><br><span class="line"> # 波浪颜色。</span><br><span class="line"> color: Optional[Sequence[str]] = None,</span><br><span class="line"></span><br><span class="line"> # 背景颜色</span><br><span class="line"> background_color: types.Union[str, dict, None] = None,</span><br><span class="line"></span><br><span class="line"> # 是否显示波浪动画。</span><br><span class="line"> is_animation: bool = True,</span><br><span class="line"></span><br><span class="line"> # 是否显示边框。</span><br><span class="line"> is_outline_show: bool = True,</span><br><span class="line"></span><br><span class="line"> # 外沿边框宽度</span><br><span class="line"> outline_border_distance: types.Numeric = 8,</span><br><span class="line"></span><br><span class="line"> # 外沿样式</span><br><span class="line"> outline_itemstyle_opts: types.ItemStyle = None,</span><br><span class="line"></span><br><span class="line"> # 标签配置项,参考 `series_options.LabelOpts`</span><br><span class="line"> label_opts: Union[opts.LabelOpts, dict] = opts.LabelOpts(font_size=50, position="inside"),</span><br><span class="line"></span><br><span class="line"> # 提示框组件配置项,参考 `series_options.TooltipOpts`</span><br><span class="line"> tooltip_opts: Union[opts.TooltipOpts, dict, None] = None,</span><br><span class="line">)</span><br></pre></td></tr></table></figure></li></ul>]]></content>
<tags>
<tag> Pyecharts </tag>
</tags>
</entry>
<entry>
<title>Flask-note2</title>
<link href="/2020/08/08/flask-note2/"/>
<url>/2020/08/08/flask-note2/</url>
<content type="html"><![CDATA[<p><a href="https://www.youtube.com/watch?v=RWviEK1Si68&list=PLDFBYdF-BxV1G4FBpG1EMyFtbsbZuJOvD" target="_blank" rel="noopener">根据Youtube上视频学习</a></p><h4 id="利用邮箱重置密码"><a href="#利用邮箱重置密码" class="headerlink" title="利用邮箱重置密码"></a>利用邮箱重置密码</h4><ol><li>在登录界面 (login.html) 创建 “忘记密码,找回选项”</li></ol><p><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/Forget_Password.png" alt><br> <figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">div</span> <span class="attr">class</span>=<span class="string">"row"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">div</span> <span class="attr">class</span>=<span class="string">"col-md-6"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">hr</span>></span></span><br><span class="line"> {# 创建 找回密码界面 "send_password_reset_request.html" #}</span><br><span class="line"> Password forget? <span class="tag"><<span class="name">a</span> <span class="attr">href</span>=<span class="string">"{{ url_for('send_password_reset_request') }}"</span>></span></span><br><span class="line"> Click here to reset your password.<span class="tag"></<span class="name">a</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">div</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">div</span>></span></span><br></pre></td></tr></table></figure><br>2. 创建 找回密码界面 “send_password_reset_request.html” 界面配置与 注册界面类似<br> <figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">{% extends 'base.html' %}</span><br><span class="line"></span><br><span class="line">{% block app_content %}</span><br><span class="line"> # 重置密码标题</span><br><span class="line"> <span class="tag"><<span class="name">h1</span>></span>Send Reset Password Email<span class="tag"></<span class="name">h1</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">br</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">div</span> <span class="attr">class</span>=<span class="string">"row"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">div</span> <span class="attr">class</span>=<span class="string">"col-md-6"</span>></span></span><br><span class="line"> {% import 'bootstrap/wtf.html' as wtf %}</span><br><span class="line"> {{ wtf.quick_form(form) }}</span><br><span class="line"> <span class="tag"></<span class="name">div</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">div</span>></span></span><br><span class="line"></span><br><span class="line">{% endblock %}</span><br></pre></td></tr></table></figure><br>3. 在 forms.py 内创建 重置密码表单,</p><pre><code>![](https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/Email_not_exists.png)<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">PasswordResetRequestForm</span><span class="params">(FlaskForm)</span>:</span></span><br><span class="line"> <span class="comment"># 输入 Email 表格</span></span><br><span class="line"> email = StringField(<span class="string">'Email'</span>, validators=[DataRequired(), Email()])</span><br><span class="line"> <span class="comment"># 发送邮件按钮</span></span><br><span class="line"> submit = SubmitField(<span class="string">'Send'</span>)</span><br><span class="line"> </span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">validate_email</span><span class="params">(self, email)</span>:</span></span><br><span class="line"> <span class="comment"># 获取 Email</span></span><br><span class="line"> email = User.query.filter_by(email=email.data).first()</span><br><span class="line"> <span class="comment"># 判断 邮箱是否存在 如果不存在 报错</span></span><br><span class="line"> <span class="keyword">if</span> <span class="keyword">not</span> email:</span><br><span class="line"> <span class="keyword">raise</span> ValidationError(<span class="string">'Email not exists.'</span>)</span><br></pre></td></tr></table></figure></code></pre><ol start="4"><li>在 route.py 页面设置 发用邮件验证界面 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">@app.route('/send_password_reset_request', methods=["GET", "POST"])</span><br><span class="line">def send_password_reset_request():</span><br><span class="line"> # 判断是否是已经处于登录状态,如果是回到主界面</span><br><span class="line"> if current_user.is_authenticated:</span><br><span class="line"> return redirect(url_for('index'))</span><br><span class="line"> form = PasswordResetRequestForm()</span><br><span class="line"> return render_template('send_password_reset_request.html', form=form)</span><br></pre></td></tr></table></figure></li></ol><hr><h4 id="通过加密发送邮件给用户,安全实现密码更改-PyJWT-加密,flask-mail-发送邮件验证"><a href="#通过加密发送邮件给用户,安全实现密码更改-PyJWT-加密,flask-mail-发送邮件验证" class="headerlink" title="通过加密发送邮件给用户,安全实现密码更改 (PyJWT 加密,flask-mail 发送邮件验证)"></a>通过加密发送邮件给用户,安全实现密码更改 (PyJWT 加密,flask-mail 发送邮件验证)</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">pip install PyJWT</span><br><span class="line">pip install flask-mail</span><br></pre></td></tr></table></figure><ol><li><p>在 model.py 下创建加密规则与解密验证规则<br><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/Token.png" alt><br><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/Check_Token.png" alt></p> <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> jwt</span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">generate_reset_password_token</span><span class="params">(self)</span>:</span></span><br><span class="line"> <span class="comment"># 将 token 与用户名 作为验证信息加密 传输</span></span><br><span class="line"> <span class="keyword">return</span> jwt.encode({<span class="string">'id'</span>: self.id}, current_app.config[<span class="string">'SECRET_KEY'</span>], algorithm=<span class="string">"HS256"</span>)</span><br><span class="line"> </span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">check_reset_password_token</span><span class="params">(self, token)</span>:</span></span><br><span class="line"> <span class="comment"># 验证是加密的 验证信息 是否正确,是否遭到篡改</span></span><br><span class="line"> <span class="keyword">try</span>:</span><br><span class="line"> data = jwt.decode(token, current_app.config[<span class="string">'SECRET_KEY'</span>], algorithm=[<span class="string">"HS256"</span>])</span><br><span class="line"> <span class="keyword">return</span> User.query.filter_by(id=data[<span class="string">'id'</span>]).first()</span><br><span class="line"> <span class="keyword">except</span>:</span><br><span class="line"> <span class="keyword">return</span></span><br></pre></td></tr></table></figure></li><li><p>在 __init__.py 内部导入 flask-mail</p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">from flask_mail import Mail</span><br><span class="line"># mail 需要很多配置 可以百度 flask mail config</span><br><span class="line">mail = Mail(app)</span><br></pre></td></tr></table></figure></li><li><p>配置 mail 设置 config (在 config.py内配置)</p><p> <img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/error_smtplib.png" alt></p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"># Flask Gmail Config</span><br><span class="line"># 服务器 也可以用 qq 邮箱 smtp.qq.com (国内最好使用qq邮箱)</span><br><span class="line">MAIL_SERVER = 'smtp.gmail.com'</span><br><span class="line"># 端口 (不要使用默认端口 465 ,使用其他端口 25 或者其他的,否则会报错 smtplib.SMTPServerDisconnected: Connection unexpectedly closed)</span><br><span class="line">MAIL_PORT = 25</span><br><span class="line">MAIL_USER_SSL = True</span><br><span class="line"># 将 MAIL_USERNAME/qq 写入环境变量,放入自己的 GMAIL/qq 账号与 令牌 (GMAIL_PASSWORD,需要填入令牌)</span><br><span class="line">MAIL_USERNAME = os.environ.get('GMAIL_USERNAME') or 'GMAIL_USERNAME'</span><br><span class="line">MAIL_PASSWORD = os.environ.get('GMAIL_PASSWORD') or 'GMAIL_PASSWORD'</span><br></pre></td></tr></table></figure></li><li><p>新建 email.py 页面 定义发送邮件</p> <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> flask <span class="keyword">import</span> current_app, render_template</span><br><span class="line"><span class="keyword">from</span> flask_mail <span class="keyword">import</span> Message</span><br><span class="line"><span class="keyword">from</span> app <span class="keyword">import</span> mail</span><br><span class="line"> </span><br><span class="line"> </span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">send_reset_password_mail</span><span class="params">(user, token)</span>:</span></span><br><span class="line"> msg = Message(<span class="string">"[Flask App] Reset Your Password!"</span>,</span><br><span class="line"> <span class="comment"># 发送者邮箱,在 config.py 内定义的 邮箱</span></span><br><span class="line"> sender=current_app.config[<span class="string">"MAIL_USERNAME"</span>],</span><br><span class="line"> <span class="comment"># 接受者邮箱</span></span><br><span class="line"> recipients=[user.email],</span><br><span class="line"> html=render_template(<span class="string">'reset_password_mail.html'</span>,user=user, token=token))</span><br><span class="line"> mail.send(message=msg)</span><br></pre></td></tr></table></figure></li><li><p>在 route.py 内配置重设密码页面以及补全发送邮箱验证界面</p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">def send_password_reset_request():</span><br><span class="line"> if current_user.is_authenticated:</span><br><span class="line"> return redirect(url_for('index'))</span><br><span class="line"> form = PasswordResetRequestForm()</span><br><span class="line"> # 获取提交上来的注册数据,进行处理</span><br><span class="line"> if form.validate_on_submit():</span><br><span class="line"> email = form.email.data</span><br><span class="line"> user = User.query.filter_by(email=email).first()</span><br><span class="line"> # token 作为参数加密放入链接发送给用户</span><br><span class="line"> token = user.generate_reset_password_token()</span><br><span class="line"> # 创建 email.py 发送给用户 加密 token</span><br><span class="line"> send_reset_password_mail(user, token)</span><br><span class="line"> # 建立 flash 提醒用户发送重设密码邮件成功</span><br><span class="line"> flash('Password reset requests mail is send, please check your mail.', category='info')</span><br><span class="line"> return render_template('send_password_reset_request.html', form=form)</span><br><span class="line"> </span><br><span class="line"> </span><br><span class="line">@app.route('/reset_password', methods=["GET", "POST"])</span><br><span class="line"># 重设密码界面 </span><br><span class="line">def reset_password():</span><br><span class="line"> # 判断用户登录状态</span><br><span class="line"> if current_user.is_authenticated:</span><br><span class="line"> return redirect(url_for('index'))</span><br><span class="line"> form = ResetPasswordForm()</span><br><span class="line"> # 根据表单渲染 reset_password.html 页面</span><br><span class="line"> return render_template('reset_password.html', form=form)</span><br></pre></td></tr></table></figure></li><li><p>创建发送给用户的 重设密码邮件 界面 “reset_password_mail.html”</p> <figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">p</span>></span>Dear {{ user.username }}<span class="tag"></<span class="name">p</span>></span></span><br><span class="line"><span class="tag"><<span class="name">p</span>></span></span><br><span class="line"> To reset your password</span><br><span class="line"> <span class="tag"><<span class="name">a</span> <span class="attr">href</span>=<span class="string">"{{ url_for('reset_password', token=token, _external=True) }}"</span>></span></span><br><span class="line"> click here!</span><br><span class="line"> <span class="tag"></<span class="name">a</span>></span></span><br><span class="line"><span class="tag"></<span class="name">p</span>></span></span><br><span class="line"><span class="tag"><<span class="name">p</span>></span>Alternatively, you can paste the following link in your browser's address bar:<span class="tag"></<span class="name">p</span>></span></span><br><span class="line"><span class="tag"><<span class="name">p</span>></span>{{ url_for('reset_password', token=token, _external=True) }}<span class="tag"></<span class="name">p</span>></span></span><br><span class="line"><span class="tag"><<span class="name">p</span>></span>If you have not requested a password reset simply ignore this message.<span class="tag"></<span class="name">p</span>></span></span><br><span class="line"><span class="tag"><<span class="name">p</span>></span>Sincerely,<span class="tag"></<span class="name">p</span>></span></span><br><span class="line"><span class="tag"><<span class="name">p</span>></span>Flask App<span class="tag"></<span class="name">p</span>></span></span><br></pre></td></tr></table></figure></li><li><p>创建重置密码页面 (reset_password.html) 与注册页面类似<br> <img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/e_mail.png" alt><br> <img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/reset_password.png" alt></p> <figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">{% extends 'base.html' %}l></span><br><span class="line"></span><br><span class="line">{% block app_content %}</span><br><span class="line"> <span class="tag"><<span class="name">h1</span>></span>Reset Your Password<span class="tag"></<span class="name">h1</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">br</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">div</span> <span class="attr">class</span>=<span class="string">"row"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">div</span> <span class="attr">class</span>=<span class="string">"col-md-6"</span>></span></span><br><span class="line"> {% import 'bootstrap/wtf.html' as wtf %}</span><br><span class="line"> {{ wtf.quick_form(form) }}</span><br><span class="line"> <span class="tag"></<span class="name">div</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">div</span>></span></span><br><span class="line"></span><br><span class="line">{% endblock %}</span><br></pre></td></tr></table></figure></li><li><p>完善 更改用户密码功能 。(route.py)</p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"># 将 '/reset_password' 改为 '/reset_password/<token>' 获取传递过去的 token</span><br><span class="line">@app.route('/reset_password/<token>', methods=["GET", "POST"])</span><br><span class="line">def reset_password(token):</span><br><span class="line"> if current_user.is_authenticated:</span><br><span class="line"> return redirect(url_for('index'))</span><br><span class="line"> form = ResetPasswordForm()</span><br><span class="line"> # 获取表单数据</span><br><span class="line"> if form.validate_on_submit():</span><br><span class="line"> # 解密 token 获得用户名</span><br><span class="line"> user = User.check_reset_password_token(token=token)</span><br><span class="line"> # 如果用户存在</span><br><span class="line"> if user:</span><br><span class="line"> # 获取用户新更改的密码,传入数据库进行更新操作</span><br><span class="line"> user.password = bcrypt.generate_password_hash(form.password.data)</span><br><span class="line"> db.session.commit()</span><br><span class="line"> flash('Your Password reset is done, You can login now use new Password.', category='info')</span><br><span class="line"> else:</span><br><span class="line"> flash("The user is not exist", category='info')</span><br><span class="line"> return redirect(url_for('login'))</span><br><span class="line"> return render_template('reset_password.html', form=form)</span><br></pre></td></tr></table></figure><p>为了方便,将 models.py 中的 验证密码改为 返回函数的静态方法,不需要实例化直接传参使用</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"> @staticmethod</span><br><span class="line"> def check_reset_password_token(token):</span><br><span class="line"> # 验证是加密的 验证信息 是否正确,是否遭到篡改</span><br><span class="line"> try:</span><br><span class="line"> data = jwt.decode(token, current_app.config['SECRET_KEY'], algorithm=["HS256"])</span><br><span class="line">return User.query.filter_by(id=data['id']).first()</span><br><span class="line">except:</span><br><span class="line">return</span><br></pre></td></tr></table></figure></li><li><p>优化: 运用线程对发送邮件进行加速,使发送在后端进行,前端快速返回 (修改 email.py)</p> <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> threading <span class="keyword">import</span> Thread</span><br><span class="line"><span class="keyword">from</span> flask <span class="keyword">import</span> current_app, render_template</span><br><span class="line"><span class="keyword">from</span> flask_mail <span class="keyword">import</span> Message</span><br><span class="line"><span class="keyword">from</span> app <span class="keyword">import</span> mail, app</span><br><span class="line"> </span><br><span class="line"> </span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">send_async_mail</span><span class="params">(app, msg)</span>:</span></span><br><span class="line"> <span class="keyword">with</span> app.app_context():</span><br><span class="line"> mail.send(msg)</span><br><span class="line"> </span><br><span class="line"> </span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">send_reset_password_mail</span><span class="params">(user, token)</span>:</span></span><br><span class="line"> msg = Message(<span class="string">"[Flask App] Reset Your Password!"</span>,</span><br><span class="line"> sender=current_app.config[<span class="string">"MAIL_USERNAME"</span>],</span><br><span class="line"> recipients=[user.email],</span><br><span class="line"> html=render_template(<span class="string">'reset_password_mail.html'</span>,user=user, token=token))</span><br><span class="line"> <span class="comment"># print(user.email, current_app.config["MAIL_USERNAME"])</span></span><br><span class="line"> <span class="comment"># mail.send(message=msg)</span></span><br><span class="line"> <span class="comment"># 调用线程在后端进行发送,前端快速进行页面更改</span></span><br><span class="line"> Thread(target=send_async_mail, args=(app, msg, )).start()</span><br></pre></td></tr></table></figure></li></ol><hr><h4 id="对-index-主页进行修改,实现一对多表格-一个用户发送多个-Post-Tweet"><a href="#对-index-主页进行修改,实现一对多表格-一个用户发送多个-Post-Tweet" class="headerlink" title="对 index 主页进行修改,实现一对多表格 (一个用户发送多个 Post Tweet)"></a>对 index 主页进行修改,实现一对多表格 (一个用户发送多个 Post Tweet)</h4><p><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/Index1.png" alt></p><ol><li><p>修改 index.html,引入 form 表格</p> <figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">{% extends "base.html" %}</span><br><span class="line"></span><br><span class="line">{% block app_content %}</span><br><span class="line"> <span class="tag"><<span class="name">h1</span>></span>Hello, {{ current_user.username }}<span class="tag"></<span class="name">h1</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">div</span> <span class="attr">class</span>=<span class="string">"row"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">div</span> <span class="attr">class</span>=<span class="string">"col-md-6"</span>></span></span><br><span class="line"> {% import 'bootstrap/wtf.html' as wtf %}</span><br><span class="line"> {{ wtf.quick_form(form) }}</span><br><span class="line"> <span class="tag"></<span class="name">div</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">div</span>></span></span><br><span class="line">{% endblock %}</span><br></pre></td></tr></table></figure></li><li><p>在 form.py 内增加 表单数据</p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">from wtforms import StringField, PasswordField, SubmitField, BooleanField, TextAreaField</span><br><span class="line">class PortTweetForm(FlaskForm):</span><br><span class="line"> # 文本框</span><br><span class="line"> text = TextAreaField('Say Something ....', validators=[DataRequired(), Length(min=1, max=40)])</span><br><span class="line"> submit = SubmitField('Post Text')</span><br></pre></td></tr></table></figure></li><li><p>删除旧的 app.db ,在 model.py 内 新建 Post 类用于存储发布的文本,并与 User 类中的数据库链接</p><p><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/Post_form.png" alt></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">from datetime import datetime</span><br><span class="line">class User(db.Model, UserMixin):</span><br><span class="line"> ...</span><br><span class="line"> # 第一个 'Post' 对于 class Post ; backref 返回的信息;'author' 数据库中存储 Post 进入数据的名称; lazy=True 如果不用就不连接</span><br><span class="line"> posts = db.relationship('Post', backref=db.backref('author', lazy=True))</span><br><span class="line"> ...</span><br><span class="line"></span><br><span class="line">class Post(db.Model):</span><br><span class="line"> id = db.Column(db.Integer, primary_key=True)</span><br><span class="line"> body = db.Column(db.String(140), nullable=False)</span><br><span class="line"> # 显示发布时间</span><br><span class="line"> timestamp = db.Column(db.DateTime, default=datetime.utcnow)</span><br><span class="line"> # 连接数据库中得到 user.id</span><br><span class="line"> user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)</span><br><span class="line"></span><br><span class="line"> def __repr__(self):</span><br><span class="line"> return '<Post {}>'.format(self.body)</span><br></pre></td></tr></table></figure></li><li><p>在 route.py 内 对 index 页面进行构造</p><p> <img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/Post1.png" alt></p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">from app.models import User, Post</span><br><span class="line">@app.route('/', methods=['GET', 'POST']) # 指定路由</span><br><span class="line"># 需要登录</span><br><span class="line">@login_required</span><br><span class="line">def index():</span><br><span class="line"> form = PortTweetForm()</span><br><span class="line"> if form.validate_on_submit():</span><br><span class="line"> body = form.text.data</span><br><span class="line"> # 将 post 发送到数据库</span><br><span class="line"> post = Post(body=body)</span><br><span class="line"> current_user.posts.append(post)</span><br><span class="line"> db.session.commit()</span><br><span class="line"> flash('You have post a new tweet.', category='success')</span><br><span class="line"> return render_template('index.html', form=form)</span><br></pre></td></tr></table></figure></li></ol><hr><h4 id="数据库的多对多关系-用户关注与取关"><a href="#数据库的多对多关系-用户关注与取关" class="headerlink" title="数据库的多对多关系 (用户关注与取关)"></a>数据库的多对多关系 (用户关注与取关)</h4><ol><li>先定义 数据库中的 关注/取关 与 User 联系 (models.py) <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br></pre></td><td class="code"><pre><span class="line"># 简单的示范性的 关注关系 (只包含关注者与被关注着),复杂的需要建立 class</span><br><span class="line">followers = db.Table("followers",</span><br><span class="line"> db.Column("follower_id", db.Integer, db.ForeignKey('user.id')),</span><br><span class="line"> db.Column("followed_id", db.Integer, db.ForeignKey('user.id'))</span><br><span class="line"> )</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">class User(db.Model, UserMixin):</span><br><span class="line"> # nullable 非空 ; unique 不能重复</span><br><span class="line"> id = db.Column(db.Integer, primary_key=True)</span><br><span class="line"> username = db.Column(db.String(20), unique=True, nullable=False)</span><br><span class="line"> password = db.Column(db.String(20), nullable=False)</span><br><span class="line"> email = db.Column(db.String(120), unique=True, nullable=False)</span><br><span class="line"> # 第一个 'Post' 对于 class Post ; backref 返回的信息;'author' 数据库中存储 Post 进入数据的名称; lazy=True 如果不用就不连接</span><br><span class="line"> posts = db.relationship('Post', backref=db.backref('author', lazy=True))</span><br><span class="line"> </span><br><span class="line"> # 'User': 关注者与被关注着链接是用户之间的连接; primaryjoin=(followers.c.follower_id==id) 左边的关注着与右边的关注者通过 id 相互连接</span><br><span class="line"> # 先正向连接,然后 backref 反向链接</span><br><span class="line"> followed = db.relationship(</span><br><span class="line"> 'User', secondary=followers,</span><br><span class="line"> primaryjoin=(followers.c.follower_id == id),</span><br><span class="line"> secondaryjoin=(followers.c.followed_id == id),</span><br><span class="line"> backref=db.backref('followers', lazy=True), lazy=True</span><br><span class="line"> )</span><br><span class="line"></span><br><span class="line"> def __repr__(self):</span><br><span class="line"> return '<User %r>' % self.username</span><br><span class="line"></span><br><span class="line"> def generate_reset_password_token(self):</span><br><span class="line"> # 将 token 与用户名 作为验证信息加密 传输</span><br><span class="line"> return jwt.encode({'id': self.id}, current_app.config['SECRET_KEY'], algorithm="HS256")</span><br><span class="line"></span><br><span class="line"> @staticmethod</span><br><span class="line"> def check_reset_password_token(token):</span><br><span class="line"> # 验证是加密的 验证信息 是否正确,是否遭到篡改</span><br><span class="line"> try:</span><br><span class="line"> data = jwt.decode(token, current_app.config['SECRET_KEY'], algorithm=["HS256"])</span><br><span class="line"> return User.query.filter_by(id=data['id']).first()</span><br><span class="line"> except:</span><br><span class="line"> return</span><br><span class="line"> </span><br><span class="line"> # 定义关注</span><br><span class="line"> def follow(self, user):</span><br><span class="line"> if not self.is_following(user):</span><br><span class="line"> self.followed.append(user)</span><br><span class="line"> </span><br><span class="line"> # 定义取关</span><br><span class="line"> def unfollow(self, user):</span><br><span class="line"> if self.is_following(user):</span><br><span class="line"> self.followed.remove(user)</span><br><span class="line"> </span><br><span class="line"> # 判断是否关注</span><br><span class="line"> def is_following(self, user):</span><br><span class="line"> # 从 followed 找到当前已经关注的,如果 >0 则已经关注</span><br><span class="line"> return self.followed.count(user) > 0</span><br></pre></td></tr></table></figure></li><li>对主页 index.html 进行修改 <figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"> <span class="tag"><<span class="name">div</span> <span class="attr">class</span>=<span class="string">"align-right"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">div</span> <span class="attr">class</span>=<span class="string">"thumbnail text-center"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">br</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">img</span> <span class="attr">src</span>=<span class="string">"{{ current_user.avatar_img }}"</span> <span class="attr">alt</span>=<span class="string">"avatar"</span> <span class="attr">width</span>=<span class="string">"100px"</span> ></span></span><br><span class="line"> <span class="tag"><<span class="name">div</span> <span class="attr">class</span>=<span class="string">"caption"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">h3</span>></span>{{ current_user.username }}<span class="tag"></<span class="name">h3</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">p</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">a</span> <span class="attr">href</span>=<span class="string">"#"</span> <span class="attr">class</span>=<span class="string">"btn btn-primary"</span> <span class="attr">role</span>=<span class="string">"button"</span>></span>{{ n_followers }} followers<span class="tag"></<span class="name">a</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">a</span> <span class="attr">href</span>=<span class="string">"#"</span> <span class="attr">class</span>=<span class="string">"btn btn-default"</span> <span class="attr">role</span>=<span class="string">"button"</span>></span>{{ n_followed }} followed<span class="tag"></<span class="name">a</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">p</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">div</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">div</span>></span></span><br><span class="line"><span class="tag"></<span class="name">div</span>></span></span><br></pre></td></tr></table></figure></li><li>在 models.py User 数据库中加入默认头像 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">class User(db.Model, UserMixin):</span><br><span class="line"> ...</span><br><span class="line"> avatar_img = db.Column(db.String(120), default='./static/asset/test.jpg', nullable=False)</span><br><span class="line"> ...</span><br></pre></td></tr></table></figure></li><li>在 route.py 中定义 index.html 中的 followers 与 followed <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">@app.route('/', methods=['GET', 'POST']) # 指定路由</span><br><span class="line"># 需要登录</span><br><span class="line">@login_required</span><br><span class="line">def index():</span><br><span class="line"> ...</span><br><span class="line"> n_followers = len(current_user.followers)</span><br><span class="line"> n_followed = len(current_user.followed)</span><br><span class="line"> return render_template('index.html', form=form, n_followers=n_followers, n_followed=n_followed)</span><br></pre></td></tr></table></figure><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/follower.png" alt></li></ol><hr><h4 id="在主页显示用户的发帖-使用-bootstrap-的-Media-heading"><a href="#在主页显示用户的发帖-使用-bootstrap-的-Media-heading" class="headerlink" title="在主页显示用户的发帖 (使用 bootstrap 的 Media heading)"></a>在主页显示用户的发帖 (使用 bootstrap 的 Media heading)</h4><p><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/index_post.png" alt></p><ol><li>在 index.html 中设置 Media heading 模块 <figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">{% for post in posts %}</span><br><span class="line"> <span class="tag"><<span class="name">div</span> <span class="attr">class</span>=<span class="string">"media"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">div</span> <span class="attr">class</span>=<span class="string">"media-left"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">a</span> <span class="attr">href</span>=<span class="string">"#"</span>></span></span><br><span class="line"> # 头像</span><br><span class="line"> <span class="tag"><<span class="name">img</span> <span class="attr">src</span>=<span class="string">"{{ post.author,avatar_img }}"</span> <span class="attr">alt</span>=<span class="string">"avatar"</span> <span class="attr">width</span>=<span class="string">"64px"</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">a</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">div</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">div</span> <span class="attr">class</span>=<span class="string">"media-body"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">h4</span> <span class="attr">class</span>=<span class="string">"media-heading"</span>></span>{{ post.author.username }}<span class="tag"></<span class="name">h4</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">small</span> <span class="attr">class</span>=<span class="string">"text-muted"</span>></span>{{ post.timestamp }}<span class="tag"></<span class="name">small</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">p</span>></span>{{ post.body }}<span class="tag"></<span class="name">p</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">div</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">div</span>></span></span><br><span class="line"> {% endfor %}</span><br></pre></td></tr></table></figure></li><li>在 route.py 中设置将 以倒序排列的 推文传入 index <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"># 取得 发布的内容 以时间倒序来排列显示</span><br><span class="line">posts = Post.query.order_by(Post.timestamp.desc()).all()</span><br></pre></td></tr></table></figure><h4 id="分页操作-主要在-index-html-内进行修改判断,在-route-py-内稍微修改"><a href="#分页操作-主要在-index-html-内进行修改判断,在-route-py-内稍微修改" class="headerlink" title="分页操作 (主要在 index.html 内进行修改判断,在 route.py 内稍微修改)"></a>分页操作 (主要在 index.html 内进行修改判断,在 route.py 内稍微修改)</h4><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/page_2.png" alt></li><li>route.py 定义 posts 便于 index.html 调用 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"># 取得 发布的内容 以时间倒序来排列显示</span><br><span class="line"> page = request.args.get('page', 1, type=int)</span><br><span class="line"> # paginate(page, 2, False): 返回页数,每页两个推文,默认超出后不会报错</span><br><span class="line"> posts = Post.query.order_by(Post.timestamp.desc()).paginate(page, 2, False)</span><br><span class="line"> return render_template('index.html', form=form, posts=posts, n_followers=n_followers, n_followed=n_followed)</span><br></pre></td></tr></table></figure></li><li>index.html 内定义 页面页数的变换 (Flask request 库的学习) <figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">nav</span> <span class="attr">aria-label</span>=<span class="string">"Page navigation"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">center</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">ul</span> <span class="attr">class</span>=<span class="string">"pagination"</span>></span></span><br><span class="line"> # 添加判断 </span><br><span class="line"> <span class="tag"><<span class="name">li</span> <span class="attr">class</span>=<span class="string">"{% if not posts.has_prev %}disabled{% endif %}"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">a</span> <span class="attr">href</span>=<span class="string">"{{ url_for('index', page=posts.prev_num) }}"</span> <span class="attr">aria-label</span>=<span class="string">"Previous"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">span</span> <span class="attr">aria-hidden</span>=<span class="string">"true"</span>></span><span class="symbol">&laquo;</span> Prev<span class="tag"></<span class="name">span</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">a</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">li</span>></span></span><br><span class="line"> {# posts.iter_page() 以当前页为中心显示左右页数 #}</span><br><span class="line"> {% for i in posts.iter_pages(right_current=3) %}</span><br><span class="line"> {% if i %}</span><br><span class="line"> {# 判断是当前页面然后颜色不同 为 active 样式 #}</span><br><span class="line"> <span class="tag"><<span class="name">li</span> <span class="attr">class</span>=<span class="string">"{% if i == posts.page %}active{% endif %}"</span>></span><span class="tag"><<span class="name">a</span> <span class="attr">href</span>=<span class="string">"{{ url_for("</span><span class="attr">index</span>", <span class="attr">page</span>=<span class="string">i)</span> }}"></span>{{ i }}<span class="tag"></<span class="name">a</span>></span> <span class="tag"></<span class="name">li</span>></span></span><br><span class="line"> {% else %}</span><br><span class="line"> <span class="tag"><<span class="name">li</span> <span class="attr">class</span>=<span class="string">"disabled"</span>></span><span class="tag"><<span class="name">a</span> <span class="attr">href</span>=<span class="string">"#"</span>></span>...<span class="tag"></<span class="name">a</span>></span> <span class="tag"></<span class="name">li</span>></span></span><br><span class="line"> {% endif %}</span><br><span class="line"> {% endfor %}</span><br><span class="line"> <span class="tag"><<span class="name">li</span> <span class="attr">class</span>=<span class="string">"{% if not posts.has_next %}disabled{% endif %}"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">a</span> <span class="attr">href</span>=<span class="string">"{{ url_for('index', page=posts.next_num) }}"</span> <span class="attr">aria-label</span>=<span class="string">"Next"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">span</span> <span class="attr">aria-hidden</span>=<span class="string">"true"</span>></span><span class="symbol">&raquo;</span> Next<span class="tag"></<span class="name">span</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">a</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">li</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">ul</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">center</span>></span></span><br><span class="line"></span><br><span class="line"><span class="tag"></<span class="name">nav</span>></span></span><br></pre></td></tr></table></figure></li></ol><hr><h4 id="编辑用户个人界面和关注取关操作"><a href="#编辑用户个人界面和关注取关操作" class="headerlink" title="编辑用户个人界面和关注取关操作"></a>编辑用户个人界面和关注取关操作</h4><p><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/unfollow.png" alt><br><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/follow.png" alt></p><ol><li>由于考虑到用户界面可能与主界面有 图片等部分重叠,从 index.html 内截取 Post 部分放入新建的 post_content.html 内,在 index.html 内引用 <figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line">{# 运用函数后返回的不是列表,需要 .Item 转换为列表 #}</span><br><span class="line">{% for post in posts.items %}</span><br><span class="line"> <span class="tag"><<span class="name">div</span> <span class="attr">class</span>=<span class="string">"media"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">div</span> <span class="attr">class</span>=<span class="string">"media-left"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">a</span> <span class="attr">href</span>=<span class="string">"{{ url_for('user_page', username=post.author.username) }}"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">img</span> <span class="attr">src</span>=<span class="string">"{{ post.author.avatar_img }}"</span> <span class="attr">alt</span>=<span class="string">"avatar"</span> <span class="attr">width</span>=<span class="string">"64px"</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">a</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">div</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">div</span> <span class="attr">class</span>=<span class="string">"media-body"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">h4</span> <span class="attr">class</span>=<span class="string">"media-heading"</span>></span>{{ post.author.username }}<span class="tag"></<span class="name">h4</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">small</span> <span class="attr">class</span>=<span class="string">"text-muted"</span>></span>{{ post.timestamp }}<span class="tag"></<span class="name">small</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">p</span>></span>{{ post.body }}<span class="tag"></<span class="name">p</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">div</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">div</span>></span></span><br><span class="line">{% endfor %}</span><br><span class="line">{# 页面跳转#}</span><br><span class="line"><span class="tag"><<span class="name">nav</span> <span class="attr">aria-label</span>=<span class="string">"Page navigation"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">center</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">ul</span> <span class="attr">class</span>=<span class="string">"pagination"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">li</span> <span class="attr">class</span>=<span class="string">"{% if not posts.has_prev %}disabled{% endif %}"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">a</span> <span class="attr">href</span>=<span class="string">"{{ url_for('index', page=posts.prev_num) }}"</span> <span class="attr">aria-label</span>=<span class="string">"Previous"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">span</span> <span class="attr">aria-hidden</span>=<span class="string">"true"</span>></span><span class="symbol">&laquo;</span> Prev<span class="tag"></<span class="name">span</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">a</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">li</span>></span></span><br><span class="line"> {# posts.iter_page() 以当前页为中心显示左右页数 #}</span><br><span class="line"> {% for i in posts.iter_pages(right_current=3) %}</span><br><span class="line"> {% if i %}</span><br><span class="line"> {# 判断是当前页面然后颜色不同 为 active 样式 #}</span><br><span class="line"> <span class="tag"><<span class="name">li</span> <span class="attr">class</span>=<span class="string">"{% if i == posts.page %}active{% endif %}"</span>></span><span class="tag"><<span class="name">a</span> <span class="attr">href</span>=<span class="string">"{{ url_for("</span><span class="attr">index</span>", <span class="attr">page</span>=<span class="string">i)</span> }}"></span>{{ i }}<span class="tag"></<span class="name">a</span>></span> <span class="tag"></<span class="name">li</span>></span></span><br><span class="line"> {% else %}</span><br><span class="line"> <span class="tag"><<span class="name">li</span> <span class="attr">class</span>=<span class="string">"disabled"</span>></span><span class="tag"><<span class="name">a</span> <span class="attr">href</span>=<span class="string">"#"</span>></span>...<span class="tag"></<span class="name">a</span>></span> <span class="tag"></<span class="name">li</span>></span></span><br><span class="line"> {% endif %}</span><br><span class="line"> {% endfor %}</span><br><span class="line"> <span class="tag"><<span class="name">li</span> <span class="attr">class</span>=<span class="string">"{% if not posts.has_next %}disabled{% endif %}"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">a</span> <span class="attr">href</span>=<span class="string">"{{ url_for('index', page=posts.next_num) }}"</span> <span class="attr">aria-label</span>=<span class="string">"Next"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">span</span> <span class="attr">aria-hidden</span>=<span class="string">"true"</span>></span><span class="symbol">&raquo;</span> Next<span class="tag"></<span class="name">span</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">a</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">li</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">ul</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">center</span>></span></span><br><span class="line"></span><br><span class="line"><span class="tag"></<span class="name">nav</span>></span></span><br></pre></td></tr></table></figure></li><li>在 route.py 内建立 个人信息,关注与取关 页面,并赋予功能 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line">@app.route('/user_page/<username>')</span><br><span class="line"> @login_required</span><br><span class="line"> def user_page(username):</span><br><span class="line"> user = User.query.filter_by(username=username).first()</span><br><span class="line"> if user:</span><br><span class="line"> page = request.args.get('page', 1, type=int)</span><br><span class="line"> posts = Post.query.filter_by(user_id=user.id).order_by(Post.timestamp.desc()).paginate(page, 2, False)</span><br><span class="line"> return render_template('user_page.html', user=user, posts=posts)</span><br><span class="line"> else:</span><br><span class="line"> return '404'</span><br><span class="line"> </span><br><span class="line"> @app.route('/follow/<username>', methods=['GET', 'POST'])</span><br><span class="line"> @login_required</span><br><span class="line"> def follow(username):</span><br><span class="line"> user = User.query.filter_by(username=username).first()</span><br><span class="line"> if user:</span><br><span class="line"> current_user.follow(user)</span><br><span class="line"> db.session.commit()</span><br><span class="line"> page = request.args.get('page', 1, type=int)</span><br><span class="line"> posts = Post.query.filter_by(user_id=user.id).order_by(Post.timestamp.desc()).paginate(page, 2, False)</span><br><span class="line"> return render_template('user_page.html', user=user, posts=posts)</span><br><span class="line"> else:</span><br><span class="line"> return '404'</span><br><span class="line"> </span><br><span class="line"> @app.route('/unfollow/<username>', methods=['GET', 'POST'])</span><br><span class="line"> @login_required</span><br><span class="line"> def unfollow(username):</span><br><span class="line"> user = User.query.filter_by(username=username).first()</span><br><span class="line"> if user:</span><br><span class="line"> current_user.unfollow(user)</span><br><span class="line"> db.session.commit()</span><br><span class="line"> page = request.args.get('page', 1, type=int)</span><br><span class="line"> posts = Post.query.filter_by(user_id=user.id).order_by(Post.timestamp.desc()).paginate(page, 2, False)</span><br><span class="line"> return render_template('user_page.html', user=user, posts=posts)</span><br><span class="line"> else:</span><br><span class="line"> return '404'</span><br></pre></td></tr></table></figure></li><li>建立个人信息界面 user_page.html <figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">{% extends 'base.html' %}</span><br><span class="line">{% block app_content %}</span><br><span class="line"> <span class="tag"><<span class="name">div</span> <span class="attr">class</span>=<span class="string">"row"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">div</span> <span class="attr">class</span>=<span class="string">"col-md-6"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">h1</span>></span>Hello, {{ current_user.username }}<span class="tag"></<span class="name">h1</span>></span></span><br><span class="line"> # 如果是用户正在观看自己的 个人信息,增加 填写信息 按钮</span><br><span class="line"> {% if current_user == user %}</span><br><span class="line"> <span class="tag"><<span class="name">a</span> <span class="attr">href</span>=<span class="string">"#"</span>></span>Edit Profile<span class="tag"></<span class="name">a</span>></span></span><br><span class="line"> # 如果用户正在看其他人的页面,添加 关注与取关 按钮</span><br><span class="line"> {% else %}</span><br><span class="line"> {% if current_user.is_following(user) %}</span><br><span class="line"> <span class="tag"><<span class="name">a</span> <span class="attr">href</span>=<span class="string">"{{ url_for("</span><span class="attr">unfollow</span>", <span class="attr">username</span>=<span class="string">user.username)</span> }}"></span>Unfollow<span class="tag"></<span class="name">a</span>></span></span><br><span class="line"> {% else %}</span><br><span class="line"> <span class="tag"><<span class="name">a</span> <span class="attr">href</span>=<span class="string">"{{ url_for("</span><span class="attr">follow</span>", <span class="attr">username</span>=<span class="string">user.username)</span> }}"></span>Follow<span class="tag"></<span class="name">a</span>></span></span><br><span class="line"> {% endif %}</span><br><span class="line"> {% endif %}</span><br><span class="line"> <span class="tag"><<span class="name">hr</span>></span></span><br><span class="line"> {% include "post_content.html" %}</span><br><span class="line"> <span class="tag"></<span class="name">div</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">div</span>></span></span><br><span class="line">{% endblock %}</span><br></pre></td></tr></table></figure></li></ol><hr><h4 id="Flask-上传文件-用来修改头像"><a href="#Flask-上传文件-用来修改头像" class="headerlink" title="Flask 上传文件 (用来修改头像)"></a>Flask 上传文件 (用来修改头像)</h4><p><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/upload_img.png" alt></p><ol><li>编写 上传文件 页面 edit_profile.html <figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">{% extends 'base.html' %}</span><br><span class="line"></span><br><span class="line">{% block app_content %}</span><br><span class="line"> <span class="tag"><<span class="name">h1</span>></span>Upload Your Avatar Image<span class="tag"></<span class="name">h1</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">br</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">div</span> <span class="attr">class</span>=<span class="string">"row"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">div</span> <span class="attr">class</span>=<span class="string">"col-md-6"</span>></span></span><br><span class="line"> {% import 'bootstrap/wtf.html' as wtf %}</span><br><span class="line"> {{ wtf.quick_form(form) }}</span><br><span class="line"> <span class="tag"></<span class="name">div</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">div</span>></span></span><br><span class="line"></span><br><span class="line">{% endblock %}</span><br></pre></td></tr></table></figure></li><li>建立 上传表单 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"># 上传文件使用 库</span><br><span class="line">from flask_wtf.file import FileField, FileRequired</span><br><span class="line">class UploadPhotoForm(FlaskForm):</span><br><span class="line"> photo =FileField(validators=[FileRequired()])</span><br><span class="line"> submit = SubmitField('Upload')</span><br></pre></td></tr></table></figure></li><li>在用户信息界面将 Edit Profile 链接到 上传页面 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><a href="{{ url_for("edit_profile") }}">Edit Profile</a></span><br></pre></td></tr></table></figure></li><li>在 route.py 完善 上传页面配置 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">import os</span><br><span class="line">from werkzeug.utils import secure_filename</span><br><span class="line">@app.route('/edit_profile', methods=['GET', 'POST'])</span><br><span class="line">def edit_profile():</span><br><span class="line"> form = UploadPhotoForm()</span><br><span class="line"> if form.validate_on_submit():</span><br><span class="line"> f = form.photo.data</span><br><span class="line"> # secure_filename 对用户上传的 软件名 进行再次包装,防止入侵</span><br><span class="line"> filename = secure_filename(f.filename)</span><br><span class="line"> if f.filename == "":</span><br><span class="line"> flash("No selected file", category="danger")</span><br><span class="line"> return render_template("edit_profile.html", form=form)</span><br><span class="line"> # 如果文件名是允许的后缀,可以进行操作</span><br><span class="line"> if f and allowed_file(f.filename):</span><br><span class="line"> # secure_filename 对用户上传的 软件名 进行再次包装,防止入侵</span><br><span class="line"> filename = secure_filename(f.filename)</span><br><span class="line"> # 定义上传图片保存位置</span><br><span class="line"> f.save(os.path.join('app', 'static', 'asset', filename))</span><br><span class="line"> # 将数据库中默认的头像转变为用户自定义头像</span><br><span class="line"> current_user.avatar_img = "/static/asset/" + filename</span><br><span class="line"> db.session.commit()</span><br><span class="line"> return redirect(url_for("user_page", username=current_user.username))</span><br><span class="line"> return render_template("edit_profile.html", form=form)</span><br></pre></td></tr></table></figure></li></ol>]]></content>
<tags>
<tag> Flask_Study </tag>
</tags>
</entry>
<entry>
<title>Flask_note1</title>
<link href="/2020/08/06/flask-note1/"/>
<url>/2020/08/06/flask-note1/</url>
<content type="html"><![CDATA[<p><a href="https://www.youtube.com/watch?v=RWviEK1Si68&list=PLDFBYdF-BxV1G4FBpG1EMyFtbsbZuJOvD" target="_blank" rel="noopener">根据Youtube上视频学习</a></p><h3 id="Flask-框架学习"><a href="#Flask-框架学习" class="headerlink" title="Flask 框架学习"></a>Flask 框架学习</h3><h4 id="通过-Pycharm-创建新的工程,创建-Falsk-项目"><a href="#通过-Pycharm-创建新的工程,创建-Falsk-项目" class="headerlink" title="通过 Pycharm 创建新的工程,创建 Falsk 项目"></a>通过 Pycharm 创建新的工程,创建 Falsk 项目</h4><p>生成的 Falsk 项目中带有一个 app.py 文件</p><p> <img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/project_build.png" alt><br> <img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/First_img.png" alt><br> <img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/First_startapp.png" alt><br> <img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/Flask_title1.png" alt></p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> flask <span class="keyword">import</span> Flask</span><br><span class="line"></span><br><span class="line">app = Flask(__name__)</span><br><span class="line"></span><br><span class="line"><span class="meta">@app.route('/') # 指定路由: '/' 根路由</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">hello_world</span><span class="params">()</span>:</span></span><br><span class="line"><span class="keyword">return</span> <span class="string">'Hello World!'</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</span><br><span class="line"><span class="comment"># debug 默认为 False, debug=True 让开发变得友好,在修改App 文件后,刷新立即可以在本地服务上看到变化</span></span><br><span class="line"><span class="comment"># windows 可能做不到</span></span><br><span class="line"><span class="comment"># port 默认端口 5000 ,port=6333 更改端口</span></span><br><span class="line"><span class="comment"># host 默认为本地 127.0.0.1, host='0.0.0.0'面向局域网都可以访问</span></span><br><span class="line"> app.run(debug=<span class="literal">True</span>, port=<span class="number">6333</span>, host=<span class="string">'0.0.0.0'</span>)</span><br></pre></td></tr></table></figure><h4 id="进行简单修改,对页面进行布局,渲染"><a href="#进行简单修改,对页面进行布局,渲染" class="headerlink" title="进行简单修改,对页面进行布局,渲染"></a>进行简单修改,对页面进行布局,渲染</h4><ol><li><p>基础:在 return 内增加 HTML 类型文件</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> flask <span class="keyword">import</span> Flask</span><br><span class="line"> </span><br><span class="line">app = Flask(__name__)</span><br><span class="line"> </span><br><span class="line"><span class="meta">@app.route('/') # 指定路由</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">hello_world</span><span class="params">()</span>:</span></span><br><span class="line"> <span class="comment"># 更改 return 内为 HTML 文件</span></span><br><span class="line"> <span class="keyword">return</span> <span class="string">'<h1>Hello</h1><p>Flask</p>'</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</span><br><span class="line"> app.run(debug=<span class="literal">True</span>)</span><br></pre></td></tr></table></figure></li><li><p>优化: 在工程下建立文件夹 “templates” (现在版本已经实现自建),”templates” 中建立 “index.html” 文件<br>在 app.py 内引进 Flask_note1 渲染<br><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/Flask_title3.png" alt></p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> flask <span class="keyword">import</span> Flask, render_template</span><br><span class="line">app = Flask(__name__)</span><br><span class="line"> </span><br><span class="line"><span class="meta">@app.route('/') # 指定路由</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">hello_world</span><span class="params">()</span>:</span></span><br><span class="line"> <span class="comment"># title=title 将 title 传入 index 作为标题</span></span><br><span class="line"> <span class="comment"># <title>{{ title }}</title></span></span><br><span class="line"> <span class="comment"># 写在 <p>{{ title }}</p> 内,也可以调用 填入 'Flask Web test'</span></span><br><span class="line"> title = <span class="string">'Flask Web test'</span></span><br><span class="line"> <span class="keyword">return</span> render_template(<span class="string">'index.html'</span>, title=title)</span><br><span class="line"> </span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</span><br><span class="line"> app.run(debug=<span class="literal">True</span>)</span><br></pre></td></tr></table></figure></li><li><p>更高级渲染 (条件判断、循环)</p><ul><li><p>条件判断:<br>如果 title 为空则默认 标题为 ‘Falsk App’<br><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/Flask_title4.png" alt><br><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/Flask_title5.png" alt></p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">{% if title %}</span><br><span class="line"> <title>{{ title }}</title></span><br><span class="line">{% else %}</span><br><span class="line"> <title>Falsk App</title></span><br><span class="line">{% endif %}</span><br></pre></td></tr></table></figure></li></ul></li></ol><pre><code> <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">hello_world</span><span class="params">()</span>:</span></span><br><span class="line"><span class="keyword">return</span> render_template(<span class="string">'index.html'</span>,</span><br><span class="line"> )</span><br></pre></td></tr></table></figure>+ 循环 建立三个 <p></p> ![](https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/Flask_p.png) <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">{% for p in data %}</span><br><span class="line"><p>{{ p }}</p></span><br><span class="line">{% endfor %}</span><br></pre></td></tr></table></figure> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">def hello_world():</span><br><span class="line"> title = 'Flask Web test'</span><br><span class="line"> paragraphs = [</span><br><span class="line"> "Selection 1",</span><br><span class="line"> "Selection 2",</span><br><span class="line"> "Selection 3"</span><br><span class="line"> ]</span><br><span class="line"> return render_template('index.html',</span><br><span class="line"> title=title,</span><br><span class="line"> data=paragraphs)</span><br></pre></td></tr></table></figure></code></pre><hr><h4 id="模板继承和引用"><a href="#模板继承和引用" class="headerlink" title="模板继承和引用"></a>模板继承和引用</h4><ol><li><p>模板的继承</p><ul><li><p>在 “templates” 下建立 “base.html” 作为模板<br><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/Flask_extends.png" alt></p> <figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta"><!DOCTYPE <span class="meta-keyword">html</span>></span></span><br><span class="line"><span class="tag"><<span class="name">html</span> <span class="attr">lang</span>=<span class="string">"en"</span>></span></span><br><span class="line"><span class="tag"><<span class="name">head</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"UTF-8"</span>></span></span><br><span class="line"> {% if title %}</span><br><span class="line"> <span class="tag"><<span class="name">title</span>></span>{{ title }}<span class="tag"></<span class="name">title</span>></span></span><br><span class="line"> {% else %}</span><br><span class="line"> <span class="tag"><<span class="name">title</span>></span>Falsk App<span class="tag"></<span class="name">title</span>></span></span><br><span class="line"> {% endif %}</span><br><span class="line"><span class="tag"></<span class="name">head</span>></span></span><br><span class="line"><span class="tag"><<span class="name">body</span>></span></span><br><span class="line"><span class="tag"><<span class="name">h3</span>></span><span class="tag"><<span class="name">a</span> <span class="attr">href</span>=<span class="string">"/"</span>></span>Flask App<span class="tag"></<span class="name">a</span>></span> <span class="tag"></<span class="name">h3</span>></span></span><br><span class="line"><span class="tag"><<span class="name">hr</span>></span></span><br><span class="line"><span class="tag"></<span class="name">body</span>></span></span><br><span class="line"><span class="tag"></<span class="name">html</span>></span></span><br></pre></td></tr></table></figure></li><li><p>在 “index.html” 中继承 “base.html” 模板</p> <figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">{% extends 'base.html' %}</span><br></pre></td></tr></table></figure></li><li><p>在 “index.html” 继承模板后,写入属于自己的东西<br> 在 “base.html” 文件相应位置写下 block 定义此自定义模块名称为 content</p> <figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta"><!DOCTYPE <span class="meta-keyword">html</span>></span></span><br><span class="line"><span class="tag"><<span class="name">html</span> <span class="attr">lang</span>=<span class="string">"en"</span>></span></span><br><span class="line"><span class="tag"><<span class="name">head</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"UTF-8"</span>></span></span><br><span class="line"> {% if title %}</span><br><span class="line"> <span class="tag"><<span class="name">title</span>></span>{{ title }}<span class="tag"></<span class="name">title</span>></span></span><br><span class="line"> {% else %}</span><br><span class="line"> <span class="tag"><<span class="name">title</span>></span>Falsk App<span class="tag"></<span class="name">title</span>></span></span><br><span class="line"> {% endif %}</span><br><span class="line"><span class="tag"></<span class="name">head</span>></span></span><br><span class="line"><span class="tag"><<span class="name">body</span>></span></span><br><span class="line"><span class="tag"><<span class="name">h3</span>></span><span class="tag"><<span class="name">a</span> <span class="attr">href</span>=<span class="string">"/"</span>></span>Flask App<span class="tag"></<span class="name">a</span>></span> <span class="tag"></<span class="name">h3</span>></span></span><br><span class="line"><span class="tag"><<span class="name">hr</span>></span></span><br><span class="line">{% block content %}</span><br><span class="line"> <span class="tag"><<span class="name">p</span>></span>Test<span class="tag"></<span class="name">p</span>></span></span><br><span class="line">{% endblock %}</span><br><span class="line"><span class="tag"></<span class="name">body</span>></span></span><br><span class="line"><span class="tag"></<span class="name">html</span>></span></span><br></pre></td></tr></table></figure></li><li><p>在 “index.html” 文件中 引用并更改</p> <figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">{% block content%}</span><br><span class="line"> <span class="tag"><<span class="name">p</span>></span>{{ title }}<span class="tag"></<span class="name">p</span>></span></span><br><span class="line">{% endblock %}</span><br></pre></td></tr></table></figure></li></ul></li><li><p>模板的引用 (导入,对于会在很多地方用到的文件引用)<br>模板模块中定义的会被,引用模板的模块重写掉,如果没有引用则会显示模板模块内的内容,常规情况下默认为空<br>可以把每一个模块(导航栏,报头)新建一个 html 文件 (一个个小组件) 并引入继承到模块<br> “navbar.html” 自定义报头</p> <figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">h3</span>></span><span class="tag"><<span class="name">a</span> <span class="attr">href</span>=<span class="string">"/"</span>></span>Flask App<span class="tag"></<span class="name">a</span>></span> <span class="tag"></<span class="name">h3</span>></span></span><br><span class="line"><span class="tag"><<span class="name">hr</span>></span></span><br></pre></td></tr></table></figure><p> “base.html” 引入</p> <figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">{% include 'navbar.html' %}</span><br></pre></td></tr></table></figure></li></ol><hr><h4 id="了解-flask-bootstrap-框架中已经定义好许多-class-的命名、风格-使界面布局更加好看,将布局封装到Flask的插件"><a href="#了解-flask-bootstrap-框架中已经定义好许多-class-的命名、风格-使界面布局更加好看,将布局封装到Flask的插件" class="headerlink" title="了解 flask-bootstrap (框架中已经定义好许多 class 的命名、风格) 使界面布局更加好看,将布局封装到Flask的插件"></a>了解 flask-bootstrap (框架中已经定义好许多 class 的命名、风格) 使界面布局更加好看,将布局封装到Flask的插件</h4><p>可以通过定义 class 的值定义组件位于 页面中的位置<br><a href="https://getbootstrap.com/" target="_blank" rel="noopener">Flask—bootstrap 重点学习官网</a><br> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">pip install flask-bootstrap</span><br><span class="line">from flask import Flask, render_template</span><br><span class="line">from flask_bootstrap import Bootstrap</span><br><span class="line"> </span><br><span class="line">app = Flask(__name__)</span><br><span class="line">bootstrap = Bootstrap(app)</span><br></pre></td></tr></table></figure></p><ol><li><p>HTML 中引用 bootstrap 从 bootstrap 的 base.html 中的库内引用</p><p> <a href="https://getbootstrap.com/docs/3.3/components/#navbar" target="_blank" rel="noopener">Boot-straap 官方提供的模板</a></p> <figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">{% extends "bootstrap/base.html" %}</span><br><span class="line">{% block title %}This is an example page{% endblock %}</span><br><span class="line">{% block navbar %}</span><br><span class="line">{# {% include 'navbar.html' %}#}</span><br><span class="line">{% endblock %}</span><br><span class="line"></span><br><span class="line">{% block content %}</span><br><span class="line"> <span class="tag"><<span class="name">h1</span>></span>Hello, Bootstrap<span class="tag"></<span class="name">h1</span>></span></span><br><span class="line">{% endblock %}</span><br></pre></td></tr></table></figure></li><li><p>重写 bootstrap 里面的类,使之满足自己的需求<br>在 <strong>“External Libraries -> site-packages -> flask_bootstrap -> templates -> bootstrap”</strong> 内修改内置的各种页面,或者,复制出来在自己的页面中引用修改</p></li><li><p>创建新的页面 (以点击形式进入新的连接)</p><ul><li>app.py<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">@app.route('/register')</span><br><span class="line">def register():</span><br><span class="line"> # 转到 regist.html 页面</span><br><span class="line"> return render_template('register.html')</span><br></pre></td></tr></table></figure></li><li>register.html<br>引用 “base.html” 模板 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">{% extends 'base.html' %}</span><br><span class="line"></span><br><span class="line">{% block app_content %}</span><br><span class="line"> <h1>Register Now</h1></span><br><span class="line">{% endblock %}</span><br></pre></td></tr></table></figure></li><li>base.html<br>引用 navbar.html 页面<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">{% block navbar %}</span><br><span class="line"> {% include 'navbar.html' %}</span><br><span class="line">{% endblock %}</span><br><span class="line"></span><br><span class="line">{% block content %}</span><br><span class="line"> <div class="container"></span><br><span class="line"> {% block app_content %}</span><br><span class="line"> {% endblock %}</span><br><span class="line"> </div></span><br><span class="line">{% endblock %}</span><br></pre></td></tr></table></figure></li><li>navbar.html.html<br>设置页面共有文件<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><ul class="nav navbar-nav navbar-right"></span><br><span class="line"> {# 只有当 request.endpoint 是 register 时 才是 active,否则是其他 #}</span><br><span class="line"> <li class="{% if request.endpoint == 'register' %}active{% endif %}"></span><br><span class="line"> {# 当点击 Regist 时 ,转到 register() 函数 ,由 render_template(register.html) 转到 register.html #}</span><br><span class="line"> <a href="{{ url_for('register') }}">Register</a></span><br><span class="line"> </li></span><br><span class="line"></ul></span><br></pre></td></tr></table></figure></li></ul></li></ol><hr><h4 id="Flask-连接数据库、-Flask-mine-找回密码、Flask-登录-采用关系数据库,Flask提供flask-sqlalchemy连接数据库"><a href="#Flask-连接数据库、-Flask-mine-找回密码、Flask-登录-采用关系数据库,Flask提供flask-sqlalchemy连接数据库" class="headerlink" title="Flask 连接数据库、 Flask mine 找回密码、Flask 登录 (采用关系数据库,Flask提供flask-sqlalchemy连接数据库)"></a>Flask 连接数据库、 Flask mine 找回密码、Flask 登录 (采用关系数据库,Flask提供flask-sqlalchemy连接数据库)</h4><p><a href="https://flask-sqlalchemy.palletsprojects.com/en/2.x/" target="_blank" rel="noopener">flask_sqlalchemy 重点学习官方文档地址</a></p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> flask <span class="keyword">import</span> Flask, render_template</span><br><span class="line"><span class="keyword">from</span> flask_bootstrap <span class="keyword">import</span> Bootstrap</span><br><span class="line"><span class="keyword">from</span> flask_sqlalchemy <span class="keyword">import</span> SQLAlchemy</span><br><span class="line"></span><br><span class="line">app = Flask(__name__)</span><br><span class="line"><span class="comment"># 给出的是本地的 SQLite 数据库链接地址,可以改为 MySQL 等,只需要改变app.config['SQLALCHEMY_DATABASE_URI'] 值</span></span><br><span class="line">app.config[<span class="string">'SQLALCHEMY_DATABASE_URI'</span>] = <span class="string">'sqlite:////tmp/test.db'</span></span><br><span class="line">db = SQLAlchemy(app)</span><br><span class="line">bootstrap = Bootstrap(app)</span><br></pre></td></tr></table></figure><ol><li>为了方便管理,建议将 SQL 配置新建一个 config.py 用于整理配置文件<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> os</span><br><span class="line"> </span><br><span class="line">basedir = os.path.abspath(os.path.dirname(__file__))</span><br><span class="line"> </span><br><span class="line"> </span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Config</span><span class="params">(object)</span>:</span></span><br><span class="line"> <span class="comment"># 如果找到 SQLite 路径就是用,如果没有找到就新建一个</span></span><br><span class="line"> SQLALCHEMY_DATABASE_URI = os.environ.get(<span class="string">'DATABASE_URL'</span>) <span class="keyword">or</span> <span class="string">"sqlite:///"</span> + os.path.join(basedir, <span class="string">'app.db'</span>)</span><br><span class="line"> SQLALCHEMY_TRACK_MODIFICATIONS = <span class="literal">False</span></span><br></pre></td></tr></table></figure></li><li>建立 models.py 存放构建数据库的结构 <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> app <span class="keyword">import</span> db</span><br><span class="line"> </span><br><span class="line"> </span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">User</span><span class="params">(db.Model)</span>:</span></span><br><span class="line"> <span class="comment"># nullable 非空 ; unique 不能重复,出现相同就报错</span></span><br><span class="line"> id = db.Column(db.Integer, primary_key=<span class="literal">True</span>)</span><br><span class="line"> username = db.Column(db.String(<span class="number">20</span>), unique=<span class="literal">True</span>, nullable=<span class="literal">False</span>)</span><br><span class="line"> password = db.Column(db.String(<span class="number">20</span>), nullable=<span class="literal">False</span>)</span><br><span class="line"> email = db.Column(db.String(<span class="number">120</span>), unique=<span class="literal">True</span>, nullable=<span class="literal">False</span>)</span><br><span class="line"> </span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">__repr__</span><span class="params">(self)</span>:</span></span><br><span class="line"> <span class="keyword">return</span> <span class="string">'<User %r>'</span> % self.username</span><br></pre></td></tr></table></figure></li><li>要创建初始数据库,需要在 “Python Console” (3、4、5都在 Python Console 中执行) 导入对象并运行 “SQLAlchemy.create_all” 方法 <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> app.models <span class="keyword">import</span> db</span><br><span class="line">db.create_all()</span><br></pre></td></tr></table></figure></li><li>创建用户用于测试 <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> app.models <span class="keyword">import</span> User</span><br><span class="line"><span class="keyword">from</span> app.models <span class="keyword">import</span> db</span><br><span class="line">user1 = User(username=<span class="string">""</span>, password=<span class="string">""</span>, email=<span class="string">""</span>)</span><br><span class="line"><span class="comment"># 添加进数据库,并运行指令</span></span><br><span class="line">db.session.add(user1)</span><br><span class="line">db.session.commit()</span><br></pre></td></tr></table></figure></li><li>访问数据库中数据 <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> app.models <span class="keyword">import</span> User</span><br><span class="line">u = User.query.all()</span><br><span class="line"><span class="comment"># [<User 'Jack'>]</span></span><br><span class="line">U = u[<span class="number">0</span>]</span><br><span class="line">U.password</span><br><span class="line"><span class="comment"># 'pwd'</span></span><br></pre></td></tr></table></figure></li></ol><hr><h4 id="FlaskWTF-创建-Flask-中的表单,基本包含所有表单"><a href="#FlaskWTF-创建-Flask-中的表单,基本包含所有表单" class="headerlink" title="FlaskWTF 创建 Flask 中的表单,基本包含所有表单"></a>FlaskWTF 创建 Flask 中的表单,基本包含所有表单</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">下载:</span><br><span class="line">1. pip install flask-WTF</span><br><span class="line">2. pip install wtforms</span><br></pre></td></tr></table></figure><p><a href="https://flask-wtf.readthedocs.io/en/stable/" target="_blank" rel="noopener">FlaskWTF 重点官网主页</a><br><a href="https://pythonhosted.org/Flask-Bootstrap/" target="_blank" rel="noopener">Flask Bootstrap 与 其他组件联系内容包括 WTF</a></p><ol><li>创建表单 forms.py (用于注册,规定注册页面的内容) <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># FlaskForm 主要负责整合 wtforms 的内容、类型 以及包装</span></span><br><span class="line"><span class="keyword">from</span> flask_wtf <span class="keyword">import</span> FlaskForm</span><br><span class="line"><span class="comment"># 定义数据类型字符串,密码,提交按钮</span></span><br><span class="line"><span class="keyword">from</span> wtforms <span class="keyword">import</span> StringField, PasswordField, SubmitField</span><br><span class="line"><span class="comment"># validators 验证者:需要的数据、数据范围Length(min=6, max=20)、Email、验证密码 EqualTo('password')</span></span><br><span class="line"><span class="keyword">from</span> wtforms.validators <span class="keyword">import</span> DataRequired, Length, Email, EqualTo</span><br><span class="line"> </span><br><span class="line"> </span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">RegisterForm</span><span class="params">(FlaskForm)</span>:</span></span><br><span class="line"> username = StringField(<span class="string">'Username'</span>, validators=[DataRequired(), Length(min=<span class="number">6</span>, max=<span class="number">20</span>)])</span><br><span class="line"> email = StringField(<span class="string">'Email'</span>, validators=[DataRequired(), Email()])</span><br><span class="line"> password = PasswordField(<span class="string">'Password'</span>, validators=[DataRequired(), Length(min=<span class="number">8</span>, max=<span class="number">20</span>)])</span><br><span class="line"> confirm = PasswordField(<span class="string">'Repeat Password'</span>, validators=[DataRequired(), EqualTo(<span class="string">'password'</span>)])</span><br><span class="line"> submit = SubmitField(<span class="string">'Register'</span>)</span><br></pre></td></tr></table></figure></li><li>使用 Email类型 时需要下载支持 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">pip install email_validator</span><br></pre></td></tr></table></figure>使用 FlaskWTF 需要秘钥设置, CSRF token 验证 每次随机生成</li></ol><p><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/csrf_token.png" alt></p><ul><li>在 config.py 内设置 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"> # SECRET_KEY 秘钥为路径内或者 字符串 'A-VERY-LONG-SECRET' 可以随意设置,主要为了防止跨网站攻击</span><br><span class="line">SECRET_KEY = os.environ.get('SECRET_KEY') or 'A-VERY-LONG-SECRET'</span><br></pre></td></tr></table></figure></li><li>在 app.py 主页设置 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">app.secret_key = '123456'</span><br></pre></td></tr></table></figure><ol start="3"><li>定义 app.py 内注册页面<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> flask <span class="keyword">import</span> Flask, render_template, request, url_for</span><br><span class="line"><span class="keyword">from</span> flask_bootstrap <span class="keyword">import</span> Bootstrap</span><br><span class="line"><span class="keyword">from</span> flask_sqlalchemy <span class="keyword">import</span> SQLAlchemy</span><br><span class="line"> </span><br><span class="line"><span class="keyword">from</span> config <span class="keyword">import</span> Config</span><br><span class="line"><span class="comment"># 引用 form 内的 注册表单 RegisterForm</span></span><br><span class="line"><span class="keyword">from</span> app.forms <span class="keyword">import</span> RegisterForm</span><br><span class="line"> </span><br><span class="line">app = Flask(__name__)</span><br><span class="line">db = SQLAlchemy(app)</span><br><span class="line">bootstrap = Bootstrap(app)</span><br><span class="line"> </span><br><span class="line">app.config.from_object(Config)</span><br><span class="line"><span class="comment"># 设置 传递数据的方法为 "GET" 与 "POST"</span></span><br><span class="line"><span class="meta">@app.route('/register', methods=['GET', 'POST'])</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">register</span><span class="params">()</span>:</span></span><br><span class="line"> <span class="comment"># 调用 RegisterForm 表单定义</span></span><br><span class="line"> form = RegisterForm()</span><br><span class="line"> <span class="comment"># 定义如果点击后 pass</span></span><br><span class="line"> <span class="keyword">if</span> form.validate_on_submit():</span><br><span class="line"> <span class="keyword">pass</span></span><br><span class="line"> <span class="comment"># 将表单传入 register,html</span></span><br><span class="line"> <span class="keyword">return</span> render_template(<span class="string">'register.html'</span>, form=form)</span><br></pre></td></tr></table></figure></li><li>定义 “register.html” : 引用 bootstrap 内的 wtf.html 内容<br>WTF 内部包含 表单提交的各种报错,验证等<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">{% extends 'base.html' %}</span><br><span class="line"></span><br><span class="line">{% block app_content %}</span><br><span class="line"> <span class="tag"><<span class="name">h1</span>></span>Register Now<span class="tag"></<span class="name">h1</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">br</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">div</span> <span class="attr">class</span>=<span class="string">"row"</span>></span></span><br><span class="line"> {# col-md-6 表单大小 #}</span><br><span class="line"> <span class="tag"><<span class="name">div</span> <span class="attr">class</span>=<span class="string">"col-md-6"</span>></span></span><br><span class="line"> {# 引用 bootstrap 内的 wtf.html 内容 #}</span><br><span class="line"> {% import 'bootstrap/wtf.html' as wtf %}</span><br><span class="line"> {# 重点 一句话获取表单 form.py 的设置,快速完成表单,而无需进行大量的微调 #}</span><br><span class="line"> {{ wtf.quick_form(form) }}</span><br><span class="line"> <span class="tag"></<span class="name">div</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">div</span>></span></span><br><span class="line"></span><br><span class="line">{% endblock %}</span><br></pre></td></tr></table></figure></li><li>额外: 人机验证 RECAPTCHA PUBLIC KEY 验证秘钥,验证是否为机器<br><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/PUBLIC_KEY.png" alt></li></ol><ul><li>forms.py 添加验证 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">from flask_wtf import FlaskForm, RecaptchaField</span><br><span class="line">recaptcha = RecaptchaField()</span><br></pre></td></tr></table></figure></li><li>config.py 注册秘钥 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"> # RECAPTCHA PUBLIC KEY 验证秘钥,验证是否为机器人</span><br><span class="line">RECAPTCHA_PUBLIC_KEY = os.environ.get('RECAPTCHA_PUBLIC_KEY') or 'A-VERY-LONG-PUBLIC-KEY'</span><br><span class="line"> RECAPTCHA_PRIVATE_KEY = os.environ.get('RECAPTCHA_PRIVATE_KEY') or 'A-VERY-LONG-PRIVATE_KEY'</span><br></pre></td></tr></table></figure></li><li>遇到报错 “需要网站所有者处理的错误:网站密钥无效”,需要自行注册页面</li></ul></li></ul><hr><h4 id="Flask-Bcrypt-加密数据项-,在服务端处理表单存入数据库"><a href="#Flask-Bcrypt-加密数据项-,在服务端处理表单存入数据库" class="headerlink" title="Flask-Bcrypt 加密数据项 ,在服务端处理表单存入数据库"></a>Flask-Bcrypt 加密数据项 ,在服务端处理表单存入数据库</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">pip install flask-bcrypt</span><br></pre></td></tr></table></figure><ol><li><p>加密数据项 “flask-bcrypy” 中的 “Bcrypy” 模块, 传入数据库</p><p> 为了避免与 congig.py 内的引用构成 bug 新建 名为”app” 的 package 将除了数据库、config.py移入, 将 app.py 分为 <strong>init</strong>.py 与 route.py,在外部创建与 config.py 同级的 run.py<br> <img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/get_data1.png" alt><br> <img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/get_data2.png" alt></p><ul><li>__init__.py <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> flask <span class="keyword">import</span> Flask</span><br><span class="line"><span class="keyword">from</span> flask_bootstrap <span class="keyword">import</span> Bootstrap</span><br><span class="line"><span class="keyword">from</span> flask_sqlalchemy <span class="keyword">import</span> SQLAlchemy</span><br><span class="line"><span class="comment"># 调用模块</span></span><br><span class="line"><span class="keyword">from</span> flask_bcrypt <span class="keyword">import</span> Bcrypt</span><br><span class="line"> </span><br><span class="line"><span class="keyword">from</span> config <span class="keyword">import</span> Config</span><br><span class="line"> </span><br><span class="line"> </span><br><span class="line">app = Flask(__name__)</span><br><span class="line">db = SQLAlchemy(app)</span><br><span class="line"><span class="comment"># 定义模块</span></span><br><span class="line">bcrypt = Bcrypt(app)</span><br><span class="line">bootstrap = Bootstrap(app)</span><br><span class="line"> </span><br><span class="line">app.config.from_object(Config)</span><br><span class="line"><span class="keyword">from</span> app.route <span class="keyword">import</span> *</span><br></pre></td></tr></table></figure></li><li>route.py <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> flask <span class="keyword">import</span> render_template, flash</span><br><span class="line"> </span><br><span class="line"><span class="keyword">from</span> app <span class="keyword">import</span> app, bcrypt, db</span><br><span class="line"><span class="keyword">from</span> app.forms <span class="keyword">import</span> RegisterForm</span><br><span class="line"><span class="keyword">from</span> app.models <span class="keyword">import</span> User</span><br><span class="line"> </span><br><span class="line"> </span><br><span class="line"><span class="meta">@app.route('/') # 指定路由</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">index</span><span class="params">()</span>:</span></span><br><span class="line"> <span class="keyword">return</span> render_template(<span class="string">'index.html'</span>)</span><br><span class="line"> </span><br><span class="line"> </span><br><span class="line"><span class="meta">@app.route('/register', methods=['GET', 'POST'])</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">register</span><span class="params">()</span>:</span></span><br><span class="line"> form = RegisterForm()</span><br><span class="line"> <span class="comment"># 获取提交上来的注册数据,进行处理</span></span><br><span class="line"> <span class="keyword">if</span> form.validate_on_submit():</span><br><span class="line"> username = form.username.data</span><br><span class="line"> email = form.email.data</span><br><span class="line"> <span class="comment"># 变种hash加密,相同密码生成值也不同</span></span><br><span class="line"> password = bcrypt.generate_password_hash(form.password.data)</span><br><span class="line"> <span class="comment"># 传入数据库内</span></span><br><span class="line"> user = User(username=username, email=email, password=password)</span><br><span class="line"> db.session.add(user)</span><br><span class="line"> db.session.commit()</span><br><span class="line"> <span class="comment"># 检查产生密码与hash 是否对应正确</span></span><br><span class="line"> <span class="comment"># bcrypt.check_password_hash(hash, password)</span></span><br><span class="line"> <span class="keyword">return</span> render_template(<span class="string">'register.html'</span>, form=form)</span><br></pre></td></tr></table></figure></li><li>run.py <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> app <span class="keyword">import</span> app</span><br><span class="line"> </span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</span><br><span class="line"> app.run(debug=<span class="literal">True</span>)</span><br></pre></td></tr></table></figure></li></ul></li><li><p>在设置 “models.py” 数据库时,设置 “unique=True 不能重复”,遇见重复的就会报错,需要写函数来辅助判断用户名和密码是否在数据库中,然后给与提示</p><ul><li>forms.py 判断是否在数据库中重复,如果在给与报错<br> <img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/Username_taken.png" alt><br> <img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/Email_token.png" alt> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">from wtforms.validators import DataRequired, Length, Email, EqualTo, ValidationError</span><br><span class="line">class RegisterForm(FlaskForm):</span><br><span class="line"> def validate_username(self, username):</span><br><span class="line"> user = User.query.filter_by(username=username.data).first()</span><br><span class="line"> if user:</span><br><span class="line"> raise ValidationError('Username already token, please choose another one.')</span><br><span class="line"> </span><br><span class="line"> def validate_email(self, email):</span><br><span class="line"> email = User.query.filter_by(email=email.data).first()</span><br><span class="line"> if email:</span><br><span class="line"> raise ValidationError('Email already token, please choose another one.')</span><br></pre></td></tr></table></figure></li><li>route.py (flash提示注册成功,并跳转页面) <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> flask <span class="keyword">import</span> render_template, flash, redirect, url_for</span><br><span class="line"> </span><br><span class="line"><span class="keyword">from</span> app <span class="keyword">import</span> app, bcrypt, db </span><br><span class="line"><span class="keyword">from</span> app.forms <span class="keyword">import</span> RegisterForm </span><br><span class="line"><span class="keyword">from</span> app.models <span class="keyword">import</span> User</span><br><span class="line"> </span><br><span class="line"> </span><br><span class="line"><span class="meta">@app.route('/') # 指定路由</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">index</span><span class="params">()</span>:</span></span><br><span class="line"> <span class="keyword">return</span> render_template(<span class="string">'index.html'</span>)</span><br><span class="line"> </span><br><span class="line"> </span><br><span class="line"><span class="meta">@app.route('/register', methods=['GET', 'POST'])</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">register</span><span class="params">()</span>:</span></span><br><span class="line"> form = RegisterForm()</span><br><span class="line"> <span class="comment"># 获取提交上来的注册数据,进行处理</span></span><br><span class="line"> <span class="keyword">if</span> form.validate_on_submit():</span><br><span class="line"> username = form.username.data</span><br><span class="line"> email = form.email.data</span><br><span class="line"> <span class="comment"># 变种hash加密,相同密码生成值也不同</span></span><br><span class="line"> password = bcrypt.generate_password_hash(form.password.data)</span><br><span class="line"> user = User(username=username, email=email, password=password)</span><br><span class="line"> db.session.add(user)</span><br><span class="line"> db.session.commit()</span><br><span class="line"> <span class="comment"># flash 提示 "用户注册成功",提示信息为 "success" 样式</span></span><br><span class="line"> flash(<span class="string">'Congrats registration success'</span>, category=<span class="string">'success'</span>)</span><br><span class="line"> <span class="comment"># 提示后转至 index.html 页面</span></span><br><span class="line"> <span class="keyword">return</span> redirect(url_for(<span class="string">'index'</span>))</span><br><span class="line"> <span class="comment"># 检查产生密码与hash 是否对应正确</span></span><br><span class="line"> <span class="comment"># bcrypt.check_password_hash(hash, password)</span></span><br><span class="line"> <span class="keyword">return</span> render_template(<span class="string">'register.html'</span>, form=form)</span><br></pre></td></tr></table></figure></li><li>base.html (修改 base.html 页面,确保 flash 成功显示在页面上)<br> <img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/flash_show.png" alt><br> <img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/flash_return.png" alt> <figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">{% block content %}</span><br><span class="line"> <span class="tag"><<span class="name">div</span> <span class="attr">class</span>=<span class="string">"container"</span>></span></span><br><span class="line"> {# 提示框 #}</span><br><span class="line"> <span class="tag"><<span class="name">div</span> <span class="attr">class</span>=<span class="string">"row"</span>></span></span><br><span class="line"> {# 页面大小 #}</span><br><span class="line"> <span class="tag"><<span class="name">div</span> <span class="attr">class</span>=<span class="string">"col-lg-6"</span>></span></span><br><span class="line"> {# 判断与循环 #}</span><br><span class="line"> {% with messages = get_flashed_messages(with_categories=True) %}</span><br><span class="line"> {% if messages %}</span><br><span class="line"> {% for category, message in messages %}</span><br><span class="line"> <span class="tag"><<span class="name">div</span> <span class="attr">class</span>=<span class="string">"alert alert-{{ category }}"</span>></span></span><br><span class="line"> {{ message }}</span><br><span class="line"> <span class="tag"></<span class="name">div</span>></span></span><br><span class="line"> {% endfor %}</span><br><span class="line"> {% endif %}</span><br><span class="line"> {% endwith %}</span><br><span class="line"> </span><br><span class="line"> <span class="tag"></<span class="name">div</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">div</span>></span></span><br><span class="line"> {# 在 app_content 注册 上添加 注册成功 提示 #}</span><br><span class="line"> {% block app_content %}</span><br><span class="line"> {% endblock %}</span><br><span class="line"> <span class="tag"></<span class="name">div</span>></span></span><br><span class="line">{% endblock %}</span><br></pre></td></tr></table></figure></li></ul></li></ol><hr><h4 id="Flask-login-用户登录包,包含很多用于登录、登出的函数"><a href="#Flask-login-用户登录包,包含很多用于登录、登出的函数" class="headerlink" title="Flask-login 用户登录包,包含很多用于登录、登出的函数"></a>Flask-login 用户登录包,包含很多用于登录、登出的函数</h4><p><code>pip install flask-login</code></p><ol><li><p>在 __init__.py 内引入 flask-login 包,并进行设置</p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">from flask_login import LoginManager</span><br><span class="line"></span><br><span class="line"># 登录界面的位置</span><br><span class="line">login.login_view = 'login'</span><br><span class="line"># 提示 "You must login to access the page" 提示框格式为 "info"</span><br><span class="line">login.login_message = "You must login to access the page"</span><br><span class="line">login.login_message_category = "info"</span><br></pre></td></tr></table></figure></li><li><p>新建 login.html 登录页面,在 forms.py 下 新建 Login 函数,构造 登录需要的部件<br> <img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/login.png" alt></p> <figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">{% extends 'base.html' %}</span><br><span class="line"></span><br><span class="line">{% block app_content %}</span><br><span class="line"> <span class="tag"><<span class="name">h1</span>></span>Login In<span class="tag"></<span class="name">h1</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">br</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">div</span> <span class="attr">class</span>=<span class="string">"row"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">div</span> <span class="attr">class</span>=<span class="string">"col-md-6"</span>></span></span><br><span class="line"> {% import 'bootstrap/wtf.html' as wtf %}</span><br><span class="line"> {{ wtf.quick_form(form) }}</span><br><span class="line"> <span class="tag"></<span class="name">div</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">div</span>></span></span><br><span class="line"></span><br><span class="line">{% endblock %}</span><br></pre></td></tr></table></figure><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">from wtforms import StringField, PasswordField, SubmitField, BooleanField</span><br><span class="line">class LoginForm(FlaskForm):</span><br><span class="line"> username = StringField('Username', validators=[DataRequired(), Length(min=6, max=20)])</span><br><span class="line"> password = PasswordField('Password', validators=[DataRequired(), Length(min=8, max=20)])</span><br><span class="line"> remember = BooleanField('Remember')</span><br><span class="line"> submit = SubmitField('Sign In')</span><br></pre></td></tr></table></figure></li><li><p>修改 navbar.html 在主页面新增 “Login” 、”Logout” 选项框,</p><p>增加判断项,在登录后只显示 “Logout” 选项,未登录时显示 “Login” 与 “Register” 选项,</p><p>修改 主页面 (index.html) 显示为 Hello, <br><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/logout.png" alt><br><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/if_not_login.png" alt><br><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/if_login.png" alt></p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><ul class="nav navbar-nav navbar-right"></span><br><span class="line"> {# 判断是否处于登录状态,current_user.is_authenticated 获取登录状态 #}</span><br><span class="line"> {% if not current_user.is_authenticated %}</span><br><span class="line"> {# 登录界面 #}</span><br><span class="line"> <li class="{% if request.endpoint == 'login' %}active{% endif %}"></span><br><span class="line"> <a href="{{ url_for('login') }}">Login In</a></span><br><span class="line"> </li></span><br><span class="line"> {# 只有当 request.endpoint 是 index 时 才是 active,否则是其他 #}</span><br><span class="line"> <li class="{% if request.endpoint == 'register' %}active{% endif %}"></span><br><span class="line"> <a href="{{ url_for('register') }}">Register</a></span><br><span class="line"> </li></span><br><span class="line"> {% else %}</span><br><span class="line"> {# 登出界面 #}</span><br><span class="line"> <li class="{% if request.endpoint == 'logout' %}active{% endif %}"></span><br><span class="line"> <a href="{{ url_for('logout') }}">Logout</a></span><br><span class="line"> </li></span><br><span class="line"> {% endif %}</span><br><span class="line"> </ul></span><br></pre></td></tr></table></figure><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">{% extends "base.html" %}</span><br><span class="line"></span><br><span class="line">{% block app_content %}</span><br><span class="line"><span class="tag"><<span class="name">h1</span>></span>Hello, {{ current_user.username }}<span class="tag"></<span class="name">h1</span>></span></span><br><span class="line">{% endblock %}</span><br></pre></td></tr></table></figure></li><li><p>修改数据库设置 model.py,删除数据库重新构建数据库结构</p> <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 引用 UserMinxin</span></span><br><span class="line"><span class="keyword">from</span> flask_login <span class="keyword">import</span> UserMixin</span><br><span class="line"><span class="comment"># 从 app 包的 __init__.py 引用 login </span></span><br><span class="line"><span class="keyword">from</span> app <span class="keyword">import</span> db, login</span><br><span class="line"> </span><br><span class="line"> </span><br><span class="line"><span class="meta">@login.user_loader</span></span><br><span class="line"><span class="comment"># 获取登录用户的 id</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">load_user</span><span class="params">(user_id)</span>:</span></span><br><span class="line"> <span class="keyword">return</span> User.query.filter_by(id=user_id).first()</span><br><span class="line"> </span><br><span class="line"> </span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">User</span><span class="params">(db.Model, UserMixin)</span>:</span></span><br><span class="line"> <span class="comment"># nullable 非空 ; unique 不能重复</span></span><br><span class="line"> id = db.Column(db.Integer, primary_key=<span class="literal">True</span>)</span><br><span class="line"> username = db.Column(db.String(<span class="number">20</span>), unique=<span class="literal">True</span>, nullable=<span class="literal">False</span>)</span><br><span class="line"> password = db.Column(db.String(<span class="number">20</span>), nullable=<span class="literal">False</span>)</span><br><span class="line"> email = db.Column(db.String(<span class="number">120</span>), unique=<span class="literal">True</span>, nullable=<span class="literal">False</span>)</span><br><span class="line"> </span><br><span class="line"> </span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">__repr__</span><span class="params">(self)</span>:</span></span><br><span class="line"> <span class="keyword">return</span> <span class="string">'<User %r>'</span> % self.username</span><br></pre></td></tr></table></figure><p>重构数据库:</p><pre><code>在 "Python Console" 内重置数据库</code></pre> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">from app.models import db</span><br><span class="line">db.create_all()</span><br></pre></td></tr></table></figure></li><li><p>设置 <strong>route.py</strong> 为登录方法进行实现</p> <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> flask <span class="keyword">import</span> render_template, flash, redirect, url_for, request</span><br><span class="line"><span class="comment"># 从登录模块引入所需的方法</span></span><br><span class="line"><span class="keyword">from</span> flask_login <span class="keyword">import</span> login_user, login_required, current_user, logout_user</span><br><span class="line"><span class="keyword">from</span> app <span class="keyword">import</span> app, bcrypt, db</span><br><span class="line"><span class="comment"># 从 form.py 内引入 注册表格与登录表格</span></span><br><span class="line"><span class="keyword">from</span> app.forms <span class="keyword">import</span> RegisterForm, LoginForm</span><br><span class="line"><span class="keyword">from</span> app.models <span class="keyword">import</span> User</span><br><span class="line"> </span><br><span class="line"> </span><br><span class="line"><span class="meta">@app.route('/') # 指定路由</span></span><br><span class="line"><span class="comment"># 需要登录</span></span><br><span class="line"><span class="meta">@login_required</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">index</span><span class="params">()</span>:</span></span><br><span class="line"> <span class="keyword">return</span> render_template(<span class="string">'index.html'</span>)</span><br><span class="line"> </span><br><span class="line"> </span><br><span class="line"><span class="meta">@app.route('/register', methods=['GET', 'POST'])</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">register</span><span class="params">()</span>:</span></span><br><span class="line"> <span class="comment"># 判断是否是已经处于登录状态,如果是回到主界面</span></span><br><span class="line"> <span class="keyword">if</span> current_user.is_authenticated:</span><br><span class="line"> <span class="keyword">return</span> redirect(url_for(<span class="string">'index'</span>))</span><br><span class="line"> form = RegisterForm()</span><br><span class="line"> <span class="comment"># 获取提交上来的注册数据,进行处理</span></span><br><span class="line"> <span class="keyword">if</span> form.validate_on_submit():</span><br><span class="line"> username = form.username.data</span><br><span class="line"> email = form.email.data</span><br><span class="line"> <span class="comment"># 变种hash加密,相同密码生成值也不同</span></span><br><span class="line"> password = bcrypt.generate_password_hash(form.password.data)</span><br><span class="line"> user = User(username=username, email=email, password=password)</span><br><span class="line"> db.session.add(user)</span><br><span class="line"> db.session.commit()</span><br><span class="line"> flash(<span class="string">'Congrats registration success'</span>, category=<span class="string">'success'</span>)</span><br><span class="line"> <span class="keyword">return</span> redirect(url_for(<span class="string">'index'</span>))</span><br><span class="line"> <span class="comment"># 检查产生密码与hash 是否对应正确</span></span><br><span class="line"> <span class="comment"># bcrypt.check_password_hash(hash, password)</span></span><br><span class="line"> <span class="keyword">return</span> render_template(<span class="string">'register.html'</span>, form=form)</span><br><span class="line"> </span><br><span class="line"><span class="comment"># 设置登录函数、登录页面、登录方法</span></span><br><span class="line"><span class="meta">@app.route('/login', methods=['GET', 'POST'])</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">login</span><span class="params">()</span>:</span></span><br><span class="line"> <span class="comment"># 判断是否是已经处于登录状态,如果是回到主界面</span></span><br><span class="line"> <span class="keyword">if</span> current_user.is_authenticated:</span><br><span class="line"> <span class="keyword">return</span> redirect(url_for(<span class="string">'index'</span>))</span><br><span class="line"> <span class="comment"># 获取 登录表格 </span></span><br><span class="line"> form = LoginForm()</span><br><span class="line"> <span class="comment"># 如果调交上来的数据不为空</span></span><br><span class="line"> <span class="keyword">if</span> form.validate_on_submit():</span><br><span class="line"> username = form.username.data</span><br><span class="line"> <span class="comment"># 获取密码,检查密码是否与数据库中匹配</span></span><br><span class="line"> password = form.password.data</span><br><span class="line"> remember = form.remember.data</span><br><span class="line"> <span class="comment"># 根据输入用户名找到数据库中用户信息</span></span><br><span class="line"> user = User.query.filter_by(username=username).first()</span><br><span class="line"> <span class="comment"># 如果用户存在且密码对应正确</span></span><br><span class="line"> <span class="keyword">if</span> user <span class="keyword">and</span> bcrypt.check_password_hash(user.password, password):</span><br><span class="line"> <span class="comment"># 设置是否记住登录信息单选框</span></span><br><span class="line"> login_user(user, remember=remember)</span><br><span class="line"> <span class="comment"># 返回登录成功信息</span></span><br><span class="line"> flash(<span class="string">"login success"</span>, category=<span class="string">'info'</span>)</span><br><span class="line"> <span class="comment"># http://127.0.0.1:5000/login?next=%2F 由 next 决定接下来进入的页面</span></span><br><span class="line"> <span class="keyword">if</span> request.args.get(<span class="string">'next'</span>):</span><br><span class="line"> next_page = request.args.get(<span class="string">'next'</span>)</span><br><span class="line"> <span class="keyword">return</span> redirect(next_page)</span><br><span class="line"> <span class="keyword">return</span> redirect(url_for(<span class="string">'index'</span>))</span><br><span class="line"> <span class="comment"># 如果用户不岑在或者密码错误,flash 出错误</span></span><br><span class="line"> flash(<span class="string">"User not exists or password not match"</span>, category=<span class="string">'danger'</span>)</span><br><span class="line"> <span class="keyword">return</span> render_template(<span class="string">'login.html'</span>, form=form)</span><br><span class="line"> </span><br><span class="line"><span class="comment"># 设置登出函数、页面</span></span><br><span class="line"><span class="meta">@app.route('/logout')</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">logout</span><span class="params">()</span>:</span></span><br><span class="line"> <span class="comment"># 直接引用 flask_login 模块中的 logout_user()</span></span><br><span class="line"> logout_user()</span><br><span class="line"> <span class="comment"># 登出后返回登录页面</span></span><br><span class="line"> <span class="keyword">return</span> redirect(url_for(<span class="string">'login'</span>))</span><br></pre></td></tr></table></figure></li></ol>]]></content>
<tags>
<tag> Flask_Study </tag>
</tags>
</entry>
<entry>
<title>Windows下 Git 因移动文件夹导致的问题及解决</title>
<link href="/2020/08/05/save-git-problem/"/>
<url>/2020/08/05/save-git-problem/</url>
<content type="html"><![CDATA[<h3 id="Git-因移动文件夹导致更新后不是覆盖原版本而是在重新创建在原路径下,以及鼠标右键快捷菜单中的Git-Bash-Here-与-Git-GUI-Here无法使用的问题解决方法"><a href="#Git-因移动文件夹导致更新后不是覆盖原版本而是在重新创建在原路径下,以及鼠标右键快捷菜单中的Git-Bash-Here-与-Git-GUI-Here无法使用的问题解决方法" class="headerlink" title="Git 因移动文件夹导致更新后不是覆盖原版本而是在重新创建在原路径下,以及鼠标右键快捷菜单中的Git Bash Here 与 Git GUI Here无法使用的问题解决方法"></a><code>Git 因移动文件夹导致更新后不是覆盖原版本而是在重新创建在原路径下,以及鼠标右键快捷菜单中的Git Bash Here 与 Git GUI Here无法使用的问题解决方法</code></h3><ol><li><p>某年某月某日为了方便管理计算机下的软件,我对大部分软件进行了”搬家”,在对软件”搬家”的过程中 Git 产生了一些问题,我花费了半天时间对出现的比较重要的问题进行一点总结。</p><p> 必须做的事 : <code>在直接移动文件夹后一定要记得在</code><strong>环境变量</strong><code>中更改所有与 Git 有关的环境变量至现在路径。</code></p></li><li><p>问题 1 : <code>鼠标右键快捷菜单中的Git Bash Here 与 Git GUI Here失去图标,以及无法使用</code></p><p> 解决 : <code>Windows 系统下鼠标右键快捷菜单的图标以及功能都与</code><strong>注册表</strong><code>有关。</code></p><ol><li><p><code>打开注册表编辑器</code>,<code>在</code> <strong>\HKEY_CLASSES_ROOT\Directory\Background\shell\git_gui\command</strong> <code>与</code><strong>\HKEY_CLASSES_ROOT\Directory\Background\shell\git_shell\command</strong> <code>下修改Icon 数据为 目前 git_gui.exe 的路径 ("E:\GitHup\Git\cmd\git-gui.exe" "--working-dir" "%v.") 与 git_shell.exe 路径 ("E:\GitHup\Git\git-bash.exe" "--cd=%v.") ,之后 鼠标右键快捷菜单的图标已经恢复,但功能仍未恢复</code></p><p><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/git_problem4.png" alt="注册表"></p></li><li><p><code>继续在</code><strong>注册表编辑器</strong><code>中</code><strong>\HKEY_CLASSES_ROOT\Directory\shell\git_gui\command</strong><code>与</code><strong>\HKEY_CLASSES_ROOT\Directory\shell\git_gui\command</strong><code>下修改 数据为目前 git_gui.exe 的路径 ("E:\GitHup\Git\cmd\git-gui.exe" "--working-dir" "%1") 与 git_shell.exe 路径 ("E:\GitHup\Git\git-bash.exe" "--cd=%1"),然后鼠标右键快捷菜单中图标也可以恢复</code><br><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/git_problem5.png" alt="注册表"></p></li></ol><p> <em>部分没有立刻恢复可能需要重启计算机</em></p></li><li><p>问题 2 : <code>Git 因移动文件夹导致更新后不是覆盖原版本而是在重新创建在原路径下</code></p><p> 解决 : <code>这个我的办法比较笨,我在注册表</code> <strong>Ctrl + F</strong> <code>查找所有Git 的数据以及注册文件, 然后进行修改为现在文件路径 (对于路径后的东西可以不管)</code><br> <img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/git_problem1.png" alt="注册表"></p></li><li><p><code>对于移动后可能出现的其他问题,查看 Git 的全局变量寻找没改过的路径进行修改</code></p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">路径: </span><br><span class="line">Git -> etc -> gitconfig 以记事本打开</span><br><span class="line">Git -> mingw64 -> etc -> gitconfig 以记事本打开</span><br></pre></td></tr></table></figure><p> <img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/git_problem2.png" alt="Git -> etc -> gitconfig"><br> <img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/git_problem3.png" alt="Git -> mingw64 -> etc -> gitconfig"><br> <code>分别修改内部 git 路径。</code></p></li></ol><p><strong>纯属在百度上搜不到类似问题的解决,又由于自身 git 之前使用不善导致 git 内部许多东西都不想删掉,然后需要更新 git 才摸索出来这种办法</strong></p>]]></content>
<tags>
<tag> Windows problem </tag>
</tags>
</entry>
<entry>
<title>pyecharts-Map</title>
<link href="/2020/08/03/pyecharts-map/"/>
<url>/2020/08/03/pyecharts-map/</url>
<content type="html"><![CDATA[<h1 id="pyecharts-Map"><a href="#pyecharts-Map" class="headerlink" title="pyecharts_Map"></a>pyecharts_Map</h1><p><a href="http://pyecharts.org/#/zh-cn/" target="_blank" rel="noopener">全部来自于官方文档,主要用于方便自己查找</a></p><h2 id="Map:地图"><a href="#Map:地图" class="headerlink" title="Map:地图"></a><code>Map:地图</code></h2> <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> pyecharts.charts <span class="keyword">import</span> Map</span><br><span class="line"><span class="keyword">from</span> pyecharts.faker <span class="keyword">import</span> Faker</span><br><span class="line"><span class="keyword">import</span> pyecharts.options <span class="keyword">as</span> opts</span><br><span class="line"></span><br><span class="line">data = [list[z] <span class="keyword">for</span> z <span class="keyword">in</span> zip(<span class="literal">False</span>.guangdong_city, Faker.values())]</span><br><span class="line">c = (</span><br><span class="line"> Map()</span><br><span class="line"> .add(series_name=<span class="string">""</span>, data_pair=data, maptype=<span class="string">"广东"</span>)</span><br><span class="line"> .set_global_opts(</span><br><span class="line"> title_opts=opts.TitleOpts(title=<span class="string">"主标题"</span>),</span><br><span class="line"> visualmap_opts=opts.VisualMapOpts(max_=<span class="number">200</span>, is_piecewise=<span class="literal">True</span>)</span><br><span class="line"> )</span><br><span class="line"> .render(<span class="string">'xxx.html'</span>)</span><br><span class="line">)</span><br></pre></td></tr></table></figure><ul><li>.add() <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line">def add(</span><br><span class="line"> # 系列名称,用于 tooltip 的显示,legend 的图例筛选。</span><br><span class="line"> series_name: str,</span><br><span class="line"></span><br><span class="line"> # 数据项 (坐标点名称,坐标点值)</span><br><span class="line"> data_pair: types.Sequence[types.Union[types.Sequence, opts.MapItem, dict]],</span><br><span class="line"></span><br><span class="line"> # 地图类型,具体参考 pyecharts.datasets.map_filenames.json 文件</span><br><span class="line"> maptype: str = "china",</span><br><span class="line"></span><br><span class="line"> # 是否选中图例</span><br><span class="line"> is_selected: bool = True,</span><br><span class="line"></span><br><span class="line"> # 是否开启鼠标缩放和平移漫游。</span><br><span class="line"> is_roam: bool = True,</span><br><span class="line"></span><br><span class="line"> # 当前视角的中心点,用经纬度表示</span><br><span class="line"> center: Optional[Sequence] = None,</span><br><span class="line"></span><br><span class="line"> # 当前视角的缩放比例。</span><br><span class="line"> zoom: Optional[Numeric] = 1,</span><br><span class="line"></span><br><span class="line"> # 自定义地区的名称映射</span><br><span class="line"> name_map: Optional[dict] = None,</span><br><span class="line"></span><br><span class="line"> # 标记图形形状</span><br><span class="line"> symbol: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 是否显示标记图形</span><br><span class="line"> is_map_symbol_show: bool = True,</span><br><span class="line"></span><br><span class="line"> # 标签配置项,参考 `series_options.LabelOpts`</span><br><span class="line"> label_opts: Union[opts.LabelOpts, dict] = opts.LabelOpts(),</span><br><span class="line"></span><br><span class="line"> # 提示框组件配置项,参考 `series_options.TooltipOpts`</span><br><span class="line"> tooltip_opts: Union[opts.TooltipOpts, dict, None] = None,</span><br><span class="line"></span><br><span class="line"> # 图元样式配置项,参考 `series_options.ItemStyleOpts`</span><br><span class="line"> itemstyle_opts: Union[opts.ItemStyleOpts, dict, None] = None,</span><br><span class="line"></span><br><span class="line"> # 高亮标签配置项,参考 `series_options.LabelOpts`</span><br><span class="line"> emphasis_label_opts: Union[opts.LabelOpts, dict, None] = None,</span><br><span class="line"></span><br><span class="line"> # 高亮图元样式配置项,参考 `series_options.ItemStyleOpts`</span><br><span class="line"> emphasis_itemstyle_opts: Union[opts.ItemStyleOpts, dict, None] = None,</span><br><span class="line">)</span><br></pre></td></tr></table></figure></li></ul><ol><li><code>MapItem: 地图数据项</code> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">class MapItem(</span><br><span class="line"> # 数据所对应的地图区域的名称,例如 '广东','浙江'。</span><br><span class="line"> name: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 该区域的数据值。</span><br><span class="line"> value: Optional[Numeric] = None,</span><br><span class="line"></span><br><span class="line"> # 该区域是否选中。</span><br><span class="line"> is_selected: bool = False,</span><br><span class="line"></span><br><span class="line"> # 标签配置项,参考 `series_options.LabelOpts`</span><br><span class="line"> label_opts: Union[LabelOpts, dict, None] = None,</span><br><span class="line"></span><br><span class="line"> # 图元样式配置项,参考 `series_options.ItemStyleOpts`</span><br><span class="line"> itemstyle_opts: Union[ItemStyleOpts, dict, None] = None,</span><br><span class="line"></span><br><span class="line"> # 提示框组件配置项,参考 `series_options.TooltipOpts`</span><br><span class="line"> tooltip_opts: Union[TooltipOpts, dict, None] = None,</span><br><span class="line">)</span><br></pre></td></tr></table></figure></li></ol>]]></content>
<tags>
<tag> Pyecharts </tag>
</tags>
</entry>
<entry>
<title>pyecharts-WordCloud</title>
<link href="/2020/08/02/pyecharts-wordcloud/"/>
<url>/2020/08/02/pyecharts-wordcloud/</url>
<content type="html"><![CDATA[<h1 id="pyecharts-WordCloud"><a href="#pyecharts-WordCloud" class="headerlink" title="pyecharts_WordCloud"></a>pyecharts_WordCloud</h1><p><a href="http://pyecharts.org/#/zh-cn/" target="_blank" rel="noopener">全部来自于官方文档,主要用于方便自己查找</a></p><h2 id="WordCloud:词云图"><a href="#WordCloud:词云图" class="headerlink" title="WordCloud:词云图"></a><code>WordCloud:词云图</code></h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> pyecharts.charts <span class="keyword">import</span> WordCloud</span><br><span class="line"><span class="keyword">import</span> pyecharts.options <span class="keyword">as</span> opts</span><br><span class="line">word = [ (<span class="string">"花鸟市场"</span>, <span class="number">1446</span>),</span><br><span class="line"> (<span class="string">"汽车"</span>, <span class="number">928</span>),</span><br><span class="line"> ...</span><br><span class="line">] </span><br><span class="line">c = (WordCloud()</span><br><span class="line"> .add(series_name=<span class="string">''</span>,</span><br><span class="line"> data_pair=word,</span><br><span class="line"> word_size_range=[],</span><br><span class="line"> mask_image=<span class="string">''</span>,</span><br><span class="line"> textstyle_opts = opts.TextStyleOpts(font_family=<span class="string">'cursive'</span>) </span><br><span class="line"> )</span><br><span class="line"> .set_global_opts(title_opts=opts.TitleOpts(title=<span class="string">'主标题'</span>))</span><br><span class="line"> .render(<span class="string">'xxx.html'</span>)</span><br><span class="line">)</span><br></pre></td></tr></table></figure><ul><li>.add()<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br></pre></td><td class="code"><pre><span class="line">def add(</span><br><span class="line"> # 系列名称,用于 tooltip 的显示,legend 的图例筛选。</span><br><span class="line"> series_name: str,</span><br><span class="line"></span><br><span class="line"> # 系列数据项,[(word1, count1), (word2, count2)]</span><br><span class="line"> data_pair: Sequence,</span><br><span class="line"></span><br><span class="line"> # 词云图轮廓,有 'circle', 'cardioid', 'diamond', 'triangle-forward', 'triangle', 'pentagon', 'star' 可选</span><br><span class="line"> shape: str = "circle",</span><br><span class="line"></span><br><span class="line"> # 自定义的图片(目前支持 jpg, jpeg, png, ico 的格式,其他的图片格式待测试)</span><br><span class="line"> # 该参数支持:</span><br><span class="line"> # 1、 base64 (需要补充 data 头);</span><br><span class="line"> # 2、本地文件路径(相对或者绝对路径都可以)</span><br><span class="line"> # 注:如果使用了 mask_image 之后第一次渲染会出现空白的情况,再刷新一次就可以了(Echarts 的问题)</span><br><span class="line"> # Echarts Issue: https://github.com/ecomfe/echarts-wordcloud/issues/74</span><br><span class="line"> mask_image: types.Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 单词间隔</span><br><span class="line"> word_gap: Numeric = 20,</span><br><span class="line"></span><br><span class="line"> # 单词字体大小范围</span><br><span class="line"> word_size_range=None,</span><br><span class="line"></span><br><span class="line"> # 旋转单词角度</span><br><span class="line"> rotate_step: Numeric = 45,</span><br><span class="line"></span><br><span class="line"> # 距离左侧的距离</span><br><span class="line"> pos_left: types.Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 距离顶部的距离</span><br><span class="line"> pos_top: types.Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 距离右侧的距离</span><br><span class="line"> pos_right: types.Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 距离底部的距离</span><br><span class="line"> pos_bottom: types.Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 词云图的宽度</span><br><span class="line"> width: types.Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 词云图的高度</span><br><span class="line"> height: types.Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 允许词云图的数据展示在画布范围之外</span><br><span class="line"> is_draw_out_of_bound: bool = False,</span><br><span class="line"></span><br><span class="line"> # 提示框组件配置项,参考 `series_options.TooltipOpts`</span><br><span class="line"> tooltip_opts: Union[opts.TooltipOpts, dict, None] = None,</span><br><span class="line"></span><br><span class="line"> # 词云图文字的配置</span><br><span class="line"> textstyle_opts: types.TextStyle = None,</span><br><span class="line"></span><br><span class="line"> # 词云图文字阴影的范围</span><br><span class="line"> emphasis_shadow_blur: types.Optional[types.Numeric] = None,</span><br><span class="line"></span><br><span class="line"> # 词云图文字阴影的颜色</span><br><span class="line"> emphasis_shadow_color: types.Optional[str] = None,</span><br><span class="line">)</span><br></pre></td></tr></table></figure></li></ul>]]></content>
<tags>
<tag> Pyecharts </tag>
</tags>
</entry>
<entry>
<title>pyecharts-Pie</title>
<link href="/2020/08/01/pyecharts-pie/"/>
<url>/2020/08/01/pyecharts-pie/</url>
<content type="html"><![CDATA[<h1 id="pyecharts-Pie"><a href="#pyecharts-Pie" class="headerlink" title="pyecharts_Pie"></a>pyecharts_Pie</h1><p><a href="http://pyecharts.org/#/zh-cn/" target="_blank" rel="noopener">全部来自于官方文档,主要用于方便自己查找</a></p><h2 id="Pie:饼图"><a href="#Pie:饼图" class="headerlink" title="Pie:饼图"></a><code>Pie:饼图</code></h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> pyecharts.charts <span class="keyword">import</span> Pie</span><br><span class="line"><span class="keyword">import</span> pyecharts.options <span class="keyword">as</span> opts</span><br><span class="line"><span class="comment"># .fake 伪造数据生成器,具体看内部代码</span></span><br><span class="line"><span class="keyword">from</span> pyecharts.faker <span class="keyword">import</span> Faker</span><br><span class="line"></span><br><span class="line"><span class="comment"># 生成多维数组 eg:[['周一', 127], ['周二', 111], ['周三', 61], ['周四', 94], ['周五', 93], ['周六', 51], ['周日', 47]]</span></span><br><span class="line">data=[list(z) <span class="keyword">for</span> z <span class="keyword">in</span> zip(Faker.choose(), Faker.values())]</span><br><span class="line"><span class="comment"># 颜色列表,与 data 中数据一一对应 </span></span><br><span class="line">color = []</span><br><span class="line">c = (</span><br><span class="line"> Pie()</span><br><span class="line"> .add(series_name=<span class="string">''</span>, data_pair=data)</span><br><span class="line"> .set_colors(color)</span><br><span class="line"> .set_global_opts(title_opts=opts.TitleOpts(title=<span class="string">"主标题"</span>))</span><br><span class="line"> <span class="comment"># 生成 饼图外标 "周一: 127"</span></span><br><span class="line"> .set_series_opts(label_opts=opts.LabelOpts(formatter=<span class="string">"{b}:{c}"</span>))</span><br><span class="line"> .render(<span class="string">'xxx.html'</span>)</span><br><span class="line">)</span><br></pre></td></tr></table></figure><ul><li>.add() <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line">def add(</span><br><span class="line"> # 系列名称,用于 tooltip 的显示,legend 的图例筛选。</span><br><span class="line"> series_name: str,</span><br><span class="line"></span><br><span class="line"> # 系列数据项,格式为 [(key1, value1), (key2, value2)]</span><br><span class="line"> data_pair: types.Sequence[types.Union[types.Sequence, opts.PieItem, dict]],</span><br><span class="line"></span><br><span class="line"> # 系列 label 颜色</span><br><span class="line"> color: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 饼图的半径,数组的第一项是内半径,第二项是外半径</span><br><span class="line"> # 默认设置成百分比,相对于容器高宽中较小的一项的一半</span><br><span class="line"> radius: Optional[Sequence] = None,</span><br><span class="line"></span><br><span class="line"> # 饼图的中心(圆心)坐标,数组的第一项是横坐标,第二项是纵坐标</span><br><span class="line"> # 默认设置成百分比,设置成百分比时第一项是相对于容器宽度,第二项是相对于容器高度</span><br><span class="line"> center: Optional[Sequence] = None,</span><br><span class="line"></span><br><span class="line"> # 是否展示成南丁格尔图,通过半径区分数据大小,有'radius'和'area'两种模式。</span><br><span class="line"> # radius:扇区圆心角展现数据的百分比,半径展现数据的大小</span><br><span class="line"> # area:所有扇区圆心角相同,仅通过半径展现数据大小</span><br><span class="line"> rosetype: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 饼图的扇区是否是顺时针排布。</span><br><span class="line"> is_clockwise: bool = True,</span><br><span class="line"></span><br><span class="line"> # 标签配置项,参考 `series_options.LabelOpts`</span><br><span class="line"> label_opts: Union[opts.LabelOpts, dict] = opts.LabelOpts(),</span><br><span class="line"></span><br><span class="line"> # 提示框组件配置项,参考 `series_options.TooltipOpts`</span><br><span class="line"> tooltip_opts: Union[opts.TooltipOpts, dict, None] = None,</span><br><span class="line"></span><br><span class="line"> # 图元样式配置项,参考 `series_options.ItemStyleOpts`</span><br><span class="line"> itemstyle_opts: Union[opts.ItemStyleOpts, dict, None] = None,</span><br><span class="line"></span><br><span class="line"> # 可以定义 data 的哪个维度被编码成什么。</span><br><span class="line"> encode: types.Union[types.JSFunc, dict, None] = None,</span><br><span class="line">)</span><br></pre></td></tr></table></figure></li></ul><ol><li><code>PieItem:饼图数据项</code> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">class PieItem(</span><br><span class="line"> # 数据项名称。</span><br><span class="line"> name: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 数据值。</span><br><span class="line"> value: Optional[Numeric] = None,</span><br><span class="line"></span><br><span class="line"> # 该数据项是否被选中。</span><br><span class="line"> is_selected: bool = False,</span><br><span class="line"></span><br><span class="line"> # 标签配置项,参考 `series_options.LabelOpts`</span><br><span class="line"> label_opts: Union[LabelOpts, dict, None] = None,</span><br><span class="line"></span><br><span class="line"> # 图元样式配置项,参考 `series_options.ItemStyleOpts`</span><br><span class="line"> itemstyle_opts: Union[ItemStyleOpts, dict, None] = None,</span><br><span class="line"></span><br><span class="line"> # 提示框组件配置项,参考 `series_options.TooltipOpts`</span><br><span class="line"> tooltip_opts: Union[TooltipOpts, dict, None] = None,</span><br><span class="line">)</span><br></pre></td></tr></table></figure></li></ol>]]></content>
<tags>
<tag> Pyecharts </tag>
</tags>
</entry>
<entry>
<title>pyecharts-Line</title>
<link href="/2020/08/01/pyecharts-line/"/>
<url>/2020/08/01/pyecharts-line/</url>
<content type="html"><![CDATA[<h1 id="pyecharts-Line"><a href="#pyecharts-Line" class="headerlink" title="pyecharts_Line"></a>pyecharts_Line</h1><p><a href="http://pyecharts.org/#/zh-cn/" target="_blank" rel="noopener">全部来自于官方文档,主要用于方便自己查找</a></p><h2 id="Line:折线-面积图"><a href="#Line:折线-面积图" class="headerlink" title="Line:折线/面积图"></a><code>Line:折线/面积图</code></h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> pyecharts.charts <span class="keyword">import</span> Line</span><br><span class="line"><span class="keyword">import</span> pyecharts.options <span class="keyword">as</span> opts</span><br><span class="line">x_data = []</span><br><span class="line">y_data = []</span><br><span class="line"></span><br><span class="line">(</span><br><span class="line"> Line(init_opts=opts.InitOpts())</span><br><span class="line"> .add_xaxis(x_data)</span><br><span class="line"> .add_yaxis(</span><br><span class="line"> series_name=<span class="string">'折线名称'</span>,</span><br><span class="line"> <span class="comment"># 折线数据 </span></span><br><span class="line"> y_axis= y_data,</span><br><span class="line"> <span class="comment"># 标记点为 </span></span><br><span class="line"> markpoint_opts=opts.MarkPointOpts(</span><br><span class="line"> data=[</span><br><span class="line"> opts.MarkPointItem(type_=<span class="string">'max'</span>, name=<span class="string">"最大值"</span>),</span><br><span class="line"> opts.MarkPointItem(type_=<span class="string">'min'</span>, name=<span class="string">'最小值'</span>),</span><br><span class="line"> ]</span><br><span class="line"> ),</span><br><span class="line"> <span class="comment"># 额外标记线</span></span><br><span class="line"> markline_opts=opts.MarkLineOpts(</span><br><span class="line"> data=[opts.MarkLineItem(type_=<span class="string">"average"</span>, name=<span class="string">"平均值"</span>)]</span><br><span class="line"> ), </span><br><span class="line"> )</span><br><span class="line"> .set_series_opts(</span><br><span class="line"> <span class="comment"># 不透明度设置</span></span><br><span class="line"> areastyle_opts=opts.AreaStyleOpts(opacity=<span class="number">0.5</span>)</span><br><span class="line"> )</span><br><span class="line"> .set_global_opts(</span><br><span class="line"> title_opts=opts.TitleOpts(title=<span class="string">"主标题"</span>, subtitle=<span class="string">'副标题'</span>),</span><br><span class="line"> toolbox_opts=opts.TooltipOpts()</span><br><span class="line"> )</span><br><span class="line"> .render(<span class="string">"xxx.html"</span>)</span><br><span class="line">)</span><br></pre></td></tr></table></figure><p><strong>.add_yaxis()</strong></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br></pre></td><td class="code"><pre><span class="line">def add_yaxis(</span><br><span class="line"> # 系列名称,用于 tooltip 的显示,legend 的图例筛选。</span><br><span class="line"> series_name: str,</span><br><span class="line"></span><br><span class="line"> # 系列数据</span><br><span class="line"> y_axis: types.Sequence[types.Union[opts.LineItem, dict]],</span><br><span class="line"></span><br><span class="line"> # 是否选中图例</span><br><span class="line"> is_selected: bool = True,</span><br><span class="line"></span><br><span class="line"> # 是否连接空数据,空数据使用 `None` 填充</span><br><span class="line"> is_connect_nones: bool = False,</span><br><span class="line"></span><br><span class="line"> # 使用的 x 轴的 index,在单个图表实例中存在多个 x 轴的时候有用。</span><br><span class="line"> xaxis_index: Optional[Numeric] = None,</span><br><span class="line"></span><br><span class="line"> # 使用的 y 轴的 index,在单个图表实例中存在多个 y 轴的时候有用。</span><br><span class="line"> yaxis_index: Optional[Numeric] = None,</span><br><span class="line"></span><br><span class="line"> # 系列 label 颜色</span><br><span class="line"> color: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 是否显示 symbol, 如果 false 则只有在 tooltip hover 的时候显示。</span><br><span class="line"> is_symbol_show: bool = True,</span><br><span class="line"></span><br><span class="line"> # 标记的图形。</span><br><span class="line"> # ECharts 提供的标记类型包括 'circle', 'rect', 'roundRect', 'triangle', </span><br><span class="line"> # 'diamond', 'pin', 'arrow', 'none'</span><br><span class="line"> # 可以通过 'image://url' 设置为图片,其中 URL 为图片的链接,或者 dataURI。</span><br><span class="line"> symbol: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 标记的大小,可以设置成诸如 10 这样单一的数字,也可以用数组分开表示宽和高,</span><br><span class="line"> # 例如 [20, 10] 表示标记宽为 20,高为 10。</span><br><span class="line"> symbol_size: Union[Numeric, Sequence] = 4,</span><br><span class="line"></span><br><span class="line"> # 数据堆叠,同个类目轴上系列配置相同的 stack 值可以堆叠放置。</span><br><span class="line"> stack: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 是否平滑曲线</span><br><span class="line"> is_smooth: bool = False,</span><br><span class="line"></span><br><span class="line"> # 是否裁剪超出坐标系部分的图形。折线图:裁掉所有超出坐标系的折线部分,拐点图形的逻辑按照散点图处理</span><br><span class="line"> is_clip: bool = True,</span><br><span class="line"></span><br><span class="line"> # 是否显示成阶梯图</span><br><span class="line"> is_step: bool = False,</span><br><span class="line"></span><br><span class="line"> # 是否开启 hover 在拐点标志上的提示动画效果。</span><br><span class="line"> is_hover_animation: bool = True,</span><br><span class="line"></span><br><span class="line"> # 折线图所有图形的 zlevel 值。</span><br><span class="line"> # zlevel用于 Canvas 分层,不同zlevel值的图形会放置在不同的 Canvas 中,Canvas 分层是一种常见的优化手段。</span><br><span class="line"> # zlevel 大的 Canvas 会放在 zlevel 小的 Canvas 的上面。</span><br><span class="line"> z_level: types.Numeric = 0,</span><br><span class="line"></span><br><span class="line"> # 折线图组件的所有图形的z值。控制图形的前后顺序。z值小的图形会被z值大的图形覆盖。</span><br><span class="line"> # z 相比 zlevel 优先级更低,而且不会创建新的 Canvas。</span><br><span class="line"> z: types.Numeric = 0,</span><br><span class="line"></span><br><span class="line"> # 标记点配置项,参考 `series_options.MarkPointOpts`</span><br><span class="line"> markpoint_opts: Union[opts.MarkPointOpts, dict, None] = None,</span><br><span class="line"></span><br><span class="line"> # 标记线配置项,参考 `series_options.MarkLineOpts`</span><br><span class="line"> markline_opts: Union[opts.MarkLineOpts, dict, None] = None,</span><br><span class="line"></span><br><span class="line"> # 提示框组件配置项,参考 `series_options.TooltipOpts`</span><br><span class="line"> tooltip_opts: Union[opts.TooltipOpts, dict, None] = None,</span><br><span class="line"></span><br><span class="line"> # 标签配置项,参考 `series_options.LabelOpts`</span><br><span class="line"> label_opts: Union[opts.LabelOpts, dict] = opts.LabelOpts(),</span><br><span class="line"></span><br><span class="line"> # 线样式配置项,参考 `series_options.LineStyleOpts`</span><br><span class="line"> linestyle_opts: Union[opts.LineStyleOpts, dict] = opts.LineStyleOpts(),</span><br><span class="line"></span><br><span class="line"> # 填充区域配置项,参考 `series_options.AreaStyleOpts`</span><br><span class="line"> areastyle_opts: Union[opts.AreaStyleOpts, dict] = opts.AreaStyleOpts(),</span><br><span class="line"></span><br><span class="line"> # 图元样式配置项,参考 `series_options.ItemStyleOpts`</span><br><span class="line"> itemstyle_opts: Union[opts.ItemStyleOpts, dict, None] = None,</span><br><span class="line">)</span><br></pre></td></tr></table></figure><ol><li><code>LineItem:折线图数据项</code><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line">class LineItem(</span><br><span class="line"> # 数据项名称。</span><br><span class="line"> name: Union[str, Numeric] = None,</span><br><span class="line"></span><br><span class="line"> # 数据项的值</span><br><span class="line"> value: Union[str, Numeric] = None,</span><br><span class="line"></span><br><span class="line"> # 单个数据标记的图形。</span><br><span class="line"> symbol: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 单个数据标记的大小</span><br><span class="line"> symbol_size: Union[Sequence[Numeric], Numeric] = None,</span><br><span class="line"></span><br><span class="line"> # 单个数据标记的旋转角度(而非弧度)。</span><br><span class="line"> symbol_rotate: Optional[Numeric] = None,</span><br><span class="line"></span><br><span class="line"> # 如果 symbol 是 path:// 的形式,是否在缩放时保持该图形的长宽比。</span><br><span class="line"> symbol_keep_aspect: bool = False,</span><br><span class="line"></span><br><span class="line"> # 单个数据标记相对于原本位置的偏移。</span><br><span class="line"> symbol_offset: Optional[Sequence] = None,</span><br><span class="line"></span><br><span class="line"> # 标签配置项,参考 `series_options.LabelOpts`</span><br><span class="line"> label_opts: Union[LabelOpts, dict, None] = None,</span><br><span class="line"></span><br><span class="line"> # 图元样式配置项,参考 `series_options.ItemStyleOpts`</span><br><span class="line"> itemstyle_opts: Union[ItemStyleOpts, dict, None] = None,</span><br><span class="line"></span><br><span class="line"> # 提示框组件配置项,参考 `series_options.TooltipOpts`</span><br><span class="line"> tooltip_opts: Union[TooltipOpts, dict, None] = None,</span><br><span class="line">)</span><br></pre></td></tr></table></figure></li></ol>]]></content>
<tags>
<tag> Pyecharts </tag>
</tags>
</entry>
<entry>
<title>pixabay_爬取</title>
<link href="/2020/07/31/pixabay-pa-qu/"/>
<url>/2020/07/31/pixabay-pa-qu/</url>
<content type="html"><![CDATA[<h2 id="记一次简单的爬虫学习"><a href="#记一次简单的爬虫学习" class="headerlink" title="记一次简单的爬虫学习"></a>记一次简单的爬虫学习</h2><p><strong>批量爬取 pixabay 图片</strong></p><p><a href="https://pixabay.com/zh/images/search/" target="_blank" rel="noopener">pixabay图片查找地址</a></p><p><a href="https://pixabay.com/api/docs/" target="_blank" rel="noopener">pixabay网站提供的API</a></p><hr><h5 id="1-pixabay-是一个做的很好的免费图片获取网站,里边有很多高质量免费的图片,有一天我想能不能写一个批量获取图片的爬虫,用于收集那些漂亮图片"><a href="#1-pixabay-是一个做的很好的免费图片获取网站,里边有很多高质量免费的图片,有一天我想能不能写一个批量获取图片的爬虫,用于收集那些漂亮图片" class="headerlink" title="1. pixabay 是一个做的很好的免费图片获取网站,里边有很多高质量免费的图片,有一天我想能不能写一个批量获取图片的爬虫,用于收集那些漂亮图片"></a>1. pixabay 是一个做的很好的免费图片获取网站,里边有很多高质量免费的图片,有一天我想能不能写一个批量获取图片的爬虫,用于收集那些漂亮图片</h5><p><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/pixabay_search.png" alt></p><h5 id="2-说做就做,我先对网站进行最简单的分析-急性子,甚至源码都没细看-然后就开始急急切切的开始了爬取"><a href="#2-说做就做,我先对网站进行最简单的分析-急性子,甚至源码都没细看-然后就开始急急切切的开始了爬取" class="headerlink" title="2. 说做就做,我先对网站进行最简单的分析(急性子,甚至源码都没细看),然后就开始急急切切的开始了爬取"></a>2. 说做就做,我先对网站进行最简单的分析(急性子,甚至源码都没细看),然后就开始急急切切的开始了爬取</h5><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> requests</span><br><span class="line"><span class="keyword">from</span> bs4 <span class="keyword">import</span> BeautifulSoup</span><br><span class="line"></span><br><span class="line">link = <span class="string">'https://pixabay.com/zh/images/search/'</span></span><br><span class="line">headers = {<span class="string">'User-Agent'</span>: <span class="string">'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '</span></span><br><span class="line"> <span class="string">'Chrome/78.0.3904.97 Safari/537.36'</span>}</span><br><span class="line">response = requests.get(link, headers=headers)</span><br><span class="line">soup = BeautifulSoup(response.text, <span class="string">'lxml'</span>)</span><br><span class="line">print(soup)</span><br></pre></td></tr></table></figure><h5 id="结果当然是失败了,并且基本上没有获得什么有用的信息"><a href="#结果当然是失败了,并且基本上没有获得什么有用的信息" class="headerlink" title="结果当然是失败了,并且基本上没有获得什么有用的信息"></a>结果当然是失败了,并且基本上没有获得什么有用的信息</h5><p><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/fail_scrapy.png" alt></p><h5 id="3-然后再对网页源码报头进行仔细查看后仍然一头雾水的我,想到了可能会提供的有-API-接口,然后通过-CTRL-F在源码中搜索,找到了官方提供的-API-结构-赞美官方"><a href="#3-然后再对网页源码报头进行仔细查看后仍然一头雾水的我,想到了可能会提供的有-API-接口,然后通过-CTRL-F在源码中搜索,找到了官方提供的-API-结构-赞美官方" class="headerlink" title="3. 然后再对网页源码报头进行仔细查看后仍然一头雾水的我,想到了可能会提供的有 API 接口,然后通过 CTRL + F在源码中搜索,找到了官方提供的 API 结构(赞美官方)"></a>3. 然后再对网页源码报头进行仔细查看后仍然一头雾水的我,想到了可能会提供的有 API 接口,然后通过 <strong>CTRL + F</strong>在源码中搜索,找到了官方提供的 API 结构(赞美官方)</h5><p><a href="https://pixabay.com/api/docs/" target="_blank" rel="noopener">pixabay网站提供的API</a></p><p><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/pixabay_API.png" alt></p><h5 id="通过阅读官方文档就能发现只需要注册官方就可以提供一个-API-key,每小时下载量也够我这个个人用户使用"><a href="#通过阅读官方文档就能发现只需要注册官方就可以提供一个-API-key,每小时下载量也够我这个个人用户使用" class="headerlink" title="通过阅读官方文档就能发现只需要注册官方就可以提供一个 API key,每小时下载量也够我这个个人用户使用"></a>通过阅读官方文档就能发现只需要注册官方就可以提供一个 API key,每小时下载量也够我这个个人用户使用</h5><p><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/API_text.png" alt></p><h5 id="官方提供了非常详细的-网址的各种参量拼接方式,以及爬出样本以供分析-出乎意料的是还有影片的接口及查询方式"><a href="#官方提供了非常详细的-网址的各种参量拼接方式,以及爬出样本以供分析-出乎意料的是还有影片的接口及查询方式" class="headerlink" title="官方提供了非常详细的 网址的各种参量拼接方式,以及爬出样本以供分析(出乎意料的是还有影片的接口及查询方式)"></a>官方提供了非常详细的 网址的各种参量拼接方式,以及爬出样本以供分析(出乎意料的是还有影片的接口及查询方式)</h5><h6 id="对链接构成简单分析后,我发现对于自己需要提供的仅仅是KEY-图片类型-image-type-,搜索条件-q-,爬取页数-page-,每页爬取数-per-page"><a href="#对链接构成简单分析后,我发现对于自己需要提供的仅仅是KEY-图片类型-image-type-,搜索条件-q-,爬取页数-page-,每页爬取数-per-page" class="headerlink" title="对链接构成简单分析后,我发现对于自己需要提供的仅仅是KEY, 图片类型(image_type),搜索条件(q),爬取页数(page),每页爬取数(per_page)"></a>对链接构成简单分析后,我发现对于自己需要提供的仅仅是<strong>KEY</strong>, <strong>图片类型(image_type)</strong>,<strong>搜索条件(q)</strong>,<strong>爬取页数(page)</strong>,<strong>每页爬取数(per_page)</strong></h6><p><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/API_text1.png" alt></p><hr><p><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/img_example.png" alt></p><hr><p><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/video_text.png" alt><br><code>于是一段简单的拼接 URL 构成了,也考虑到了翻页的情况</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">def get_json(page, condition, image_type):</span><br><span class="line"> page += 1</span><br><span class="line"> key = ''</span><br><span class="line"> url = 'https://pixabay.com/api/'</span><br><span class="line"></span><br><span class="line"> pa = {</span><br><span class="line"> 'key': key,</span><br><span class="line"> 'q': condition,</span><br><span class="line"> 'image_type': image_type,</span><br><span class="line"> 'lang': 'zh',</span><br><span class="line"> 'page': page,</span><br><span class="line"> 'per_page': 200,</span><br><span class="line"> }</span><br><span class="line"> r = requests.get(url, params=pa, headers=headers)</span><br><span class="line"> decode_json = json.loads(r.text)</span><br><span class="line"> return decode_json</span><br></pre></td></tr></table></figure><h5 id="4-根据官方提供的例子进行分析,挑出自己需要的部分"><a href="#4-根据官方提供的例子进行分析,挑出自己需要的部分" class="headerlink" title="4. 根据官方提供的例子进行分析,挑出自己需要的部分"></a>4. 根据官方提供的例子进行分析,挑出自己需要的部分</h5><figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line">{</span><br><span class="line"><span class="attr">"total"</span>: <span class="number">4692</span>,</span><br><span class="line"><span class="attr">"totalHits"</span>: <span class="number">500</span>,</span><br><span class="line"><span class="attr">"hits"</span>: [</span><br><span class="line"> {</span><br><span class="line"> <span class="attr">"id"</span>: <span class="number">195893</span>,</span><br><span class="line"> <span class="attr">"pageURL"</span>: <span class="string">"https://pixabay.com/en/blossom-bloom-flower-195893/"</span>,</span><br><span class="line"> <span class="attr">"type"</span>: <span class="string">"photo"</span>,</span><br><span class="line"> <span class="attr">"tags"</span>: <span class="string">"blossom, bloom, flower"</span>,</span><br><span class="line"> <span class="attr">"previewURL"</span>: <span class="string">"https://cdn.pixabay.com/photo/2013/10/15/09/12/flower-195893_150.jpg"</span>,</span><br><span class="line"> <span class="attr">"previewWidth"</span>: <span class="number">150</span>,</span><br><span class="line"> <span class="attr">"previewHeight"</span>: <span class="number">84</span>,</span><br><span class="line"> <span class="attr">"webformatURL"</span>: <span class="string">"https://pixabay.com/get/35bbf209e13e39d2_640.jpg"</span>,</span><br><span class="line"> <span class="attr">"webformatWidth"</span>: <span class="number">640</span>,</span><br><span class="line"> <span class="attr">"webformatHeight"</span>: <span class="number">360</span>,</span><br><span class="line"> <span class="attr">"largeImageURL"</span>: <span class="string">"https://pixabay.com/get/ed6a99fd0a76647_1280.jpg"</span>,</span><br><span class="line"> <span class="attr">"fullHDURL"</span>: <span class="string">"https://pixabay.com/get/ed6a9369fd0a76647_1920.jpg"</span>,</span><br><span class="line"> <span class="attr">"imageURL"</span>: <span class="string">"https://pixabay.com/get/ed6a9364a9fd0a76647.jpg"</span>,</span><br><span class="line"> <span class="attr">"imageWidth"</span>: <span class="number">4000</span>,</span><br><span class="line"> <span class="attr">"imageHeight"</span>: <span class="number">2250</span>,</span><br><span class="line"> <span class="attr">"imageSize"</span>: <span class="number">4731420</span>,</span><br><span class="line"> <span class="attr">"views"</span>: <span class="number">7671</span>,</span><br><span class="line"> <span class="attr">"downloads"</span>: <span class="number">6439</span>,</span><br><span class="line"> <span class="attr">"favorites"</span>: <span class="number">1</span>,</span><br><span class="line"> <span class="attr">"likes"</span>: <span class="number">5</span>,</span><br><span class="line"> <span class="attr">"comments"</span>: <span class="number">2</span>,</span><br><span class="line"> <span class="attr">"user_id"</span>: <span class="number">48777</span>,</span><br><span class="line"> <span class="attr">"user"</span>: <span class="string">"Josch13"</span>,</span><br><span class="line"> <span class="attr">"userImageURL"</span>: <span class="string">"https://cdn.pixabay.com/user/2013/11/05/02-10-23-764_250x250.jpg"</span></span><br><span class="line"> },</span><br><span class="line"> {</span><br><span class="line"> <span class="attr">"id"</span>: <span class="number">73424</span></span><br><span class="line"> }</span><br><span class="line">]</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h5 id="在这些里面我们需要的是-图片的类型、图片的标签、图片的大图链接-,由于最近在学习数据分析,于是我也爬取了图片的下载量、收藏量以及被标记为爱好的量"><a href="#在这些里面我们需要的是-图片的类型、图片的标签、图片的大图链接-,由于最近在学习数据分析,于是我也爬取了图片的下载量、收藏量以及被标记为爱好的量" class="headerlink" title="在这些里面我们需要的是 图片的类型、图片的标签、图片的大图链接 ,由于最近在学习数据分析,于是我也爬取了图片的下载量、收藏量以及被标记为爱好的量"></a>在这些里面我们需要的是 <strong>图片的类型、图片的标签、图片的大图链接</strong> ,由于最近在学习数据分析,于是我也爬取了图片的<strong>下载量、收藏量以及被标记为爱好的量</strong></h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">data = {'type': picture['type'],</span><br><span class="line"> 'tags': picture['tags'],</span><br><span class="line"> 'largeImageURL': picture['largeImageURL'],</span><br><span class="line"> 'downloads': picture['downloads'], # 下载总数</span><br><span class="line"> 'favorites': picture['favorites'], # 收藏夹总数</span><br><span class="line"> 'like': picture['likes'], # 点赞总数</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h5 id="然后就是对大体框架的编写"><a href="#然后就是对大体框架的编写" class="headerlink" title="然后就是对大体框架的编写"></a>然后就是对大体框架的编写</h5><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> requests</span><br><span class="line"><span class="keyword">import</span> json</span><br><span class="line"><span class="keyword">from</span> pymongo <span class="keyword">import</span> MongoClient</span><br><span class="line"><span class="keyword">import</span> time</span><br><span class="line"><span class="keyword">import</span> csv</span><br><span class="line"><span class="keyword">import</span> pymysql</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">get_json</span><span class="params">(page, condition, image_type)</span>:</span></span><br><span class="line"> page += <span class="number">1</span></span><br><span class="line"> key = <span class="string">''</span></span><br><span class="line"> url = <span class="string">'https://pixabay.com/api/'</span></span><br><span class="line"></span><br><span class="line"> pa = {</span><br><span class="line"> <span class="string">'key'</span>: key,</span><br><span class="line"> <span class="string">'q'</span>: condition,</span><br><span class="line"> <span class="string">'image_type'</span>: image_type,</span><br><span class="line"> <span class="string">'lang'</span>: <span class="string">'zh'</span>,</span><br><span class="line"> <span class="string">'page'</span>: page,</span><br><span class="line"> <span class="string">'per_page'</span>: <span class="number">200</span>,</span><br><span class="line"> }</span><br><span class="line"> r = requests.get(url, params=pa, headers=headers)</span><br><span class="line"> decode_json = json.loads(r.text)</span><br><span class="line"> <span class="keyword">return</span> decode_json</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">page = <span class="number">0</span></span><br><span class="line">id = <span class="number">0</span></span><br><span class="line">headers = {<span class="string">'User-Agent'</span>: <span class="string">'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '</span></span><br><span class="line"> <span class="string">'Chrome/78.0.3904.97 Safari/537.36'</span>}</span><br><span class="line">condition = input(<span class="string">"请输入你的搜索条件(多个条件以+连接): "</span>)</span><br><span class="line">image_type = input(<span class="string">"请输入搜索图片类型( 'all, photo, illustration插图, vector矢量'): "</span>)</span><br><span class="line">number = input(<span class="string">'请输入想要查询的图片页数num (num*200每页200条数据): '</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 存入 Nosql 数据库</span></span><br><span class="line">client = MongoClient(<span class="string">'localhost'</span>, <span class="number">27017</span>)</span><br><span class="line">db = client.pixabay_database</span><br><span class="line">collection = db.pixabay</span><br><span class="line"><span class="keyword">for</span> _ <span class="keyword">in</span> range(<span class="number">1</span>, int(number)):</span><br><span class="line"> decode_json = get_json(page, condition, image_type)</span><br><span class="line"> <span class="keyword">for</span> picture <span class="keyword">in</span> decode_json[<span class="string">'hits'</span>]:</span><br><span class="line"> id += <span class="number">1</span></span><br><span class="line"> data = {<span class="string">'id'</span>: id,</span><br><span class="line"> <span class="string">'type'</span>: picture[<span class="string">'type'</span>],</span><br><span class="line"> <span class="string">'tags'</span>: picture[<span class="string">'tags'</span>],</span><br><span class="line"> <span class="string">'largeImageURL'</span>: picture[<span class="string">'largeImageURL'</span>],</span><br><span class="line"> <span class="string">'downloads'</span>: picture[<span class="string">'downloads'</span>], <span class="comment"># 下载总数</span></span><br><span class="line"> <span class="string">'favorites'</span>: picture[<span class="string">'favorites'</span>], <span class="comment"># 收藏夹总数</span></span><br><span class="line"> <span class="string">'like'</span>: picture[<span class="string">'likes'</span>], <span class="comment"># 点赞总数</span></span><br><span class="line"> }</span><br><span class="line"> print(<span class="string">'开始存入数据库'</span>)</span><br><span class="line"> collection.insert_one(data)</span><br><span class="line"><span class="comment"># 存入Mysql数据库</span></span><br><span class="line"><span class="string">'''db = pymysql.connect(host='localhost',</span></span><br><span class="line"><span class="string"> user="root",</span></span><br><span class="line"><span class="string"> passwd="root",</span></span><br><span class="line"><span class="string"> port=3306,</span></span><br><span class="line"><span class="string"> db='pixabay',</span></span><br><span class="line"><span class="string"> charset='utf8')</span></span><br><span class="line"><span class="string">cursor = db.cursor()</span></span><br><span class="line"><span class="string">for _ in range(1, int(number)):</span></span><br><span class="line"><span class="string"> decode_json = get_json(page, condition, image_type)</span></span><br><span class="line"><span class="string"> for picture in decode_json['hits']:</span></span><br><span class="line"><span class="string"> id += 1</span></span><br><span class="line"><span class="string"> data = {'id': id,</span></span><br><span class="line"><span class="string"> 'type': picture['type'],</span></span><br><span class="line"><span class="string"> 'tags': picture['tags'],</span></span><br><span class="line"><span class="string"> 'largeImageURL': picture['largeImageURL'],</span></span><br><span class="line"><span class="string"> 'downloads': picture['downloads'], # 下载总数</span></span><br><span class="line"><span class="string"> 'favorites': picture['favorites'], # 收藏夹总数</span></span><br><span class="line"><span class="string"> 'like': picture['likes'], # 点赞总数</span></span><br><span class="line"><span class="string"> }</span></span><br><span class="line"><span class="string"> print('开始存入数据库')</span></span><br><span class="line"><span class="string"> sql = """INSERT INTO pixabay</span></span><br><span class="line"><span class="string"> (type, tags, largeImageURL, downloads, favorites, likes)</span></span><br><span class="line"><span class="string"> VALUES</span></span><br><span class="line"><span class="string"> (%s, %s, %s, %s, %s, %s)</span></span><br><span class="line"><span class="string"> """</span></span><br><span class="line"><span class="string"> cursor.execute(sql, (</span></span><br><span class="line"><span class="string"> picture['type'], picture['tags'], picture['largeImageURL'], picture['downloads'], picture['favorites'],</span></span><br><span class="line"><span class="string"> picture['likes']))</span></span><br><span class="line"><span class="string"> db.commit()</span></span><br><span class="line"><span class="string"># print(data)</span></span><br><span class="line"><span class="string">cursor.close()</span></span><br><span class="line"><span class="string">db.close()'''</span></span><br><span class="line"><span class="comment"># 存为 CSV 格式</span></span><br><span class="line"><span class="string">'''with open('/helper_hu/analysis_pixabay/analysis_pixabay.csv', 'a+', encoding='UTF-8', newline='') as csvfile:</span></span><br><span class="line"><span class="string"> head = ['id', 'type', 'tags', 'largeImageURL', 'downloads', 'favorites', 'like']</span></span><br><span class="line"><span class="string"> writer = csv.writer(csvfile)</span></span><br><span class="line"><span class="string"> writer.writerow(head)</span></span><br><span class="line"><span class="string"> for _ in range(1, int(number)):</span></span><br><span class="line"><span class="string"> decode_json = get_json(page, condition, image_type)</span></span><br><span class="line"><span class="string"> for picture in decode_json['hits']:</span></span><br><span class="line"><span class="string"> id += 1</span></span><br><span class="line"><span class="string"> data = {'id': id,</span></span><br><span class="line"><span class="string"> 'type': picture['type'],</span></span><br><span class="line"><span class="string"> 'tags': picture['tags'],</span></span><br><span class="line"><span class="string"> 'largeImageURL': picture['largeImageURL'],</span></span><br><span class="line"><span class="string"> 'downloads': picture['downloads'], # 下载总数</span></span><br><span class="line"><span class="string"> 'favorites': picture['favorites'], # 收藏夹总数</span></span><br><span class="line"><span class="string"> 'like': picture['likes'], # 点赞总数</span></span><br><span class="line"><span class="string"> }</span></span><br><span class="line"><span class="string"> # head = list(data.keys())</span></span><br><span class="line"><span class="string"> rows = list(data.values())</span></span><br><span class="line"><span class="string"> writer.writerow(rows)</span></span><br><span class="line"><span class="string"> print('写入数据id={}'.format(id))</span></span><br><span class="line"><span class="string"> time.sleep(1.0)</span></span><br><span class="line"><span class="string">'''</span></span><br></pre></td></tr></table></figure><p><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/mysql_img.png" alt><br><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/MongoDB_img.png" alt><br><img src="https://cdn.jsdelivr.net/gh/Forgotten-Forever/BlogImages/images/csv_img.png" alt></p><h6 id="小结:分别写了3种存储方式-MySQL、MangoDB以及CSV,之后我用读取CSV-数据的方法简单分析了一类图片下用户最喜欢的图片以及下载量最多的。"><a href="#小结:分别写了3种存储方式-MySQL、MangoDB以及CSV,之后我用读取CSV-数据的方法简单分析了一类图片下用户最喜欢的图片以及下载量最多的。" class="headerlink" title="小结:分别写了3种存储方式 MySQL、MangoDB以及CSV,之后我用读取CSV 数据的方法简单分析了一类图片下用户最喜欢的图片以及下载量最多的。"></a>小结:分别写了3种存储方式 MySQL、MangoDB以及CSV,之后我用读取CSV 数据的方法简单分析了一类图片下用户最喜欢的图片以及下载量最多的。</h6>]]></content>
<tags>
<tag> scrapy_study </tag>
</tags>
</entry>
<entry>
<title>pyecharts_series_options</title>
<link href="/2020/07/30/pyecharts-series-options/"/>
<url>/2020/07/30/pyecharts-series-options/</url>
<content type="html"><![CDATA[<h2 id="pyecharts-配置项"><a href="#pyecharts-配置项" class="headerlink" title="pyecharts 配置项"></a>pyecharts 配置项</h2><p><a href="http://pyecharts.org/#/zh-cn/" target="_blank" rel="noopener">全部来自于官方文档,主要用于方便自己查找</a><br><strong>系列配置项 (按常用顺序排序)</strong></p><ol><li><p><code>ItemStyleOpts:图元样式配置项</code></p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br></pre></td><td class="code"><pre><span class="line">class ItemStyleOpts(</span><br><span class="line"> # 图形的颜色。</span><br><span class="line"> # 颜色可以使用 RGB 表示,比如 'rgb(128, 128, 128)',如果想要加上 alpha 通道表示不透明度,</span><br><span class="line"> # 可以使用 RGBA,比如 'rgba(128, 128, 128, 0.5)',也可以使用十六进制格式,比如 '#ccc'。</span><br><span class="line"> # 除了纯色之外颜色也支持渐变色和纹理填充</span><br><span class="line"> # </span><br><span class="line"> # 线性渐变,前四个参数分别是 x0, y0, x2, y2, 范围从 0 - 1,相当于在图形包围盒中的百分比,</span><br><span class="line"> # 如果 globalCoord 为 `true`,则该四个值是绝对的像素位置</span><br><span class="line"> # color: {</span><br><span class="line"> # type: 'linear',</span><br><span class="line"> # x: 0,</span><br><span class="line"> # y: 0,</span><br><span class="line"> # x2: 0,</span><br><span class="line"> # y2: 1,</span><br><span class="line"> # colorStops: [{</span><br><span class="line"> # offset: 0, color: 'red' // 0% 处的颜色</span><br><span class="line"> # }, {</span><br><span class="line"> # offset: 1, color: 'blue' // 100% 处的颜色</span><br><span class="line"> # }],</span><br><span class="line"> # global: false // 缺省为 false</span><br><span class="line"> # }</span><br><span class="line"> # </span><br><span class="line"> # 径向渐变,前三个参数分别是圆心 x, y 和半径,取值同线性渐变</span><br><span class="line"> # color: {</span><br><span class="line"> # type: 'radial',</span><br><span class="line"> # x: 0.5,</span><br><span class="line"> # y: 0.5,</span><br><span class="line"> # r: 0.5,</span><br><span class="line"> # colorStops: [{</span><br><span class="line"> # offset: 0, color: 'red' // 0% 处的颜色</span><br><span class="line"> # }, {</span><br><span class="line"> # offset: 1, color: 'blue' // 100% 处的颜色</span><br><span class="line"> # }],</span><br><span class="line"> # global: false // 缺省为 false</span><br><span class="line"> # }</span><br><span class="line"> # </span><br><span class="line"> # 纹理填充</span><br><span class="line"> # color: {</span><br><span class="line"> # image: imageDom, // 支持为 HTMLImageElement, HTMLCanvasElement,不支持路径字符串</span><br><span class="line"> # repeat: 'repeat' // 是否平铺, 可以是 'repeat-x', 'repeat-y', 'no-repeat'</span><br><span class="line"> # }</span><br><span class="line"> color: Optional[str] = None,</span><br><span class="line"> </span><br><span class="line"> # 阴线 图形的颜色。</span><br><span class="line"> color0: Optional[str] = None,</span><br><span class="line"> </span><br><span class="line"> # 图形的描边颜色。支持的颜色格式同 color,不支持回调函数。</span><br><span class="line"> border_color: Optional[str] = None,</span><br><span class="line"> </span><br><span class="line"> # 阴线 图形的描边颜色。</span><br><span class="line"> border_color0: Optional[str] = None,</span><br><span class="line"> </span><br><span class="line"> # 描边宽度,默认不描边。</span><br><span class="line"> border_width: Optional[Numeric] = None,</span><br><span class="line"> </span><br><span class="line"> # 支持 'dashed', 'dotted'。</span><br><span class="line"> border_type: Optional[str] = None,</span><br><span class="line"> </span><br><span class="line"> # 图形透明度。支持从 0 到 1 的数字,为 0 时不绘制该图形。</span><br><span class="line"> opacity: Optional[Numeric] = None,</span><br><span class="line"> </span><br><span class="line"> # 区域的颜色。 </span><br><span class="line"> area_color: Optional[str] = None,</span><br><span class="line"> )</span><br></pre></td></tr></table></figure></li><li><p><code>TextStyleOpts:文字样式配置项</code></p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br></pre></td><td class="code"><pre><span class="line">class TextStyleOpts(</span><br><span class="line"> # 文字颜色。</span><br><span class="line"> color: Optional[str] = None,</span><br><span class="line"> </span><br><span class="line"> # 文字字体的风格</span><br><span class="line"> # 可选:'normal','italic','oblique'</span><br><span class="line"> font_style: Optional[str] = None,</span><br><span class="line"> </span><br><span class="line"> # 主标题文字字体的粗细,可选:</span><br><span class="line"> # 'normal','bold','bolder','lighter'</span><br><span class="line"> font_weight: Optional[str] = None,</span><br><span class="line"> </span><br><span class="line"> # 文字的字体系列</span><br><span class="line"> # 还可以是 'serif' , 'monospace', 'Arial', 'Courier New', 'Microsoft YaHei', ...</span><br><span class="line"> font_family: Optional[str] = None,</span><br><span class="line"> </span><br><span class="line"> # 文字的字体大小</span><br><span class="line"> font_size: Optional[Numeric] = None,</span><br><span class="line"> </span><br><span class="line"> # 文字水平对齐方式,默认自动</span><br><span class="line"> align: Optional[str] = None,</span><br><span class="line"> </span><br><span class="line"> # 文字垂直对齐方式,默认自动</span><br><span class="line"> vertical_align: Optional[str] = None,</span><br><span class="line"> </span><br><span class="line"> # 行高</span><br><span class="line"> line_height: Optional[str] = None,</span><br><span class="line"> </span><br><span class="line"> # 文字块背景色。可以是直接的颜色值,例如:'#123234', 'red', 'rgba(0,23,11,0.3)'</span><br><span class="line"> background_color: Optional[str] = None,</span><br><span class="line"> </span><br><span class="line"> # 文字块边框颜色</span><br><span class="line"> border_color: Optional[str] = None,</span><br><span class="line"> </span><br><span class="line"> # 文字块边框宽度</span><br><span class="line"> border_width: Optional[Numeric] = None,</span><br><span class="line"> </span><br><span class="line"> # 文字块的圆角</span><br><span class="line"> border_radius: Union[Numeric, Sequence, None] = None,</span><br><span class="line"> </span><br><span class="line"> # 文字块的内边距 </span><br><span class="line"> # 例如 padding: [3, 4, 5, 6]:表示 [上, 右, 下, 左] 的边距</span><br><span class="line"> # 例如 padding: 4:表示 padding: [4, 4, 4, 4]</span><br><span class="line"> # 例如 padding: [3, 4]:表示 padding: [3, 4, 3, 4]</span><br><span class="line"> padding: Union[Numeric, Sequence, None] = None,</span><br><span class="line"> </span><br><span class="line"> # 文字块的背景阴影颜色</span><br><span class="line"> shadow_color: Optional[str] = None,</span><br><span class="line"> </span><br><span class="line"> # 文字块的背景阴影长度</span><br><span class="line"> shadow_blur: Optional[Numeric] = None,</span><br><span class="line"> </span><br><span class="line"> # 文字块的宽度</span><br><span class="line"> width: Optional[str] = None,</span><br><span class="line"> </span><br><span class="line"> # 文字块的高度</span><br><span class="line"> height: Optional[str] = None,</span><br><span class="line"> </span><br><span class="line"> # 在 rich 里面,可以自定义富文本样式。利用富文本样式,可以在标签中做出非常丰富的效果</span><br><span class="line"> # 具体配置可以参考一下 https://www.echartsjs.com/tutorial.html#%E5%AF%8C%E6%96%87%E6%9C%AC%E6%A0%87%E7%AD%BE</span><br><span class="line"> rich: Optional[dict] = None,</span><br><span class="line"> )</span><br></pre></td></tr></table></figure></li><li><p><code>LabelOpts:标签配置项</code></p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br></pre></td><td class="code"><pre><span class="line">class LabelOpts(</span><br><span class="line"> # 是否显示标签。</span><br><span class="line"> is_show: bool = True,</span><br><span class="line"></span><br><span class="line"> # 标签的位置。可选</span><br><span class="line"> # 'top','left','right','bottom','inside','insideLeft','insideRight'</span><br><span class="line"> # 'insideTop','insideBottom', 'insideTopLeft','insideBottomLeft'</span><br><span class="line"> # 'insideTopRight','insideBottomRight'</span><br><span class="line"> position: Union[str, Sequence] = "top",</span><br><span class="line"></span><br><span class="line"> # 文字的颜色。</span><br><span class="line"> # 如果设置为 'auto',则为视觉映射得到的颜色,如系列色。</span><br><span class="line"> color: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 距离图形元素的距离。当 position 为字符描述值(如 'top'、'insideRight')时候有效。</span><br><span class="line"> distance: Union[Numeric, Sequence, None] = None,</span><br><span class="line"></span><br><span class="line"> # 文字的字体大小</span><br><span class="line"> font_size: Numeric = 12,</span><br><span class="line"></span><br><span class="line"> # 文字字体的风格,可选:</span><br><span class="line"> # 'normal','italic','oblique'</span><br><span class="line"> font_style: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 文字字体的粗细,可选:</span><br><span class="line"> # 'normal','bold','bolder','lighter'</span><br><span class="line"> font_weight: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 文字的字体系列</span><br><span class="line"> # 还可以是 'serif' , 'monospace', 'Arial', 'Courier New', 'Microsoft YaHei', ...</span><br><span class="line"> font_family: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 标签旋转。从 -90 度到 90 度。正值是逆时针。</span><br><span class="line"> rotate: Optional[Numeric] = None,</span><br><span class="line"></span><br><span class="line"> # 刻度标签与轴线之间的距离。</span><br><span class="line"> margin: Optional[Numeric] = 8,</span><br><span class="line"></span><br><span class="line"> # 坐标轴刻度标签的显示间隔,在类目轴中有效。</span><br><span class="line"> # 默认会采用标签不重叠的策略间隔显示标签。</span><br><span class="line"> # 可以设置成 0 强制显示所有标签。</span><br><span class="line"> # 如果设置为 1,表示『隔一个标签显示一个标签』,如果值为 2,表示隔两个标签显示一个标签,以此类推。</span><br><span class="line"> # 可以用数值表示间隔的数据,也可以通过回调函数控制。回调函数格式如下:</span><br><span class="line"> # (index:number, value: string) => boolean</span><br><span class="line"> # 第一个参数是类目的 index,第二个值是类目名称,如果跳过则返回 false。</span><br><span class="line"> interval: Union[Numeric, str, None]= None,</span><br><span class="line"></span><br><span class="line"> # 文字水平对齐方式,默认自动。可选:</span><br><span class="line"> # 'left','center','right'</span><br><span class="line"> horizontal_align: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 文字垂直对齐方式,默认自动。可选:</span><br><span class="line"> # 'top','middle','bottom'</span><br><span class="line"> vertical_align: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 标签内容格式器,支持字符串模板和回调函数两种形式,字符串模板与回调函数返回的字符串均支持用 \n 换行。</span><br><span class="line"> # 模板变量有 {a}, {b},{c},{d},{e},分别表示系列名,数据名,数据值等。 </span><br><span class="line"> # 在 trigger 为 'axis' 的时候,会有多个系列的数据,此时可以通过 {a0}, {a1}, {a2} 这种后面加索引的方式表示系列的索引。 </span><br><span class="line"> # 不同图表类型下的 {a},{b},{c},{d} 含义不一样。 其中变量{a}, {b}, {c}, {d}在不同图表类型下代表数据含义为:</span><br><span class="line"></span><br><span class="line"> # 折线(区域)图、柱状(条形)图、K线图 : {a}(系列名称),{b}(类目值),{c}(数值), {d}(无)</span><br><span class="line"> # 散点图(气泡)图 : {a}(系列名称),{b}(数据名称),{c}(数值数组), {d}(无)</span><br><span class="line"> # 地图 : {a}(系列名称),{b}(区域名称),{c}(合并数值), {d}(无)</span><br><span class="line"> # 饼图、仪表盘、漏斗图: {a}(系列名称),{b}(数据项名称),{c}(数值), {d}(百分比)</span><br><span class="line"> # 示例:formatter: '{b}: {@score}'</span><br><span class="line"> # </span><br><span class="line"> # 回调函数,回调函数格式:</span><br><span class="line"> # (params: Object|Array) => string</span><br><span class="line"> # 参数 params 是 formatter 需要的单个数据集。格式如下:</span><br><span class="line"> # {</span><br><span class="line"> # componentType: 'series',</span><br><span class="line"> # // 系列类型</span><br><span class="line"> # seriesType: string,</span><br><span class="line"> # // 系列在传入的 option.series 中的 index</span><br><span class="line"> # seriesIndex: number,</span><br><span class="line"> # // 系列名称</span><br><span class="line"> # seriesName: string,</span><br><span class="line"> # // 数据名,类目名</span><br><span class="line"> # name: string,</span><br><span class="line"> # // 数据在传入的 data 数组中的 index</span><br><span class="line"> # dataIndex: number,</span><br><span class="line"> # // 传入的原始数据项</span><br><span class="line"> # data: Object,</span><br><span class="line"> # // 传入的数据值</span><br><span class="line"> # value: number|Array,</span><br><span class="line"> # // 数据图形的颜色</span><br><span class="line"> # color: string,</span><br><span class="line"> # }</span><br><span class="line"> formatter: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 在 rich 里面,可以自定义富文本样式。利用富文本样式,可以在标签中做出非常丰富的效果</span><br><span class="line"> # 具体配置可以参考一下 https://www.echartsjs.com/tutorial.html#%E5%AF%8C%E6%96%87%E6%9C%AC%E6%A0%87%E7%AD%BE</span><br><span class="line"> rich: Optional[dict] = None,</span><br><span class="line">)</span><br></pre></td></tr></table></figure></li><li><p><code>LineStyleOpts:线样式配置项</code></p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br></pre></td><td class="code"><pre><span class="line">class LineStyleOpts(</span><br><span class="line"> # 是否显示</span><br><span class="line"> is_show: bool = True,</span><br><span class="line"></span><br><span class="line"> # 线宽。</span><br><span class="line"> width: Numeric = 1,</span><br><span class="line"></span><br><span class="line"> # 图形透明度。支持从 0 到 1 的数字,为 0 时不绘制该图形。</span><br><span class="line"> opacity: Numeric = 1,</span><br><span class="line"></span><br><span class="line"> # 线的弯曲度,0 表示完全不弯曲</span><br><span class="line"> curve: Numeric = 0,</span><br><span class="line"></span><br><span class="line"> # 线的类型。可选:</span><br><span class="line"> # 'solid', 'dashed', 'dotted'</span><br><span class="line"> type_: str = "solid",</span><br><span class="line"></span><br><span class="line"> # 线的颜色。</span><br><span class="line"> # 颜色可以使用 RGB 表示,比如 'rgb(128, 128, 128)',如果想要加上 alpha 通道表示不透明度,</span><br><span class="line"> # 可以使用 RGBA,比如 'rgba(128, 128, 128, 0.5)',也可以使用十六进制格式,比如 '#ccc'。</span><br><span class="line"> # 除了纯色之外颜色也支持渐变色和纹理填充</span><br><span class="line"> # </span><br><span class="line"> # 线性渐变,前四个参数分别是 x0, y0, x2, y2, 范围从 0 - 1,相当于在图形包围盒中的百分比,</span><br><span class="line"> # 如果 globalCoord 为 `true`,则该四个值是绝对的像素位置</span><br><span class="line"> # color: {</span><br><span class="line"> # type: 'linear',</span><br><span class="line"> # x: 0,</span><br><span class="line"> # y: 0,</span><br><span class="line"> # x2: 0,</span><br><span class="line"> # y2: 1,</span><br><span class="line"> # colorStops: [{</span><br><span class="line"> # offset: 0, color: 'red' // 0% 处的颜色</span><br><span class="line"> # }, {</span><br><span class="line"> # offset: 1, color: 'blue' // 100% 处的颜色</span><br><span class="line"> # }],</span><br><span class="line"> # global: false // 缺省为 false</span><br><span class="line"> # }</span><br><span class="line"> # </span><br><span class="line"> # 径向渐变,前三个参数分别是圆心 x, y 和半径,取值同线性渐变</span><br><span class="line"> # color: {</span><br><span class="line"> # type: 'radial',</span><br><span class="line"> # x: 0.5,</span><br><span class="line"> # y: 0.5,</span><br><span class="line"> # r: 0.5,</span><br><span class="line"> # colorStops: [{</span><br><span class="line"> # offset: 0, color: 'red' // 0% 处的颜色</span><br><span class="line"> # }, {</span><br><span class="line"> # offset: 1, color: 'blue' // 100% 处的颜色</span><br><span class="line"> # }],</span><br><span class="line"> # global: false // 缺省为 false</span><br><span class="line"> # }</span><br><span class="line"> # </span><br><span class="line"> # 纹理填充</span><br><span class="line"> # color: {</span><br><span class="line"> # image: imageDom, // 支持为 HTMLImageElement, HTMLCanvasElement,不支持路径字符串</span><br><span class="line"> # repeat: 'repeat' // 是否平铺, 可以是 'repeat-x', 'repeat-y', 'no-repeat'</span><br><span class="line"> # }</span><br><span class="line"> color: Union[str, Sequence, None] = None,</span><br><span class="line">)</span><br></pre></td></tr></table></figure></li><li><p><code>Lines3DEffectOpts:3D线样式配置项</code></p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">class Line3DEffectOpts(</span><br><span class="line"> # 是否显示尾迹特效,默认不显示。</span><br><span class="line"> is_show: bool = True,</span><br><span class="line"></span><br><span class="line"> # 尾迹特效的周期。</span><br><span class="line"> period: Numeric = 4,</span><br><span class="line"></span><br><span class="line"> # 轨迹特效的移动动画是否是固定速度,单位按三维空间的尺寸</span><br><span class="line"> # 设置为非 null 的值后会忽略 period 配置项。</span><br><span class="line"> constant_speed: Optional[Numeric] = None,</span><br><span class="line"></span><br><span class="line"> # 尾迹的宽度。</span><br><span class="line"> trail_width: Numeric = 4,</span><br><span class="line"></span><br><span class="line"> # 尾迹的长度,范围从 0 到 1,为线条长度的百分比。</span><br><span class="line"> trail_length: Numeric = 0.1,</span><br><span class="line"></span><br><span class="line"> # 尾迹的颜色,默认跟线条颜色相同。</span><br><span class="line"> trail_color: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 尾迹的不透明度,默认跟线条不透明度相同。</span><br><span class="line"> trail_opacity: Optional[Numeric] = None,</span><br><span class="line">)</span><br></pre></td></tr></table></figure></li><li><p><code>SplitLineOpts:分割线配置项</code></p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">class SplitLineOpts(</span><br><span class="line"> # 是否显示分割线</span><br><span class="line"> is_show: bool = False,</span><br><span class="line"></span><br><span class="line"> # 线风格配置项,参考 `series_options.SplitLineOpts`</span><br><span class="line"> linestyle_opts: LineStyleOpts = LineStyleOpts()</span><br><span class="line">)</span><br></pre></td></tr></table></figure></li><li><p><code>MarkPointItem:标记点数据项</code></p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line">class MarkPointItem(</span><br><span class="line"> # 标注名称。</span><br><span class="line"> name: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 特殊的标注类型,用于标注最大值最小值等。可选:</span><br><span class="line"> # 'min' 最大值。</span><br><span class="line"> # 'max' 最大值。</span><br><span class="line"> # 'average' 平均值。</span><br><span class="line"> type_: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 在使用 type 时有效,用于指定在哪个维度上指定最大值最小值,可以是 </span><br><span class="line"> # 0(xAxis, radiusAxis),</span><br><span class="line"> # 1(yAxis, angleAxis),默认使用第一个数值轴所在的维度。</span><br><span class="line"> value_index: Optional[Numeric] = None,</span><br><span class="line"></span><br><span class="line"> # 在使用 type 时有效,用于指定在哪个维度上指定最大值最小值。这可以是维度的直接名称,</span><br><span class="line"> # 例如折线图时可以是 x、angle 等、candlestick 图时可以是 open、close 等维度名称。</span><br><span class="line"> value_dim: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 标注的坐标。坐标格式视系列的坐标系而定,可以是直角坐标系上的 x, y,</span><br><span class="line"> # 也可以是极坐标系上的 radius, angle。例如 [121, 2323]、['aa', 998]。</span><br><span class="line"> coord: Optional[Sequence] = None,</span><br><span class="line"></span><br><span class="line"> # 相对容器的屏幕 x 坐标,单位像素。</span><br><span class="line"> x: Optional[Numeric] = None,</span><br><span class="line"></span><br><span class="line"> # 相对容器的屏幕 y 坐标,单位像素。</span><br><span class="line"> y: Optional[Numeric] = None,</span><br><span class="line"></span><br><span class="line"> # 标注值,可以不设。</span><br><span class="line"> value: Optional[Numeric] = None,</span><br><span class="line"></span><br><span class="line"> # 标记的图形。</span><br><span class="line"> # ECharts 提供的标记类型包括 'circle', 'rect', 'roundRect', 'triangle', </span><br><span class="line"> # 'diamond', 'pin', 'arrow', 'none'</span><br><span class="line"> # 可以通过 'image://url' 设置为图片,其中 URL 为图片的链接,或者 dataURI。</span><br><span class="line"> symbol: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 标记的大小,可以设置成诸如 10 这样单一的数字,也可以用数组分开表示宽和高,</span><br><span class="line"> # 例如 [20, 10] 表示标记宽为 20,高为 10。</span><br><span class="line"> symbol_size: Union[Numeric, Sequence] = None,</span><br><span class="line"></span><br><span class="line"> # 标记点样式配置项,参考 `series_options.ItemStyleOpts`</span><br><span class="line"> itemstyle_opts: Union[ItemStyleOpts, dict, None] = None,</span><br><span class="line">)</span><br></pre></td></tr></table></figure></li><li><p><code>MarkPointOpts:标记点配置项</code></p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">class MarkPointOpts(</span><br><span class="line"> # 标记点数据,参考 `series_options.MarkPointItem`</span><br><span class="line"> data: Sequence[Union[MarkPointItem, dict]] = None,</span><br><span class="line"></span><br><span class="line"> # 标记的图形。</span><br><span class="line"> # ECharts 提供的标记类型包括 'circle', 'rect', 'roundRect', 'triangle', </span><br><span class="line"> # 'diamond', 'pin', 'arrow', 'none'</span><br><span class="line"> # 可以通过 'image://url' 设置为图片,其中 URL 为图片的链接,或者 dataURI。</span><br><span class="line"> symbol: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 标记的大小,可以设置成诸如 10 这样单一的数字,也可以用数组分开表示宽和高,</span><br><span class="line"> # 例如 [20, 10] 表示标记宽为 20,高为 10。</span><br><span class="line"> # 如果需要每个数据的图形大小不一样,可以设置为如下格式的回调函数:</span><br><span class="line"> # (value: Array|number, params: Object) => number|Array</span><br><span class="line"> # 其中第一个参数 value 为 data 中的数据值。第二个参数 params 是其它的数据项参数。</span><br><span class="line"> symbol_size: Union[None, Numeric] = None,</span><br><span class="line"></span><br><span class="line"> # 标签配置项,参考 `series_options.LabelOpts`</span><br><span class="line"> label_opts: LabelOpts = LabelOpts(position="inside", color="#fff"),</span><br><span class="line">)</span><br></pre></td></tr></table></figure></li><li><p><code>MarkLineItem:标记线数据项</code></p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line">class MarkLineItem(</span><br><span class="line"> # 标注名称。</span><br><span class="line"> name: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 特殊的标注类型,用于标注最大值最小值等。可选:</span><br><span class="line"> # 'min' 最大值。</span><br><span class="line"> # 'max' 最大值。</span><br><span class="line"> # 'average' 平均值。</span><br><span class="line"> type_: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 相对容器的屏幕 x 坐标,单位像素。</span><br><span class="line"> x: Union[str, Numeric, None] = None,</span><br><span class="line"></span><br><span class="line"> # 相对容器的屏幕 y 坐标,单位像素。</span><br><span class="line"> y: Union[str, Numeric, None] = None,</span><br><span class="line"></span><br><span class="line"> # 在使用 type 时有效,用于指定在哪个维度上指定最大值最小值,可以是 </span><br><span class="line"> # 0(xAxis, radiusAxis),</span><br><span class="line"> # 1(yAxis, angleAxis),默认使用第一个数值轴所在的维度。</span><br><span class="line"> value_index: Optional[Numeric] = None,</span><br><span class="line"></span><br><span class="line"> # 在使用 type 时有效,用于指定在哪个维度上指定最大值最小值。这可以是维度的直接名称,</span><br><span class="line"> # 例如折线图时可以是 x、angle 等、candlestick 图时可以是 open、close 等维度名称。</span><br><span class="line"> value_dim: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 起点或终点的坐标。坐标格式视系列的坐标系而定,可以是直角坐标系上的 x, y,</span><br><span class="line"> # 也可以是极坐标系上的 radius, angle。</span><br><span class="line"> coord: Optional[Sequence] = None,</span><br><span class="line"></span><br><span class="line"> # 终点标记的图形。</span><br><span class="line"> # ECharts 提供的标记类型包括 'circle', 'rect', 'roundRect', 'triangle',</span><br><span class="line"> # 'diamond', 'pin', 'arrow', 'none'</span><br><span class="line"> # 可以通过 'image://url' 设置为图片,其中 URL 为图片的链接,或者 dataURI。</span><br><span class="line"> symbol: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 标记的大小,可以设置成诸如 10 这样单一的数字,也可以用数组分开表示宽和高,</span><br><span class="line"> # 例如 [20, 10] 表示标记宽为 20,高为 10。</span><br><span class="line"> symbol_size: Optional[Numeric] = None,</span><br><span class="line">)</span><br></pre></td></tr></table></figure></li><li><p><code>MarkLineOpts:标记线配置项</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">class MarkLineOpts(</span><br><span class="line"> # 图形是否不响应和触发鼠标事件,默认为 false,即响应和触发鼠标事件。</span><br><span class="line"> is_silent: bool = False,</span><br><span class="line"></span><br><span class="line"> # 标记线数据,参考 `series_options.MarkLineItem`</span><br><span class="line"> data: Sequence[Union[MarkLineItem, dict]] = None,</span><br><span class="line"></span><br><span class="line"> # 标线两端的标记类型,可以是一个数组分别指定两端,也可以是单个统一指定,具体格式见 data.symbol。</span><br><span class="line"> symbol: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 标线两端的标记大小,可以是一个数组分别指定两端,也可以是单个统一指定。</span><br><span class="line"> symbol_size: Union[None, Numeric] = None,</span><br><span class="line"></span><br><span class="line"> # 标线数值的精度,在显示平均值线的时候有用。</span><br><span class="line"> precision: int = 2,</span><br><span class="line"></span><br><span class="line"> # 标签配置项,参考 `series_options.LabelOpts`</span><br><span class="line"> label_opts: LabelOpts = LabelOpts(),</span><br><span class="line"></span><br><span class="line"> # 标记线样式配置项,参考 `series_options.LineStyleOpts`</span><br><span class="line"> linestyle_opts: Union[LineStyleOpts, dict, None] = None,</span><br><span class="line">)</span><br></pre></td></tr></table></figure></li><li><p><code>MarkAreaItem:标记区域数据项</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">class MarkAreaItem(</span><br><span class="line"> # 区域名称, 仅仅就是一个名称而已</span><br><span class="line"> name: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 特殊的标注类型,用于标注最大值最小值等。</span><br><span class="line"> # 'min' 最大值。</span><br><span class="line"> # 'max' 最大值。</span><br><span class="line"> # 'average' 平均值。</span><br><span class="line"> type_: Sequence[Optional[str], Optional[str]] = (None, None),</span><br><span class="line"></span><br><span class="line"> # 在使用 type 时有效,用于指定在哪个维度上指定最大值最小值,可以是 0(xAxis, radiusAxis),1(yAxis, angleAxis)。</span><br><span class="line"> # 默认使用第一个数值轴所在的维度。</span><br><span class="line"> value_index: Sequence[Optional[Numeric], Optional[Numeric]] = (None, None),</span><br><span class="line"></span><br><span class="line"> # 在使用 type 时有效,用于指定在哪个维度上指定最大值最小值。</span><br><span class="line"> # 这可以是维度的直接名称,例如折线图时可以是 x、angle 等、candlestick 图时可以是 open、close 等维度名称。</span><br><span class="line"> value_dim: Sequence[Optional[str], Optional[str]] = (None, None),</span><br><span class="line"></span><br><span class="line"> # 相对容器的屏幕 x 坐标,单位像素,支持百分比形式,例如 '20%'。</span><br><span class="line"> x: Sequence[Union[str, Numeric, None], Union[str, Numeric, None]] = (None, None),</span><br><span class="line"></span><br><span class="line"> # 相对容器的屏幕 y 坐标,单位像素,支持百分比形式,例如 '20%'。</span><br><span class="line"> y: Sequence[Union[str, Numeric, None], Union[str, Numeric, None]] = (None, None),</span><br><span class="line"></span><br><span class="line"> # 标签配置项,参考 `series_options.LabelOpts`</span><br><span class="line"> label_opts: Union[LabelOpts, dict, None] = None,</span><br><span class="line"></span><br><span class="line"> # 该数据项区域的样式,起点和终点项的 itemStyle 会合并到一起。参考 `series_options.ItemStyleOpts`</span><br><span class="line"> itemstyle_opts: Union[ItemStyleOpts, dict, None] = None,</span><br><span class="line">)</span><br></pre></td></tr></table></figure></li><li><p><code>MarkAreaOpts:标记区域配置项</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">class MarkAreaOpts(</span><br><span class="line"> # 图形是否不响应和触发鼠标事件,默认为 False,即响应和触发鼠标事件。</span><br><span class="line"> is_silent: bool = False,</span><br><span class="line"></span><br><span class="line"> # 标签配置项,参考 `series_options.LabelOpts`</span><br><span class="line"> label_opts: LabelOpts = LabelOpts(),</span><br><span class="line"></span><br><span class="line"> # 标记区域数据,参考 `series_options.MarkAreaItem`</span><br><span class="line"> data: Sequence[Union[MarkAreaItem, Sequence, dict]] = None,</span><br><span class="line"></span><br><span class="line"> # 该数据项区域的样式。参考 `series_options.ItemStyleOpts`</span><br><span class="line"> itemstyle_opts: ItemStyleOpts = None,</span><br><span class="line">)</span><br></pre></td></tr></table></figure><hr></li><li><p><code>EffectOpts:涟漪特效配置项</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">class EffectOpts(</span><br><span class="line"> # 是否显示特效。</span><br><span class="line"> is_show: bool = True,</span><br><span class="line"></span><br><span class="line"> # 波纹的绘制方式,可选 'stroke' 和 'fill',Scatter 类型有效。</span><br><span class="line"> brush_type: str = "stroke",</span><br><span class="line"></span><br><span class="line"> # 动画中波纹的最大缩放比例,Scatter 类型有效。</span><br><span class="line"> scale: Numeric = 2.5,</span><br><span class="line"></span><br><span class="line"> # 动画的周期,秒数,Scatter 类型有效。</span><br><span class="line"> period: Numeric = 4,</span><br><span class="line"></span><br><span class="line"> # 特效标记的颜色</span><br><span class="line"> color: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 特效图形的标记。</span><br><span class="line"> # ECharts 提供的标记类型包括 'circle', 'rect', 'roundRect', 'triangle', </span><br><span class="line"> # 'diamond', 'pin', 'arrow', 'none'</span><br><span class="line"> # 可以通过 'image://url' 设置为图片,其中 URL 为图片的链接,或者 dataURI。</span><br><span class="line"> symbol: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 特效标记的大小,可以设置成诸如 10 这样单一的数字,也可以用数组分开表示高和宽,</span><br><span class="line"> # 例如 [20, 10] 表示标记宽为 20,高为 10。</span><br><span class="line"> symbol_size: Optional[Numeric] = None,</span><br><span class="line"></span><br><span class="line"> # 特效尾迹的长度。取从 0 到 1 的值,数值越大尾迹越长。Geo 图设置 Lines 类型时有效。</span><br><span class="line"> trail_length: Optional[Numeric] = None,</span><br><span class="line">)</span><br></pre></td></tr></table></figure></li><li><p><code>AreaStyleOpts:区域填充样式配置项</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line">class AreaStyleOpts(</span><br><span class="line"> # 图形透明度。支持从 0 到 1 的数字,为 0 时不绘制该图形。</span><br><span class="line"> opacity: Optional[Numeric] = 0,</span><br><span class="line"> # 填充的颜色。</span><br><span class="line"> # 颜色可以使用 RGB 表示,比如 'rgb(128, 128, 128)',如果想要加上 alpha 通道表示不透明度,</span><br><span class="line"> # 可以使用 RGBA,比如 'rgba(128, 128, 128, 0.5)',也可以使用十六进制格式,比如 '#ccc'。</span><br><span class="line"> # 除了纯色之外颜色也支持渐变色和纹理填充</span><br><span class="line"> # </span><br><span class="line"> # 线性渐变,前四个参数分别是 x0, y0, x2, y2, 范围从 0 - 1,相当于在图形包围盒中的百分比,</span><br><span class="line"> # 如果 globalCoord 为 `true`,则该四个值是绝对的像素位置</span><br><span class="line"> # color: {</span><br><span class="line"> # type: 'linear',</span><br><span class="line"> # x: 0,</span><br><span class="line"> # y: 0,</span><br><span class="line"> # x2: 0,</span><br><span class="line"> # y2: 1,</span><br><span class="line"> # colorStops: [{</span><br><span class="line"> # offset: 0, color: 'red' // 0% 处的颜色</span><br><span class="line"> # }, {</span><br><span class="line"> # offset: 1, color: 'blue' // 100% 处的颜色</span><br><span class="line"> # }],</span><br><span class="line"> # global: false // 缺省为 false</span><br><span class="line"> # }</span><br><span class="line"> # </span><br><span class="line"> # 径向渐变,前三个参数分别是圆心 x, y 和半径,取值同线性渐变</span><br><span class="line"> # color: {</span><br><span class="line"> # type: 'radial',</span><br><span class="line"> # x: 0.5,</span><br><span class="line"> # y: 0.5,</span><br><span class="line"> # r: 0.5,</span><br><span class="line"> # colorStops: [{</span><br><span class="line"> # offset: 0, color: 'red' // 0% 处的颜色</span><br><span class="line"> # }, {</span><br><span class="line"> # offset: 1, color: 'blue' // 100% 处的颜色</span><br><span class="line"> # }],</span><br><span class="line"> # global: false // 缺省为 false</span><br><span class="line"> # }</span><br><span class="line"> # </span><br><span class="line"> # 纹理填充</span><br><span class="line"> # color: {</span><br><span class="line"> # image: imageDom, // 支持为 HTMLImageElement, HTMLCanvasElement,不支持路径字符串</span><br><span class="line"> # repeat: 'repeat' // 是否平铺, 可以是 'repeat-x', 'repeat-y', 'no-repeat'</span><br><span class="line"> # }</span><br><span class="line"> color: Optional[str] = None</span><br><span class="line">)</span><br></pre></td></tr></table></figure></li><li><p><code>SplitAreaOpts:分隔区域配置项</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">class SplitAreaOpts(</span><br><span class="line"> # 是否显示分隔区域。</span><br><span class="line"> is_show=True, </span><br><span class="line"> # 分隔区域的样式配置项,参考 `series_options.AreaStyleOpts`</span><br><span class="line"> areastyle_opts: AreaStyleOpts = AreaStyleOpts()</span><br><span class="line">)</span><br></pre></td></tr></table></figure></li><li><p><code>MinorTickOpts:次级刻度配置项</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">class MinorTickOpts(</span><br><span class="line"> # 是否显示次刻度线。</span><br><span class="line"> is_show: bool = False,</span><br><span class="line"></span><br><span class="line"> # 次刻度线分割数,默认会分割成 5 段</span><br><span class="line"> split_number: Numeric = 5,</span><br><span class="line"></span><br><span class="line"> # 次刻度线的长度。</span><br><span class="line"> length: Numeric = 3,</span><br><span class="line"></span><br><span class="line"> # 次刻度线的样式</span><br><span class="line"> linestyle_opts: Union[LineStyleOpts, dict, None] = None,</span><br><span class="line">)</span><br></pre></td></tr></table></figure></li><li><p><code>MinorSplitLineOpts:次级分割线配置项</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">class MinorSplitLineOpts(</span><br><span class="line"> # 是否显示次分隔线。默认不显示。</span><br><span class="line"> is_show: bool = False,</span><br><span class="line"></span><br><span class="line"> # 次分隔线线宽。</span><br><span class="line"> width: Numeric = 1,</span><br><span class="line"></span><br><span class="line"> # 次分隔线线的类型。可选:'solid','dashed','dotted'</span><br><span class="line"> type_: str = "solid",</span><br><span class="line"></span><br><span class="line"> # 图形透明度。支持从 0 到 1 的数字,为 0 时不绘制该图形。</span><br><span class="line"> opacity: Union[Numeric, None] = None,</span><br><span class="line"></span><br><span class="line"> # 线的样式</span><br><span class="line"> linestyle_opts: Union[LineStyleOpts, dict, None] = None,</span><br><span class="line">)</span><br></pre></td></tr></table></figure></li></ol>]]></content>
<tags>
<tag> Pyecharts </tag>
</tags>
</entry>
<entry>
<title>pyecharts_Bar</title>
<link href="/2020/07/30/pyecharts-bar/"/>
<url>/2020/07/30/pyecharts-bar/</url>
<content type="html"><![CDATA[<h1 id="pyecharts-Bar"><a href="#pyecharts-Bar" class="headerlink" title="pyecharts_Bar"></a>pyecharts_Bar</h1><p><a href="http://pyecharts.org/#/zh-cn/" target="_blank" rel="noopener">全部来自于官方文档,主要用于方便自己查找</a></p><h2 id="Bar-柱状图-条形图"><a href="#Bar-柱状图-条形图" class="headerlink" title="Bar: 柱状图/条形图"></a><code>Bar: 柱状图/条形图</code></h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">Bar(init_opts=opts.InitOpts())</span><br><span class="line">.add_xaxis(x_data)</span><br><span class="line">.add_yaxis(y_data)</span><br><span class="line"> # 系列配置项</span><br><span class="line">.set_series_opts()</span><br><span class="line"> # 全局配置项</span><br><span class="line">.set_global_opts(</span><br><span class="line"> title_opts=opts.TitleOpts(title="标题"),</span><br><span class="line"> # 工具箱选择项</span><br><span class="line"> toolbox_opts=opts.ToolboxOpts(),</span><br><span class="line"> # 图例配置项</span><br><span class="line"> legend_opts=opts.legendOpts(is_show=False),</span><br><span class="line"> # 坐标轴配置项</span><br><span class="line"> yaxis_opts=opts.AxisOpts(type_="value"),</span><br><span class="line"> # 原生图形元素组件</span><br><span class="line"> opts.GraphicGroup(graphic_item=opts.GraphicItem()) </span><br><span class="line"> ) </span><br><span class="line">.render()</span><br></pre></td></tr></table></figure><ul><li><p>.add_yaxis()</p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br></pre></td><td class="code"><pre><span class="line">def add_yaxis(</span><br><span class="line"> # 系列名称,用于 tooltip 的显示,legend 的图例筛选。</span><br><span class="line"> series_name: str,</span><br><span class="line"></span><br><span class="line"> # 系列数据</span><br><span class="line"> y_axis: Sequence[Numeric, opts.BarItem, dict],</span><br><span class="line"></span><br><span class="line"> # 是否选中图例</span><br><span class="line"> is_selected: bool = True,</span><br><span class="line"></span><br><span class="line"> # 使用的 x 轴的 index,在单个图表实例中存在多个 x 轴的时候有用。</span><br><span class="line"> xaxis_index: Optional[Numeric] = None,</span><br><span class="line"></span><br><span class="line"> # 使用的 y 轴的 index,在单个图表实例中存在多个 y 轴的时候有用。</span><br><span class="line"> yaxis_index: Optional[Numeric] = None,</span><br><span class="line"></span><br><span class="line"> # 是否启用图例 hover 时的联动高亮</span><br><span class="line"> is_legend_hover_link: bool = True,</span><br><span class="line"></span><br><span class="line"> # 系列 label 颜色</span><br><span class="line"> color: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 是否显示柱条的背景色。通过 backgroundStyle 配置背景样式。</span><br><span class="line"> is_show_background: bool = False,</span><br><span class="line"></span><br><span class="line"> # 每一个柱条的背景样式。需要将 showBackground 设置为 true 时才有效。</span><br><span class="line"> background_style: types.Union[types.BarBackground, dict, None] = None,</span><br><span class="line"></span><br><span class="line"> # 数据堆叠,同个类目轴上系列配置相同的 stack 值可以堆叠放置。</span><br><span class="line"> stack: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 柱条的宽度,不设时自适应。</span><br><span class="line"> # 可以是绝对值例如 40 或者百分数例如 '60%'。百分数基于自动计算出的每一类目的宽度。</span><br><span class="line"> # 在同一坐标系上,此属性会被多个 'bar' 系列共享。此属性应设置于此坐标系中最后一个 'bar' 系列上才会生效,并且是对此坐标系中所有 'bar' 系列生效。</span><br><span class="line"> bar_width: types.Union[types.Numeric, str] = None,</span><br><span class="line"></span><br><span class="line"> # 柱条的最大宽度。比 barWidth 优先级高。</span><br><span class="line"> bar_max_width: types.Union[types.Numeric, str] = None,</span><br><span class="line"></span><br><span class="line"> # 柱条的最小宽度。在直角坐标系中,默认值是 1。否则默认值是 null。比 barWidth 优先级高。</span><br><span class="line"> bar_min_width: types.Union[types.Numeric, str] = None,</span><br><span class="line"></span><br><span class="line"> # 柱条最小高度,可用于防止某数据项的值过小而影响交互。</span><br><span class="line"> bar_min_height: types.Numeric = 0,</span><br><span class="line"></span><br><span class="line"> # 同一系列的柱间距离,默认为类目间距的 20%,可设固定值</span><br><span class="line"> category_gap: Union[Numeric, str] = "20%",</span><br><span class="line"></span><br><span class="line"> # 不同系列的柱间距离,为百分比(如 '30%',表示柱子宽度的 30%)。</span><br><span class="line"> # 如果想要两个系列的柱子重叠,可以设置 gap 为 '-100%'。这在用柱子做背景的时候有用。</span><br><span class="line"> gap: Optional[str] = "30%",</span><br><span class="line"></span><br><span class="line"> # 是否开启大数据量优化,在数据图形特别多而出现卡顿时候可以开启。</span><br><span class="line"> # 开启后配合 largeThreshold 在数据量大于指定阈值的时候对绘制进行优化。</span><br><span class="line"> # 缺点:优化后不能自定义设置单个数据项的样式。</span><br><span class="line"> is_large: bool = False,</span><br><span class="line"></span><br><span class="line"> # 开启绘制优化的阈值。</span><br><span class="line"> large_threshold: types.Numeric = 400,</span><br><span class="line"></span><br><span class="line"> # 使用 dimensions 定义 series.data 或者 dataset.source 的每个维度的信息。</span><br><span class="line"> # 注意:如果使用了 dataset,那么可以在 dataset.source 的第一行/列中给出 dimension 名称。</span><br><span class="line"> # 于是就不用在这里指定 dimension。</span><br><span class="line"> # 但是,如果在这里指定了 dimensions,那么 ECharts 不再会自动从 dataset.source 的第一行/列中获取维度信息。</span><br><span class="line"> dimensions: types.Union[types.Sequence, None] = None,</span><br><span class="line"></span><br><span class="line"> # 当使用 dataset 时,seriesLayoutBy 指定了 dataset 中用行还是列对应到系列上,也就是说,系列“排布”到 dataset 的行还是列上。可取值:</span><br><span class="line"> # 'column':默认,dataset 的列对应于系列,从而 dataset 中每一列是一个维度(dimension)。</span><br><span class="line"> # 'row':dataset 的行对应于系列,从而 dataset 中每一行是一个维度(dimension)。</span><br><span class="line"> series_layout_by: str = "column",</span><br><span class="line"></span><br><span class="line"> # 如果 series.data 没有指定,并且 dataset 存在,那么就会使用 dataset。</span><br><span class="line"> # datasetIndex 指定本系列使用那个 dataset。</span><br><span class="line"> dataset_index: types.Numeric = 0,</span><br><span class="line"></span><br><span class="line"> # 是否裁剪超出坐标系部分的图形。柱状图:裁掉所有超出坐标系的部分,但是依然保留柱子的宽度</span><br><span class="line"> is_clip: bool = True,</span><br><span class="line"></span><br><span class="line"> # 柱状图所有图形的 zlevel 值。</span><br><span class="line"> z_level: types.Numeric = 0,</span><br><span class="line"></span><br><span class="line"> # 柱状图组件的所有图形的z值。控制图形的前后顺序。</span><br><span class="line"> # z值小的图形会被z值大的图形覆盖。</span><br><span class="line"> # z相比zlevel优先级更低,而且不会创建新的 Canvas。</span><br><span class="line"> z: types.Numeric = 2,</span><br><span class="line"></span><br><span class="line"> # 标签配置项,参考 `series_options.LabelOpts`</span><br><span class="line"> label_opts: Union[opts.LabelOpts, dict] = opts.LabelOpts(),</span><br><span class="line"></span><br><span class="line"> # 标记点配置项,参考 `series_options.MarkPointOpts`</span><br><span class="line"> markpoint_opts: Union[opts.MarkPointOpts, dict, None] = None,</span><br><span class="line"></span><br><span class="line"> # 标记线配置项,参考 `series_options.MarkLineOpts`</span><br><span class="line"> markline_opts: Union[opts.MarkLineOpts, dict, None] = None,</span><br><span class="line"></span><br><span class="line"> # 提示框组件配置项,参考 `series_options.TooltipOpts`</span><br><span class="line"> tooltip_opts: Union[opts.TooltipOpts, dict, None] = None,</span><br><span class="line"></span><br><span class="line"> # 图元样式配置项,参考 `series_options.ItemStyleOpts`</span><br><span class="line"> itemstyle_opts: Union[opts.ItemStyleOpts, dict, None] = None,</span><br><span class="line"></span><br><span class="line"> # 可以定义 data 的哪个维度被编码成什么。</span><br><span class="line"> encode: types.Union[types.JSFunc, dict, None] = None,</span><br><span class="line">)</span><br></pre></td></tr></table></figure></li></ul><ol><li><code>BarItem:柱状图数据项</code> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">class BarItem(</span><br><span class="line"> # 数据项名称。</span><br><span class="line"> name: Optional[str] = None,</span><br><span class="line"> </span><br><span class="line"> # 单个数据项的数值。</span><br><span class="line"> value: Optional[Numeric] = None,</span><br><span class="line"> </span><br><span class="line"> # 单个柱条文本的样式设置,参考 `series_options.LabelOpts`。</span><br><span class="line"> label_opts: Union[LabelOpts, dict, None] = None,</span><br><span class="line"> </span><br><span class="line"> # 图元样式配置项,参考 `series_options.ItemStyleOpts`</span><br><span class="line"> itemstyle_opts: Union[ItemStyleOpts, dict, None] = None,</span><br><span class="line"> </span><br><span class="line"> # 提示框组件配置项,参考 `series_options.TooltipOpts`</span><br><span class="line"> tooltip_opts: Union[TooltipOpts, dict, None] = None,</span><br><span class="line"> )</span><br></pre></td></tr></table></figure></li><li><code>BarBackgroundStyleOpts:柱状图背景样式配置</code> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line">class BarBackgroundStyleOpts(</span><br><span class="line"> # 柱条的颜色。</span><br><span class="line"> color: str = "rgba(180, 180, 180, 0.2)",</span><br><span class="line"></span><br><span class="line"> # 柱条的描边颜色。</span><br><span class="line"> border_color: str = "#000",</span><br><span class="line"></span><br><span class="line"> # 柱条的描边宽度,默认不描边。</span><br><span class="line"> border_width: Numeric = 0,</span><br><span class="line"></span><br><span class="line"> # 柱条的描边类型,默认为实线,支持 'dashed', 'dotted'。</span><br><span class="line"> border_type: str = "solid",</span><br><span class="line"></span><br><span class="line"> # 圆角半径,单位px,支持传入数组分别指定 4 个圆角半径。 如:</span><br><span class="line"> # barBorderRadius: 5, // 统一设置四个角的圆角大小</span><br><span class="line"> # barBorderRadius: [5, 5, 0, 0] //(顺时针左上,右上,右下,左下)</span><br><span class="line"> bar_border_radius: Union[Numeric, Sequence] = 0,</span><br><span class="line"></span><br><span class="line"> # 图形阴影的模糊大小。</span><br><span class="line"> # 该属性配合 shadowColor,shadowOffsetX, shadowOffsetY 一起设置图形的阴影效果。</span><br><span class="line"> shadow_blur: Optional[Numeric] = None,</span><br><span class="line"></span><br><span class="line"> # 阴影颜色。支持的格式同color。</span><br><span class="line"> shadow_color: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 阴影水平方向上的偏移距离。</span><br><span class="line"> shadow_offset_x: Numeric = 0,</span><br><span class="line"></span><br><span class="line"> # 阴影垂直方向上的偏移距离。</span><br><span class="line"> shadow_offset_y: Numeric = 0,</span><br><span class="line"></span><br><span class="line"> # 图形透明度。支持从 0 到 1 的数字,为 0 时不绘制该图形。</span><br><span class="line"> opacity: Optional[Numeric] = None,</span><br><span class="line">)</span><br></pre></td></tr></table></figure></li></ol>]]></content>
<tags>
<tag> Pyecharts </tag>
</tags>
</entry>
<entry>
<title>pyecharts_global_options</title>
<link href="/2020/07/30/pyecharts-global-options/"/>
<url>/2020/07/30/pyecharts-global-options/</url>
<content type="html"><![CDATA[<h2 id="pyecharts-配置项"><a href="#pyecharts-配置项" class="headerlink" title="pyecharts 配置项"></a>pyecharts 配置项</h2><p><a href="http://pyecharts.org/#/zh-cn/" target="_blank" rel="noopener">全部来自于官方文档,主要用于方便自己查找</a><br><strong>全局配置项 (按常用顺序排序)</strong></p><ol><li><p><code>InitOpts: 初始化配置项 (class pyecharts.options.InitOpts)</code></p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">class InitOpts(</span><br><span class="line"> # 图表画布宽度,css 长度单位。</span><br><span class="line"> width: str = "900px",</span><br><span class="line"></span><br><span class="line"> # 图表画布高度,css 长度单位。</span><br><span class="line"> height: str = "500px",</span><br><span class="line"></span><br><span class="line"> # 图表 ID,图表唯一标识,用于在多图表时区分。</span><br><span class="line"> chart_id: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 渲染风格,可选 "canvas", "svg"</span><br><span class="line"> # # 参考 `全局变量` 章节</span><br><span class="line"> renderer: str = RenderType.CANVAS,</span><br><span class="line"></span><br><span class="line"> # 网页标题</span><br><span class="line"> page_title: str = "Awesome-pyecharts",</span><br><span class="line"></span><br><span class="line"> # 图表主题</span><br><span class="line"> theme: str = "white",</span><br><span class="line"></span><br><span class="line"> # 图表背景颜色</span><br><span class="line"> bg_color: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 远程 js host,如不设置默认为 https://assets.pyecharts.org/assets/"</span><br><span class="line"> # 参考 `全局变量` 章节</span><br><span class="line"> js_host: str = "",</span><br><span class="line"></span><br><span class="line"> # 画图动画初始化配置,参考 `global_options.AnimationOpts`</span><br><span class="line"> animation_opts: Union[AnimationOpts, dict] = AnimationOpts(),</span><br><span class="line">)</span><br></pre></td></tr></table></figure></li><li><p><code>TitleOpts:标题配置项 (class pyecharts.options.TitleOpts)</code></p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br></pre></td><td class="code"><pre><span class="line">class TitleOpts(</span><br><span class="line"> # 主标题文本,支持使用 \n 换行。</span><br><span class="line"> title: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 主标题跳转 URL 链接</span><br><span class="line"> title_link: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 主标题跳转链接方式,默认值是: blank</span><br><span class="line"> # 可选参数: 'self', 'blank': 'self' 当前窗口打开; 'blank' 新窗口打开</span><br><span class="line"> title_target: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 副标题文本,支持使用 \n 换行。</span><br><span class="line"> subtitle: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 副标题跳转 URL 链接</span><br><span class="line"> subtitle_link: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 副标题跳转链接方式, 默认值是: blank</span><br><span class="line"> # 可选参数: 'self', 'blank': 'self' 当前窗口打开; 'blank' 新窗口打开</span><br><span class="line"> subtitle_target: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # title 组件离容器左侧的距离。left 的值可以是像 20 这样的具体像素值,可以是像 '20%' 这样相对于容器高宽的百分比,</span><br><span class="line"> # 也可以是 'left', 'center', 'right'。</span><br><span class="line"> # 如果 left 的值为'left', 'center', 'right',组件会根据相应的位置自动对齐。</span><br><span class="line"> pos_left: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # title 组件离容器右侧的距离。</span><br><span class="line"> # right 的值可以是像 20 这样的具体像素值,可以是像 '20%' 这样相对于容器高宽的百分比。</span><br><span class="line"> pos_right: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # title 组件离容器上侧的距离。</span><br><span class="line"> # top 的值可以是像 20 这样的具体像素值,可以是像 '20%' 这样相对于容器高宽的百分比,</span><br><span class="line"> # 也可以是 'top', 'middle', 'bottom'。</span><br><span class="line"> # 如果 top 的值为'top', 'middle', 'bottom',组件会根据相应的位置自动对齐。</span><br><span class="line"> pos_top: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # title 组件离容器下侧的距离。</span><br><span class="line"> # bottom 的值可以是像 20 这样的具体像素值,可以是像 '20%' 这样相对于容器高宽的百分比。</span><br><span class="line"> pos_bottom: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 标题内边距,单位px,默认各方向内边距为5,接受数组分别设定上右下左边距。</span><br><span class="line"> # // 设置内边距为 5</span><br><span class="line"> # padding: 5</span><br><span class="line"> # // 设置上下的内边距为 5,左右的内边距为 10</span><br><span class="line"> # padding: [5, 10]</span><br><span class="line"> # // 分别设置四个方向的内边距</span><br><span class="line"> # padding: [</span><br><span class="line"> # 5, // 上</span><br><span class="line"> # 10, // 右</span><br><span class="line"> # 5, // 下</span><br><span class="line"> # 10, // 左</span><br><span class="line"> # ]</span><br><span class="line"> padding: Union[Sequence, Numeric] = 5,</span><br><span class="line"></span><br><span class="line"> # 主副标题之间的间距。</span><br><span class="line"> item_gap: Numeric = 10,</span><br><span class="line"></span><br><span class="line"> # 主标题字体样式配置项,参考 `series_options.TextStyleOpts`</span><br><span class="line"> title_textstyle_opts: Union[TextStyleOpts, dict, None] = None,</span><br><span class="line"></span><br><span class="line"> # 副标题字体样式配置项,参考 `series_options.TextStyleOpts`</span><br><span class="line"> subtitle_textstyle_opts: Union[TextStyleOpts, dict, None] = None,</span><br><span class="line">)</span><br></pre></td></tr></table></figure></li><li><p><code>TooltipOpts:提示框配置项 (class pyecharts.options.TooltipOpts)</code></p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br></pre></td><td class="code"><pre><span class="line">class TooltipOpts(</span><br><span class="line"> # 是否显示提示框组件,包括提示框浮层和 axisPointer。</span><br><span class="line"> is_show: bool = True,</span><br><span class="line"></span><br><span class="line"> # 触发类型。可选:</span><br><span class="line"> # 'item': 数据项图形触发,主要在散点图,饼图等无类目轴的图表中使用。</span><br><span class="line"> # 'axis': 坐标轴触发,主要在柱状图,折线图等会使用类目轴的图表中使用。</span><br><span class="line"> # 'none': 什么都不触发</span><br><span class="line"> trigger: str = "item",</span><br><span class="line"></span><br><span class="line"> # 提示框触发的条件,可选:</span><br><span class="line"> # 'mousemove': 鼠标移动时触发。</span><br><span class="line"> # 'click': 鼠标点击时触发。</span><br><span class="line"> # 'mousemove|click': 同时鼠标移动和点击时触发。</span><br><span class="line"> # 'none': 不在 'mousemove' 或 'click' 时触发,</span><br><span class="line"> trigger_on: str = "mousemove|click",</span><br><span class="line"></span><br><span class="line"> # 指示器类型。可选</span><br><span class="line"> # 'line':直线指示器; 'shadow':阴影指示器; 'none':无指示器</span><br><span class="line"> # 'cross':十字准星指示器。其实是种简写,表示启用两个正交的轴的 axisPointer。</span><br><span class="line"> axis_pointer_type: str = "line",</span><br><span class="line"></span><br><span class="line"> # 是否显示提示框浮层,默认显示。</span><br><span class="line"> # 只需 tooltip 触发事件或显示 axisPointer 而不需要显示内容时可配置该项为 false。</span><br><span class="line"> is_show_content: bool = True,</span><br><span class="line"></span><br><span class="line"> # 是否永远显示提示框内容,</span><br><span class="line"> # 默认情况下在移出可触发提示框区域后一定时间后隐藏,设置为 true 可以保证一直显示提示框内容。</span><br><span class="line"> is_always_show_content: bool = False,</span><br><span class="line"></span><br><span class="line"> # 浮层显示的延迟,单位为 ms,默认没有延迟,也不建议设置。</span><br><span class="line"> show_delay: Numeric = 0,</span><br><span class="line"></span><br><span class="line"> # 浮层隐藏的延迟,单位为 ms,在 alwaysShowContent 为 true 的时候无效。</span><br><span class="line"> hide_delay: Numeric = 100,</span><br><span class="line"></span><br><span class="line"> # 提示框浮层的位置,默认不设置时位置会跟随鼠标的位置。</span><br><span class="line"> # 1、通过数组配置:</span><br><span class="line"> # 绝对位置,相对于容器左侧 10px, 上侧 10 px ===> position: [10, 10]</span><br><span class="line"> # 相对位置,放置在容器正中间 ===> position: ['50%', '50%']</span><br><span class="line"> # 2、通过回调函数配置</span><br><span class="line"> # 3、固定参数配置:'inside','top','left','right','bottom'</span><br><span class="line"> position: Union[str, Sequence, JSFunc] = None,</span><br><span class="line"></span><br><span class="line"> # 标签内容格式器,支持字符串模板和回调函数两种形式,字符串模板与回调函数返回的字符串均支持用 \n 换行。</span><br><span class="line"> # 字符串模板 模板变量有:</span><br><span class="line"> # {a}:系列名。</span><br><span class="line"> # {b}:数据名。</span><br><span class="line"> # {c}:数据值。</span><br><span class="line"> # {@xxx}:数据中名为 'xxx' 的维度的值,如 {@product} 表示名为 'product'` 的维度的值。</span><br><span class="line"> # {@[n]}:数据中维度 n 的值,如{@[3]}` 表示维度 3 的值,从 0 开始计数。</span><br><span class="line"> # 示例:formatter: '{b}: {@score}'</span><br><span class="line"> # </span><br><span class="line"> # 回调函数,回调函数格式:</span><br><span class="line"> # (params: Object|Array) => string</span><br><span class="line"> # 参数 params 是 formatter 需要的单个数据集。格式如下:</span><br><span class="line"> # {</span><br><span class="line"> # componentType: 'series',</span><br><span class="line"> # // 系列类型</span><br><span class="line"> # seriesType: string,</span><br><span class="line"> # // 系列在传入的 option.series 中的 index</span><br><span class="line"> # seriesIndex: number,</span><br><span class="line"> # // 系列名称</span><br><span class="line"> # seriesName: string,</span><br><span class="line"> # // 数据名,类目名</span><br><span class="line"> # name: string,</span><br><span class="line"> # // 数据在传入的 data 数组中的 index</span><br><span class="line"> # dataIndex: number,</span><br><span class="line"> # // 传入的原始数据项</span><br><span class="line"> # data: Object,</span><br><span class="line"> # // 传入的数据值</span><br><span class="line"> # value: number|Array,</span><br><span class="line"> # // 数据图形的颜色</span><br><span class="line"> # color: string,</span><br><span class="line"> # }</span><br><span class="line"> formatter: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 提示框浮层的背景颜色。</span><br><span class="line"> background_color: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 提示框浮层的边框颜色。</span><br><span class="line"> border_color: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 提示框浮层的边框宽。</span><br><span class="line"> border_width: Numeric = 0,</span><br><span class="line"></span><br><span class="line"> # 文字样式配置项,参考 `series_options.TextStyleOpts`</span><br><span class="line"> textstyle_opts: TextStyleOpts = TextStyleOpts(font_size=14),</span><br><span class="line">)</span><br><span class="line">```</span><br></pre></td></tr></table></figure></li></ol><ol start="4"><li><p><code>AnimationOpts:Echarts 画图动画配置项 (class pyecharts.options.Animation)</code></p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line">class AnimationOpts(</span><br><span class="line"> # 是否开启动画,默认为 True 开启。</span><br><span class="line"> animation: bool = True,</span><br><span class="line"></span><br><span class="line"> # 是否开启动画的阈值,当单个系列显示的图形数量大于这个阈值时会关闭动画。默认 2000。</span><br><span class="line"> animation_threshold: Numeric = 2000,</span><br><span class="line"></span><br><span class="line"> # 初始动画的时长,默认值为 1000。</span><br><span class="line"> # 支持回调函数,可以通过每个数据返回不同的 delay 时间实现更戏剧的初始动画效果:</span><br><span class="line"> animation_duration: Union[Numeric, JSFunc] = 1000,</span><br><span class="line"></span><br><span class="line"> # 初始动画的缓动效果。</span><br><span class="line"> # 不同的缓动效果可以参考,缓动示例 (https://www.echartsjs.com/gallery/editor.html?c=line-easing)。</span><br><span class="line"> animation_easing: Union[str] = "cubicOut",</span><br><span class="line"></span><br><span class="line"> # 初始动画的延迟,默认值为 0。</span><br><span class="line"> # 支持回调函数,可以通过每个数据返回不同的 delay 时间实现更戏剧的初始动画效果。</span><br><span class="line"> animation_delay: Union[Numeric, JSFunc] = 0,</span><br><span class="line"></span><br><span class="line"> # 数据更新动画的时长,默认值为 300。</span><br><span class="line"> # 支持回调函数,可以通过每个数据返回不同的 delay 时间实现更戏剧的更新动画效果:</span><br><span class="line"> animation_duration_update: Union[Numeric, JSFunc] = 300,</span><br><span class="line"></span><br><span class="line"> # 数据更新动画的缓动效果。</span><br><span class="line"> # 不同的缓动效果可以参考,缓动示例 (https://www.echartsjs.com/gallery/editor.html?c=line-easing)。</span><br><span class="line"> animation_easing_update: Union[Numeric] = "cubicOut",</span><br><span class="line"></span><br><span class="line"> # 数据更新动画的延迟,默认值为 0。</span><br><span class="line"> # 支持回调函数,可以通过每个数据返回不同的 delay 时间实现更戏剧的更新动画效果。</span><br><span class="line"> animation_delay_update: Union[Numeric, JSFunc] = 0,</span><br><span class="line">)</span><br></pre></td></tr></table></figure></li><li><p><code>LegendOpts:图例配置项</code></p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br></pre></td><td class="code"><pre><span class="line">class LegendOpts(</span><br><span class="line"> # 图例的类型。可选值:</span><br><span class="line"> # 'plain':普通图例。缺省就是普通图例。</span><br><span class="line"> # 'scroll':可滚动翻页的图例。当图例数量较多时可以使用。</span><br><span class="line"> type_: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 图例选择的模式,控制是否可以通过点击图例改变系列的显示状态。默认开启图例选择,可以设成 false 关闭</span><br><span class="line"> # 除此之外也可以设成 'single' 或者 'multiple' 使用单选或者多选模式。</span><br><span class="line"> selected_mode: Union[str, bool, None] = None,</span><br><span class="line"></span><br><span class="line"> # 是否显示图例组件</span><br><span class="line"> is_show: bool = True,</span><br><span class="line"></span><br><span class="line"> # 图例组件离容器左侧的距离。</span><br><span class="line"> # left 的值可以是像 20 这样的具体像素值,可以是像 '20%' 这样相对于容器高宽的百分比,</span><br><span class="line"> # 也可以是 'left', 'center', 'right'。</span><br><span class="line"> # 如果 left 的值为'left', 'center', 'right',组件会根据相应的位置自动对齐。</span><br><span class="line"> pos_left: Union[str, Numeric, None] = None,</span><br><span class="line"></span><br><span class="line"> # 图例组件离容器右侧的距离。</span><br><span class="line"> # right 的值可以是像 20 这样的具体像素值,可以是像 '20%' 这样相对于容器高宽的百分比。</span><br><span class="line"> pos_right: Union[str, Numeric, None] = None,</span><br><span class="line"></span><br><span class="line"> # 图例组件离容器上侧的距离。</span><br><span class="line"> # top 的值可以是像 20 这样的具体像素值,可以是像 '20%' 这样相对于容器高宽的百分比,</span><br><span class="line"> # 也可以是 'top', 'middle', 'bottom'。</span><br><span class="line"> # 如果 top 的值为'top', 'middle', 'bottom',组件会根据相应的位置自动对齐。</span><br><span class="line"> pos_top: Union[str, Numeric, None] = None,</span><br><span class="line"></span><br><span class="line"> # 图例组件离容器下侧的距离。</span><br><span class="line"> # bottom 的值可以是像 20 这样的具体像素值,可以是像 '20%' 这样相对于容器高宽的百分比。</span><br><span class="line"> pos_bottom: Union[str, Numeric, None] = None,</span><br><span class="line"></span><br><span class="line"> # 图例列表的布局朝向。可选:'horizontal', 'vertical'</span><br><span class="line"> orient: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 图例标记和文本的对齐。默认自动(auto)</span><br><span class="line"> # 根据组件的位置和 orient 决定</span><br><span class="line"> # 当组件的 left 值为 'right' 以及纵向布局(orient 为 'vertical')的时候为右对齐,即为 'right'。</span><br><span class="line"> # 可选参数: `auto`, `left`, `right`</span><br><span class="line"> align: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 图例内边距,单位px,默认各方向内边距为5</span><br><span class="line"> padding: int = 5,</span><br><span class="line"></span><br><span class="line"> # 图例每项之间的间隔。横向布局时为水平间隔,纵向布局时为纵向间隔。</span><br><span class="line"> # 默认间隔为 10</span><br><span class="line"> item_gap: int = 10,</span><br><span class="line"></span><br><span class="line"> # 图例标记的图形宽度。默认宽度为 25</span><br><span class="line"> item_width: int = 25,</span><br><span class="line"></span><br><span class="line"> # 图例标记的图形高度。默认高度为 14</span><br><span class="line"> item_height: int = 14,</span><br><span class="line"></span><br><span class="line"> # 图例关闭时的颜色。默认是 #ccc</span><br><span class="line"> inactive_color: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 图例组件字体样式,参考 `series_options.TextStyleOpts`</span><br><span class="line"> textstyle_opts: Union[TextStyleOpts, dict, None] = None,</span><br><span class="line"></span><br><span class="line"> # 图例项的 icon。</span><br><span class="line"> # ECharts 提供的标记类型包括 'circle', 'rect', 'roundRect', 'triangle', 'diamond', 'pin', 'arrow', 'none'</span><br><span class="line"> # 可以通过 'image://url' 设置为图片,其中 URL 为图片的链接,或者 dataURI。</span><br><span class="line"> # 可以通过 'path://' 将图标设置为任意的矢量路径。</span><br><span class="line"> legend_icon: Optional[str] = None,</span><br><span class="line">)</span><br></pre></td></tr></table></figure></li><li><p><code>AxisLineOpts:坐标轴尺寸配置项</code></p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">class AxisLineOpts(</span><br><span class="line"> # 是否显示坐标轴轴线。</span><br><span class="line"> is_show: bool = True,</span><br><span class="line"></span><br><span class="line"> # X 轴或者 Y 轴的轴线是否在另一个轴的 0 刻度上,只有在另一个轴为数值轴且包含 0 刻度时有效。</span><br><span class="line"> is_on_zero: bool = True,</span><br><span class="line"></span><br><span class="line"> # 当有双轴时,可以用这个属性手动指定,在哪个轴的 0 刻度上。</span><br><span class="line"> on_zero_axis_index: int = 0,</span><br><span class="line"></span><br><span class="line"> # 轴线两边的箭头。可以是字符串,表示两端使用同样的箭头;或者长度为 2 的字符串数组,分别表示两端的箭头。</span><br><span class="line"> # 默认不显示箭头,即 'none'。</span><br><span class="line"> # 两端都显示箭头可以设置为 'arrow'。</span><br><span class="line"> # 只在末端显示箭头可以设置为 ['none', 'arrow']。</span><br><span class="line"> symbol: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 坐标轴线风格配置项,参考 `series_optionsLineStyleOpts`</span><br><span class="line"> linestyle_opts: Union[LineStyleOpts, dict, None] = None,</span><br><span class="line">)</span><br></pre></td></tr></table></figure></li><li><p><code>AxisPointerOpts:坐标轴指示器配置项</code></p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">class AxisPointerOpts(</span><br><span class="line"> # 默认显示坐标轴指示器</span><br><span class="line"> is_show: bool = True,</span><br><span class="line"></span><br><span class="line"> # 不同轴的 axisPointer 可以进行联动,在这里设置。联动表示轴能同步一起活动。</span><br><span class="line"> # 轴依据他们的 axisPointer 当前对应的值来联动。</span><br><span class="line"> # link 是一个数组,其中每一项表示一个 link group,一个 group 中的坐标轴互相联动。</span><br><span class="line"> # 具体使用方式可以参见:https://www.echartsjs.com/option.html#axisPointer.link</span><br><span class="line"> link: Sequence[dict] = None,</span><br><span class="line"></span><br><span class="line"> # 指示器类型。</span><br><span class="line"> # 可选参数如下,默认为 'line'</span><br><span class="line"> # 'line' 直线指示器</span><br><span class="line"> # 'shadow' 阴影指示器</span><br><span class="line"> # 'none' 无指示器</span><br><span class="line"> type_: str = "line",</span><br><span class="line"></span><br><span class="line"> # 坐标轴指示器的文本标签,坐标轴标签配置项,参考 `series_options.LabelOpts`</span><br><span class="line"> label: Union[LabelOpts, dict, None] = None,</span><br><span class="line"></span><br><span class="line"> # 坐标轴线风格配置项,参考 `series_optionsLineStyleOpts`</span><br><span class="line"> linestyle_opts: Union[LineStyleOpts, dict, None] = None,</span><br><span class="line">)</span><br></pre></td></tr></table></figure></li><li><p><code>AxisOpts:坐标轴配置项</code></p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br></pre></td><td class="code"><pre><span class="line">class AxisOpts(</span><br><span class="line"> # 坐标轴类型。可选:</span><br><span class="line"> # 'value': 数值轴,适用于连续数据。</span><br><span class="line"> # 'category': 类目轴,适用于离散的类目数据,为该类型时必须通过 data 设置类目数据。</span><br><span class="line"> # 'time': 时间轴,适用于连续的时序数据,与数值轴相比时间轴带有时间的格式化,在刻度计算上也有所不同,</span><br><span class="line"> # 例如会根据跨度的范围来决定使用月,星期,日还是小时范围的刻度。</span><br><span class="line"> # 'log' 对数轴。适用于对数数据。</span><br><span class="line"> type_: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 坐标轴名称。</span><br><span class="line"> name: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 是否显示 x 轴。</span><br><span class="line"> is_show: bool = True,</span><br><span class="line"></span><br><span class="line"> # 只在数值轴中(type: 'value')有效。</span><br><span class="line"> # 是否是脱离 0 值比例。设置成 true 后坐标刻度不会强制包含零刻度。在双数值轴的散点图中比较有用。</span><br><span class="line"> # 在设置 min 和 max 之后该配置项无效。</span><br><span class="line"> is_scale: bool = False,</span><br><span class="line"></span><br><span class="line"> # 是否反向坐标轴。</span><br><span class="line"> is_inverse: bool = False,</span><br><span class="line"></span><br><span class="line"> # 坐标轴名称显示位置。可选:</span><br><span class="line"> # 'start', 'middle' 或者 'center','end'</span><br><span class="line"> name_location: str = "end",</span><br><span class="line"></span><br><span class="line"> # 坐标轴名称与轴线之间的距离。</span><br><span class="line"> name_gap: Numeric = 15,</span><br><span class="line"></span><br><span class="line"> # 坐标轴名字旋转,角度值。</span><br><span class="line"> name_rotate: Optional[Numeric] = None,</span><br><span class="line"></span><br><span class="line"> # 强制设置坐标轴分割间隔。</span><br><span class="line"> # 因为 splitNumber 是预估的值,实际根据策略计算出来的刻度可能无法达到想要的效果,</span><br><span class="line"> # 这时候可以使用 interval 配合 min、max 强制设定刻度划分,一般不建议使用。</span><br><span class="line"> # 无法在类目轴中使用。在时间轴(type: 'time')中需要传时间戳,在对数轴(type: 'log')中需要传指数值。</span><br><span class="line"> interval: Optional[Numeric] = None,</span><br><span class="line"></span><br><span class="line"> # x 轴所在的 grid 的索引,默认位于第一个 grid。</span><br><span class="line"> grid_index: Numeric = 0,</span><br><span class="line"></span><br><span class="line"> # x 轴的位置。可选:</span><br><span class="line"> # 'top', 'bottom'</span><br><span class="line"> # 默认 grid 中的第一个 x 轴在 grid 的下方('bottom'),第二个 x 轴视第一个 x 轴的位置放在另一侧。</span><br><span class="line"> position: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # Y 轴相对于默认位置的偏移,在相同的 position 上有多个 Y 轴的时候有用。</span><br><span class="line"> offset: Numeric = 0,</span><br><span class="line"></span><br><span class="line"> # 坐标轴的分割段数,需要注意的是这个分割段数只是个预估值,最后实际显示的段数会在这个基础上根据分割后坐标轴刻度显示的易读程度作调整。 </span><br><span class="line"> # 默认值是 5</span><br><span class="line"> split_number: Numeric = 5,</span><br><span class="line"></span><br><span class="line"> # 坐标轴两边留白策略,类目轴和非类目轴的设置和表现不一样。</span><br><span class="line"> # 类目轴中 boundaryGap 可以配置为 true 和 false。默认为 true,这时候刻度只是作为分隔线,</span><br><span class="line"> # 标签和数据点都会在两个刻度之间的带(band)中间。</span><br><span class="line"> # 非类目轴,包括时间,数值,对数轴,boundaryGap是一个两个值的数组,分别表示数据最小值和最大值的延伸范围</span><br><span class="line"> # 可以直接设置数值或者相对的百分比,在设置 min 和 max 后无效。 示例:boundaryGap: ['20%', '20%']</span><br><span class="line"> boundary_gap: Union[str, bool, None] = None,</span><br><span class="line"></span><br><span class="line"> # 坐标轴刻度最小值。</span><br><span class="line"> # 可以设置成特殊值 'dataMin',此时取数据在该轴上的最小值作为最小刻度。</span><br><span class="line"> # 不设置时会自动计算最小值保证坐标轴刻度的均匀分布。</span><br><span class="line"> # 在类目轴中,也可以设置为类目的序数(如类目轴 data: ['类A', '类B', '类C'] 中,序数 2 表示 '类C'。</span><br><span class="line"> # 也可以设置为负数,如 -3)。</span><br><span class="line"> min_: Union[Numeric, str, None] = None,</span><br><span class="line"></span><br><span class="line"> # 坐标轴刻度最大值。</span><br><span class="line"> # 可以设置成特殊值 'dataMax',此时取数据在该轴上的最大值作为最大刻度。</span><br><span class="line"> # 不设置时会自动计算最大值保证坐标轴刻度的均匀分布。</span><br><span class="line"> # 在类目轴中,也可以设置为类目的序数(如类目轴 data: ['类A', '类B', '类C'] 中,序数 2 表示 '类C'。</span><br><span class="line"> # 也可以设置为负数,如 -3)。</span><br><span class="line"> max_: Union[Numeric, str, None] = None,</span><br><span class="line"></span><br><span class="line"> # 自动计算的坐标轴最小间隔大小。</span><br><span class="line"> # 例如可以设置成1保证坐标轴分割刻度显示成整数。</span><br><span class="line"> # 默认值是 0</span><br><span class="line"> min_interval: Numeric = 0,</span><br><span class="line"></span><br><span class="line"> # 自动计算的坐标轴最大间隔大小。</span><br><span class="line"> # 例如,在时间轴((type: 'time'))可以设置成 3600 * 24 * 1000 保证坐标轴分割刻度最大为一天。</span><br><span class="line"> max_interval: Optional[Numeric] = None,</span><br><span class="line"></span><br><span class="line"> # 坐标轴刻度线配置项,参考 `global_options.AxisLineOpts`</span><br><span class="line"> axisline_opts: Union[AxisLineOpts, dict, None] = None,</span><br><span class="line"></span><br><span class="line"> # 坐标轴刻度配置项,参考 `global_options.AxisTickOpts`</span><br><span class="line"> axistick_opts: Union[AxisTickOpts, dict, None] = None,</span><br><span class="line"></span><br><span class="line"> # 坐标轴标签配置项,参考 `series_options.LabelOpts`</span><br><span class="line"> axislabel_opts: Union[LabelOpts, dict, None] = None,</span><br><span class="line"></span><br><span class="line"> # 坐标轴指示器配置项,参考 `global_options.AxisPointerOpts`</span><br><span class="line"> axispointer_opts: Union[AxisPointerOpts, dict, None] = None,</span><br><span class="line"></span><br><span class="line"> # 坐标轴名称的文字样式,参考 `series_options.TextStyleOpts`</span><br><span class="line"> name_textstyle_opts: Union[TextStyleOpts, dict, None] = None,</span><br><span class="line"></span><br><span class="line"> # 分割区域配置项,参考 `series_options.SplitAreaOpts`</span><br><span class="line"> splitarea_opts: Union[SplitAreaOpts, dict, None] = None,</span><br><span class="line"></span><br><span class="line"> # 分割线配置项,参考 `series_options.SplitLineOpts`</span><br><span class="line"> splitline_opts: Union[SplitLineOpts, dict] = SplitLineOpts(),</span><br><span class="line"></span><br><span class="line"> # 坐标轴次刻度线相关设置,参考 `series_options.MinorTickOpts`</span><br><span class="line"> minor_tick_opts: Union[MinorTickOpts, dict, None] = None,</span><br><span class="line"></span><br><span class="line"> # 坐标轴在 grid 区域中的次分隔线。次分割线会对齐次刻度线 minorTick,参考 `series_options.MinorSplitLineOpts`</span><br><span class="line"> minor_split_line_opts: Union[MinorSplitLineOpts, dict, None] = None,</span><br><span class="line">)</span><br></pre></td></tr></table></figure></li><li><p><code>SingleAxisOpts:单轴配置项</code></p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br></pre></td><td class="code"><pre><span class="line">class SingleAxisOpts(</span><br><span class="line"> # 坐标轴名称。</span><br><span class="line"> name: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 坐标轴刻度最大值。</span><br><span class="line"> # 可以设置成特殊值 'dataMax',此时取数据在该轴上的最大值作为最大刻度。</span><br><span class="line"> # 不设置时会自动计算最大值保证坐标轴刻度的均匀分布。</span><br><span class="line"> # 在类目轴中,也可以设置为类目的序数(如类目轴 data: ['类A', '类B', '类C'] 中,序数 2 表示 '类C'。</span><br><span class="line"> # 也可以设置为负数,如 -3)。</span><br><span class="line"> max_: Union[str, Numeric, None] = None,</span><br><span class="line"></span><br><span class="line"> # 坐标轴刻度最小值。</span><br><span class="line"> # 可以设置成特殊值 'dataMin',此时取数据在该轴上的最小值作为最小刻度。</span><br><span class="line"> # 不设置时会自动计算最小值保证坐标轴刻度的均匀分布。</span><br><span class="line"> # 在类目轴中,也可以设置为类目的序数(如类目轴 data: ['类A', '类B', '类C'] 中,序数 2 表示 '类C'。</span><br><span class="line"> # 也可以设置为负数,如 -3)。</span><br><span class="line"> min_: Union[str, Numeric, None] = None,</span><br><span class="line"></span><br><span class="line"> # single 组件离容器左侧的距离。</span><br><span class="line"> # left 的值可以是像 20 这样的具体像素值,可以是像 '20%' 这样相对于容器高宽的百分比,</span><br><span class="line"> # 也可以是 'left', 'center', 'right'。</span><br><span class="line"> # 如果 left 的值为'left', 'center', 'right',组件会根据相应的位置自动对齐。</span><br><span class="line"> pos_left: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # single组件离容器右侧的距离。</span><br><span class="line"> # right 的值可以是像 20 这样的具体像素值,可以是像 '20%' 这样相对于容器高宽的百分比。</span><br><span class="line"> pos_right: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # single组件离容器上侧的距离。</span><br><span class="line"> # top 的值可以是像 20 这样的具体像素值,可以是像 '20%' 这样相对于容器高宽的百分比,</span><br><span class="line"> # 也可以是 'top', 'middle', 'bottom'。</span><br><span class="line"> # 如果 top 的值为'top', 'middle', 'bottom',组件会根据相应的位置自动对齐。</span><br><span class="line"> pos_top: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # single组件离容器下侧的距离。</span><br><span class="line"> # bottom 的值可以是像 20 这样的具体像素值,可以是像 '20%' 这样相对于容器高宽的百分比。</span><br><span class="line"> pos_bottom: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # single 组件的宽度。默认自适应。</span><br><span class="line"> width: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # single 组件的高度。默认自适应。</span><br><span class="line"> height: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 轴的朝向,默认水平朝向,可以设置成 'vertical' 垂直朝向。</span><br><span class="line"> orient: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 坐标轴类型。可选:</span><br><span class="line"> # 'value': 数值轴,适用于连续数据。</span><br><span class="line"> # 'category': 类目轴,适用于离散的类目数据,为该类型时必须通过 data 设置类目数据。</span><br><span class="line"> # 'time': 时间轴,适用于连续的时序数据,与数值轴相比时间轴带有时间的格式化,在刻度计算上也有所不同,</span><br><span class="line"> # 例如会根据跨度的范围来决定使用月,星期,日还是小时范围的刻度。</span><br><span class="line"> # 'log' 对数轴。适用于对数数据。</span><br><span class="line"> type_: Optional[str] = None,</span><br><span class="line">)</span><br></pre></td></tr></table></figure></li></ol><ol start="10"><li><p><code>GraphicGroup:原生图形元素组件</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">class GraphicGroup(</span><br><span class="line"> # 图形的配置项</span><br><span class="line"> graphic_item: Union[GraphicItem, dict, None] = None,</span><br><span class="line"></span><br><span class="line"> # 根据其 children 中每个图形元素的 name 属性进行重绘</span><br><span class="line"> is_diff_children_by_name: bool = False,</span><br><span class="line"></span><br><span class="line"> # 子节点列表,其中项都是一个图形元素定义。</span><br><span class="line"> # 目前可以选择 GraphicText,GraphicImage,GraphicRect</span><br><span class="line"> children: Optional[Sequence[BaseGraphic]] = None,</span><br><span class="line">)</span><br></pre></td></tr></table></figure><ol><li><p><code>GraphicItem:原生图形配置项</code></p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br></pre></td><td class="code"><pre><span class="line">class GraphicItem(</span><br><span class="line"> # id 用于在更新或删除图形元素时指定更新哪个图形元素,如果不需要用可以忽略。</span><br><span class="line"> id_: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # setOption 时指定本次对该图形元素的操作行为。可选:</span><br><span class="line"> # 'merge':如果已有元素,则新的配置项和已有的设定进行 merge。如果没有则新建。</span><br><span class="line"> # 'replace':如果已有元素,删除之,新建元素替代之。</span><br><span class="line"> # 'remove':删除元素。</span><br><span class="line"> action: str = "merge",</span><br><span class="line"></span><br><span class="line"> # 平移(position):默认值是 [0, 0]。表示 [横向平移的距离, 纵向平移的距离]。右和下为正值。</span><br><span class="line"> position: [Sequence, Numeric, None] = None,</span><br><span class="line"></span><br><span class="line"> # 旋转(rotation):默认值是 0。表示旋转的弧度值。正值表示逆时针旋转。</span><br><span class="line"> rotation: Union[Numeric, JSFunc, None] = 0,</span><br><span class="line"></span><br><span class="line"> # 缩放(scale):默认值是 [1, 1]。表示 [横向缩放的倍数, 纵向缩放的倍数]。</span><br><span class="line"> scale: Union[Sequence, Numeric, None] = None,</span><br><span class="line"></span><br><span class="line"> # origin 指定了旋转和缩放的中心点,默认值是 [0, 0]。</span><br><span class="line"> origin: Union[Numeric, Sequence, None] = None,</span><br><span class="line"></span><br><span class="line"> # 描述怎么根据父元素进行定位。</span><br><span class="line"> # 父元素是指:如果是顶层元素,父元素是 echarts 图表容器。如果是 group 的子元素,父元素就是 group 元素。</span><br><span class="line"> # 值的类型可以是:</span><br><span class="line"> # 数值:表示像素值。</span><br><span class="line"> # 百分比值:如 '33%',用父元素的高和此百分比计算出最终值。</span><br><span class="line"> # 位置:如 'center':表示自动居中。</span><br><span class="line"> # 注:left 和 right 只有一个可以生效。如果指定 left 或 right,则 shape 里的 x、cx 等定位属性不再生效。</span><br><span class="line"> left: Union[Numeric, str, None] = None,</span><br><span class="line"></span><br><span class="line"> # 同上</span><br><span class="line"> right: Union[Numeric, str, None] = None,</span><br><span class="line"></span><br><span class="line"> # 配置和 left 及 right 相同, 注:top 和 bottom 只有一个可以生效。</span><br><span class="line"> top: Union[Numeric, str, None] = None,</span><br><span class="line"></span><br><span class="line"> # 同上</span><br><span class="line"> bottom: Union[Numeric, str, None] = None,</span><br><span class="line"></span><br><span class="line"> # 决定此图形元素在定位时,对自身的包围盒计算方式。可选:</span><br><span class="line"> # 'all':(默认) 表示用自身以及子节点整体的经过 transform 后的包围盒进行定位。这种方式易于使整体都限制在父元素范围中。</span><br><span class="line"> # 'raw':表示仅仅用自身(不包括子节点)的没经过 tranform 的包围盒进行定位。这种方式易于内容超出父元素范围的定位方式。</span><br><span class="line"> bounding: str = "all",</span><br><span class="line"></span><br><span class="line"> # z 方向的高度,决定层叠关系。</span><br><span class="line"> z: Numeric = 0,</span><br><span class="line"></span><br><span class="line"> # 决定此元素绘制在哪个 canvas 层中。注意,越多 canvas 层会占用越多资源。</span><br><span class="line"> z_level: Numeric = 0,</span><br><span class="line"></span><br><span class="line"> # 是否不响应鼠标以及触摸事件。</span><br><span class="line"> is_silent: bool = False,</span><br><span class="line"></span><br><span class="line"> # 节点是否可见。</span><br><span class="line"> is_invisible: bool = False,</span><br><span class="line"></span><br><span class="line"> # 节点是否完全被忽略(既不渲染,也不响应事件)。</span><br><span class="line"> is_ignore: bool = False,</span><br><span class="line"></span><br><span class="line"> # 鼠标悬浮时在图形元素上时鼠标的样式是什么。同 CSS 的 cursor。</span><br><span class="line"> cursor: str = "pointer",</span><br><span class="line"></span><br><span class="line"> # 图形元素是否可以被拖拽。</span><br><span class="line"> is_draggable: bool = False,</span><br><span class="line"></span><br><span class="line"> # 是否渐进式渲染。当图形元素过多时才使用。</span><br><span class="line"> is_progressive: bool = False,</span><br><span class="line"></span><br><span class="line"> # 用于描述此 group 的宽。这个宽只用于给子节点定位。</span><br><span class="line"> # 即便当宽度为零的时候,子节点也可以使用 left: 'center' 相对于父节点水平居中。</span><br><span class="line"> width: Numeric = 0,</span><br><span class="line"></span><br><span class="line"> # 用于描述此 group 的高。这个高只用于给子节点定位。</span><br><span class="line"> # 即便当高度为零的时候,子节点也可以使用 top: 'middle' 相对于父节点垂直居中。</span><br><span class="line"> height: Numeric = 0,</span><br><span class="line">)</span><br></pre></td></tr></table></figure></li><li><p><code>GraphicBasicStyleOpts:原生图形基础配置项</code></p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">class GraphicBasicStyleOpts(</span><br><span class="line"> # 填充色。</span><br><span class="line"> fill: str = "#000",</span><br><span class="line"></span><br><span class="line"> # 笔画颜色。</span><br><span class="line"> stroke: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 笔画宽度。</span><br><span class="line"> line_width: Numeric = 0,</span><br><span class="line"></span><br><span class="line"> # 阴影宽度。</span><br><span class="line"> shadow_blur: Optional[Numeric] = None,</span><br><span class="line"></span><br><span class="line"> # 阴影 X 方向偏移。</span><br><span class="line"> shadow_offset_x: Optional[Numeric] = None,</span><br><span class="line"></span><br><span class="line"> # 阴影 Y 方向偏移。</span><br><span class="line"> shadow_offset_y: Optional[Numeric] = None,</span><br><span class="line"></span><br><span class="line"> # 阴影颜色。</span><br><span class="line"> shadow_color: Optional[str] = None,</span><br><span class="line">)</span><br></pre></td></tr></table></figure></li><li><p><code>GraphicShapeOpts:原生图形形状配置项</code></p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">class GraphicShapeOpts(</span><br><span class="line"> # 图形元素的左上角在父节点坐标系(以父节点左上角为原点)中的横坐标值。</span><br><span class="line"> pos_x: Numeric = 0,</span><br><span class="line"></span><br><span class="line"> # 图形元素的左上角在父节点坐标系(以父节点左上角为原点)中的横坐标值。</span><br><span class="line"> pos_y: Numeric = 0,</span><br><span class="line"></span><br><span class="line"> # 图形元素的宽度。</span><br><span class="line"> width: Numeric = 0,</span><br><span class="line"></span><br><span class="line"> # 图形元素的高度。</span><br><span class="line"> height: Numeric = 0,</span><br><span class="line"></span><br><span class="line"> # 可以用于设置圆角矩形。r: [r1, r2, r3, r4], 左上、右上、右下、左下角的半径依次为r1、r2、r3、r4。</span><br><span class="line"> # 可以缩写,例如:</span><br><span class="line"> # r 缩写为 1 相当于 [1, 1, 1, 1]</span><br><span class="line"> # r 缩写为 [1] 相当于 [1, 1, 1, 1]</span><br><span class="line"> # r 缩写为 [1, 2] 相当于 [1, 2, 1, 2]</span><br><span class="line"> # r 缩写为 [1, 2, 3]1 相当于[1, 2, 3, 2]`</span><br><span class="line"> r: Union[Sequence, Numeric, None] = None,</span><br><span class="line">)</span><br></pre></td></tr></table></figure></li><li><p><code>GraphicImage:原始图形图片配置项</code></p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">class GraphicImage(</span><br><span class="line"> # 图形的配置项,参考 GraphicItem</span><br><span class="line"> graphic_item: Union[GraphicItem, dict, None] = None,</span><br><span class="line"></span><br><span class="line"> # 图形图片样式的配置项</span><br><span class="line"> graphic_imagestyle_opts: Union[GraphicImageStyleOpts, dict, None] = None,</span><br><span class="line">)</span><br></pre></td></tr></table></figure></li><li><p><code>GraphicImageStyleOpts:原始图形图片样式配置项</code></p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">class GraphicImageStyleOpts(</span><br><span class="line"> # 图片的内容,可以是图片的 URL。</span><br><span class="line"> image: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 图形元素的左上角在父节点坐标系(以父节点左上角为原点)中的横坐标值。</span><br><span class="line"> pos_x: Numeric = 0,</span><br><span class="line"></span><br><span class="line"> # 图形元素的左上角在父节点坐标系(以父节点左上角为原点)中的纵坐标值。</span><br><span class="line"> pos_y: Numeric = 0,</span><br><span class="line"></span><br><span class="line"> # 图形元素的宽度。</span><br><span class="line"> width: Numeric = 0,</span><br><span class="line"></span><br><span class="line"> # 图形元素的高度。</span><br><span class="line"> height: Numeric = 0,</span><br><span class="line"></span><br><span class="line"> # 透明度 0 到 1。1 即完整显示</span><br><span class="line"> opacity: Numeric = 1,</span><br><span class="line"></span><br><span class="line"> # 图形基本配置项,参考 GraphicBasicStyleOpts</span><br><span class="line"> graphic_basicstyle_opts: Union[GraphicBasicStyleOpts, dict, None] = None,</span><br><span class="line">)</span><br></pre></td></tr></table></figure></li><li><p><code>GraphicText:原始图形文本配置项</code></p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">class GraphText(</span><br><span class="line"> # 图形的配置项,参考 GraphicItem</span><br><span class="line"> graphic_item: Union[GraphicItem, dict, None] = None,</span><br><span class="line"></span><br><span class="line"> # 图形文本样式的配置项</span><br><span class="line"> graphic_textstyle_opts: Union[GraphicTextStyleOpts, dict, None] = None,</span><br><span class="line">)</span><br></pre></td></tr></table></figure></li><li><p><code>GraphicTextStyleOpts:原生图形文本样式配置项</code></p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">class GraphicTextStyleOpts(</span><br><span class="line"> # 文本块文字。可以使用 \n 来换行。</span><br><span class="line"> text: Optional[JSFunc] = None,</span><br><span class="line"></span><br><span class="line"> # 图形元素的左上角在父节点坐标系(以父节点左上角为原点)中的横坐标值。</span><br><span class="line"> pos_x: Numeric = 0,</span><br><span class="line"></span><br><span class="line"> # 图形元素的左上角在父节点坐标系(以父节点左上角为原点)中的纵坐标值。</span><br><span class="line"> pos_y: Numeric = 0,</span><br><span class="line"></span><br><span class="line"> # 字体大小、字体类型、粗细、字体样式。</span><br><span class="line"> # 例如:</span><br><span class="line"> # // size | family</span><br><span class="line"> # font: '2em "STHeiti", sans-serif'</span><br><span class="line"> # // style | weight | size | family</span><br><span class="line"> # font: 'italic bolder 16px cursive'</span><br><span class="line"> # // weight | size | family</span><br><span class="line"> # font: 'bolder 2em "Microsoft YaHei", sans-serif'</span><br><span class="line"> font: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 水平对齐方式,取值:'left', 'center', 'right'。默认值为:'left'</span><br><span class="line"> # 如果为 'left',表示文本最左端在 x 值上。如果为 'right',表示文本最右端在 x 值上。</span><br><span class="line"> text_align: str = "left",</span><br><span class="line"></span><br><span class="line"> # 垂直对齐方式,取值:'top', 'middle', 'bottom'。默认值为:'None'</span><br><span class="line"> text_vertical_align: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 图形基本配置项,参考 GraphicBasicStyleOpts</span><br><span class="line"> graphic_basicstyle_opts: Union[GraphicBasicStyleOpts, dict, None] = None,</span><br><span class="line">)</span><br></pre></td></tr></table></figure></li><li><p><code>GraphicRect:原生图形矩形配置项</code></p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">class GraphicRect(</span><br><span class="line"> # 图形的配置项,参考 GraphicItem</span><br><span class="line"> graphic_item: Union[GraphicItem, dict, None] = None,</span><br><span class="line"></span><br><span class="line"> # 图形的形状配置项,参考 GraphicShapeOpts</span><br><span class="line"> graphic_shape_opts: Union[GraphicShapeOpts, dict, None] = None,</span><br><span class="line"></span><br><span class="line"> # 图形基本配置项,参考 GraphicBasicStyleOpts</span><br><span class="line"> graphic_basicstyle_opts: Union[GraphicBasicStyleOpts, dict, None] = None,</span><br><span class="line">)</span><br></pre></td></tr></table></figure></li></ol></li><li><p><code>PolarOpts:极坐标系配置</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">class PolarOpts(</span><br><span class="line"> # 极坐标系的中心(圆心)坐标,数组的第一项是横坐标,第二项是纵坐标。</span><br><span class="line"> # 支持设置成百分比,设置成百分比时第一项是相对于容器宽度,第二项是相对于容器高度。</span><br><span class="line"> center: Optional[Sequence] = None,</span><br><span class="line"></span><br><span class="line"> # 极坐标系的半径。可以为如下类型:</span><br><span class="line"> # number:直接指定外半径值。</span><br><span class="line"> # string:例如,'20%',表示外半径为可视区尺寸(容器高宽中较小一项)的 20% 长度。</span><br><span class="line"> # Array.<number|string>:数组的第一项是内半径,第二项是外半径。每一项遵从上述 number string 的描述。</span><br><span class="line"> radius: Optional[Union[Sequence, str]] = None,</span><br><span class="line"></span><br><span class="line"> # 本坐标系特定的 tooltip 设定。参考 `global_options.TooltipOpts`</span><br><span class="line"> tooltip_opts: TooltipOpts = None,</span><br><span class="line">)</span><br></pre></td></tr></table></figure></li><li><p><code>TooltipOpts类,工具提示选项:提示框配置项</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br></pre></td><td class="code"><pre><span class="line">class TooltipOpts(</span><br><span class="line"> # 是否显示提示框组件,包括提示框浮层和 axisPointer。</span><br><span class="line"> is_show: bool = True,</span><br><span class="line"></span><br><span class="line"> # 触发类型。可选:</span><br><span class="line"> # 'item': 数据项图形触发,主要在散点图,饼图等无类目轴的图表中使用。</span><br><span class="line"> # 'axis': 坐标轴触发,主要在柱状图,折线图等会使用类目轴的图表中使用。</span><br><span class="line"> # 'none': 什么都不触发</span><br><span class="line"> trigger: str = "item",</span><br><span class="line"></span><br><span class="line"> # 提示框触发的条件,可选:</span><br><span class="line"> # 'mousemove': 鼠标移动时触发。</span><br><span class="line"> # 'click': 鼠标点击时触发。</span><br><span class="line"> # 'mousemove|click': 同时鼠标移动和点击时触发。</span><br><span class="line"> # 'none': 不在 'mousemove' 或 'click' 时触发,</span><br><span class="line"> trigger_on: str = "mousemove|click",</span><br><span class="line"></span><br><span class="line"> # 指示器类型。可选</span><br><span class="line"> # 'line':直线指示器</span><br><span class="line"> # 'shadow':阴影指示器</span><br><span class="line"> # 'none':无指示器</span><br><span class="line"> # 'cross':十字准星指示器。其实是种简写,表示启用两个正交的轴的 axisPointer。</span><br><span class="line"> axis_pointer_type: str = "line",</span><br><span class="line"></span><br><span class="line"> # 是否显示提示框浮层,默认显示。</span><br><span class="line"> # 只需 tooltip 触发事件或显示 axisPointer 而不需要显示内容时可配置该项为 false。</span><br><span class="line"> is_show_content: bool = True,</span><br><span class="line"></span><br><span class="line"> # 是否永远显示提示框内容,</span><br><span class="line"> # 默认情况下在移出可触发提示框区域后一定时间后隐藏,设置为 true 可以保证一直显示提示框内容。</span><br><span class="line"> is_always_show_content: bool = False,</span><br><span class="line"></span><br><span class="line"> # 浮层显示的延迟,单位为 ms,默认没有延迟,也不建议设置。</span><br><span class="line"> show_delay: Numeric = 0,</span><br><span class="line"></span><br><span class="line"> # 浮层隐藏的延迟,单位为 ms,在 alwaysShowContent 为 true 的时候无效。</span><br><span class="line"> hide_delay: Numeric = 100,</span><br><span class="line"></span><br><span class="line"> # 提示框浮层的位置,默认不设置时位置会跟随鼠标的位置。</span><br><span class="line"> # 1、通过数组配置:</span><br><span class="line"> # 绝对位置,相对于容器左侧 10px, 上侧 10 px ===> position: [10, 10]</span><br><span class="line"> # 相对位置,放置在容器正中间 ===> position: ['50%', '50%']</span><br><span class="line"> # 2、通过回调函数配置</span><br><span class="line"> # 3、固定参数配置:'inside','top','left','right','bottom'</span><br><span class="line"> position: Union[str, Sequence, JSFunc] = None,</span><br><span class="line"></span><br><span class="line"> # 标签内容格式器,支持字符串模板和回调函数两种形式,字符串模板与回调函数返回的字符串均支持用 \n 换行。</span><br><span class="line"> # 字符串模板 模板变量有:</span><br><span class="line"> # {a}:系列名。</span><br><span class="line"> # {b}:数据名。</span><br><span class="line"> # {c}:数据值。</span><br><span class="line"> # {@xxx}:数据中名为 'xxx' 的维度的值,如 {@product} 表示名为 'product'` 的维度的值。</span><br><span class="line"> # {@[n]}:数据中维度 n 的值,如{@[3]}` 表示维度 3 的值,从 0 开始计数。</span><br><span class="line"> # 示例:formatter: '{b}: {@score}'</span><br><span class="line"> # </span><br><span class="line"> # 回调函数,回调函数格式:</span><br><span class="line"> # (params: Object|Array) => string</span><br><span class="line"> # 参数 params 是 formatter 需要的单个数据集。格式如下:</span><br><span class="line"> # {</span><br><span class="line"> # componentType: 'series',</span><br><span class="line"> # // 系列类型</span><br><span class="line"> # seriesType: string,</span><br><span class="line"> # // 系列在传入的 option.series 中的 index</span><br><span class="line"> # seriesIndex: number,</span><br><span class="line"> # // 系列名称</span><br><span class="line"> # seriesName: string,</span><br><span class="line"> # // 数据名,类目名</span><br><span class="line"> # name: string,</span><br><span class="line"> # // 数据在传入的 data 数组中的 index</span><br><span class="line"> # dataIndex: number,</span><br><span class="line"> # // 传入的原始数据项</span><br><span class="line"> # data: Object,</span><br><span class="line"> # // 传入的数据值</span><br><span class="line"> # value: number|Array,</span><br><span class="line"> # // 数据图形的颜色</span><br><span class="line"> # color: string,</span><br><span class="line"> # }</span><br><span class="line"> formatter: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 提示框浮层的背景颜色。</span><br><span class="line"> background_color: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 提示框浮层的边框颜色。</span><br><span class="line"> border_color: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 提示框浮层的边框宽。</span><br><span class="line"> border_width: Numeric = 0,</span><br><span class="line"></span><br><span class="line"> # 文字样式配置项,参考 `series_options.TextStyleOpts`</span><br><span class="line"> textstyle_opts: TextStyleOpts = TextStyleOpts(font_size=14),</span><br><span class="line">)</span><br></pre></td></tr></table></figure></li><li><p><code>BrushOpts:区域选择组件配置项</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br></pre></td><td class="code"><pre><span class="line">class BrushOpts(</span><br><span class="line"> # 使用在 toolbox 中的按钮。默认值为 ["rect", "polygon", "keep", "clear"]</span><br><span class="line"> # brush 相关的 toolbox 按钮有:</span><br><span class="line"> # "rect":开启矩形选框选择功能。</span><br><span class="line"> # "polygon":开启任意形状选框选择功能。</span><br><span class="line"> # "lineX":开启横向选择功能。</span><br><span class="line"> # "lineY"':开启纵向选择功能。</span><br><span class="line"> # "keep":切换『单选』和『多选』模式。后者可支持同时画多个选框。前者支持单击清除所有选框。</span><br><span class="line"> # "clear":清空所有选框。</span><br><span class="line"> tool_box: Optional[Sequence] = None,</span><br><span class="line"></span><br><span class="line"> # 不同系列间,选中的项可以联动。</span><br><span class="line"> # brush_link 配置项是一个列表,内容是 seriesIndex,指定了哪些 series 可以被联动。</span><br><span class="line"> # 例如可以是:</span><br><span class="line"> # [3, 4, 5] 表示 seriesIndex 为 3, 4, 5 的 series 可以被联动。</span><br><span class="line"> # "all" 表示所有 series 都进行 brushLink。</span><br><span class="line"> # None 表示不启用 brush_link 功能。</span><br><span class="line"> brush_link: Union[Sequence, str] = None,</span><br><span class="line"></span><br><span class="line"> # 指定哪些 series 可以被刷选,可取值为:</span><br><span class="line"> # "all": 所有 series</span><br><span class="line"> # series index 列表, 如 [0, 4, 2],表示指定这些 index 所对应的坐标系。</span><br><span class="line"> # 某个 series index, 如 0,表示这个 index 所对应的坐标系。</span><br><span class="line"> series_index: Union[Sequence, Numeric, str] = None,</span><br><span class="line"></span><br><span class="line"> # 指定哪些 geo 可以被刷选。可以设置 brush 是全局的还是属于坐标系的。</span><br><span class="line"> # 全局 brush</span><br><span class="line"> # 在 echarts 实例中任意地方刷选。这是默认情况。如果没有指定为坐标系 brush,就是全局 brush。</span><br><span class="line"> # 坐标系 brush</span><br><span class="line"> # 在指定的坐标系中刷选。选框可以跟随坐标系的缩放和平移( roam 和 dataZoom )而移动。</span><br><span class="line"> # 坐标系 brush 实际更为常用,尤其是在 geo 中。</span><br><span class="line"> # 通过指定 brush.geoIndex 或 brush.xAxisIndex 或 brush.yAxisIndex 来规定可以在哪些坐标系中进行刷选。</span><br><span class="line"> # 指定哪些 series 可以被刷选,可取值为:</span><br><span class="line"> # "all": 表示所有 series</span><br><span class="line"> # series index 列表, 如 [0, 4, 2],表示指定这些 index 所对应的坐标系。</span><br><span class="line"> # 某个 series index, 如 0,表示这个 index 所对应的坐标系。</span><br><span class="line"> geo_index: Union[Sequence, Numeric, str] = None,</span><br><span class="line"></span><br><span class="line"> # 指定哪些 xAxisIndex 可以被刷选。可以设置 brush 是全局的还是属于坐标系的。</span><br><span class="line"> # 全局 brush</span><br><span class="line"> # 在 echarts 实例中任意地方刷选。这是默认情况。如果没有指定为坐标系 brush,就是全局 brush。</span><br><span class="line"> # 坐标系 brush</span><br><span class="line"> # 在指定的坐标系中刷选。选框可以跟随坐标系的缩放和平移( roam 和 dataZoom )而移动。</span><br><span class="line"> # 坐标系 brush 实际更为常用,尤其是在 geo 中。</span><br><span class="line"> # 通过指定 brush.geoIndex 或 brush.xAxisIndex 或 brush.yAxisIndex 来规定可以在哪些坐标系中进行刷选。</span><br><span class="line"> # 指定哪些 series 可以被刷选,可取值为:</span><br><span class="line"> # "all": 表示所有 series</span><br><span class="line"> # series index 列表, 如 [0, 4, 2],表示指定这些 index 所对应的坐标系。</span><br><span class="line"> # 某个 series index, 如 0,表示这个 index 所对应的坐标系。</span><br><span class="line"> x_axis_index: Union[Sequence, Numeric, str] = None,</span><br><span class="line"></span><br><span class="line"> # 指定哪些 yAxisIndex 可以被刷选。可以设置 brush 是全局的还是属于坐标系的。</span><br><span class="line"> # 全局 brush</span><br><span class="line"> # 在 echarts 实例中任意地方刷选。这是默认情况。如果没有指定为坐标系 brush,就是全局 brush。</span><br><span class="line"> # 坐标系 brush</span><br><span class="line"> # 在指定的坐标系中刷选。选框可以跟随坐标系的缩放和平移( roam 和 dataZoom )而移动。</span><br><span class="line"> # 坐标系 brush 实际更为常用,尤其是在 geo 中。</span><br><span class="line"> # 通过指定 brush.geoIndex 或 brush.xAxisIndex 或 brush.yAxisIndex 来规定可以在哪些坐标系中进行刷选。</span><br><span class="line"> # 指定哪些 series 可以被刷选,可取值为:</span><br><span class="line"> # "all": 表示所有 series</span><br><span class="line"> # series index 列表, 如 [0, 4, 2],表示指定这些 index 所对应的坐标系。</span><br><span class="line"> # 某个 series index, 如 0,表示这个 index 所对应的坐标系。</span><br><span class="line"> y_axis_index: Union[Sequence, Numeric, str] = None,</span><br><span class="line"></span><br><span class="line"> # 默认的刷子类型。默认值为 rect。</span><br><span class="line"> # 可选参数如下:</span><br><span class="line"> # "rect":矩形选框。</span><br><span class="line"> # "polygon":任意形状选框。</span><br><span class="line"> # "lineX":横向选择。</span><br><span class="line"> # "lineY":纵向选择。</span><br><span class="line"> brush_type: str = "rect",</span><br><span class="line"></span><br><span class="line"> # 默认的刷子的模式。可取值为:</span><br><span class="line"> # 默认为 single</span><br><span class="line"> # "single":单选。</span><br><span class="line"> # "multiple":多选。</span><br><span class="line"> brush_mode: str = "single",</span><br><span class="line"></span><br><span class="line"> # 已经选好的选框是否可以被调整形状或平移。默认值为 True</span><br><span class="line"> transformable: bool = True,</span><br><span class="line"></span><br><span class="line"> # 选框的默认样式,值为</span><br><span class="line"> # {</span><br><span class="line"> # "borderWidth": 1,</span><br><span class="line"> # "color": "rgba(120,140,180,0.3)",</span><br><span class="line"> # "borderColor": "rgba(120,140,180,0.8)"</span><br><span class="line"> # },</span><br><span class="line"> brush_style: Optional[dict] = None,</span><br><span class="line"></span><br><span class="line"> # 默认情况,刷选或者移动选区的时候,会不断得发 brushSelected 事件,从而告诉外界选中的内容。</span><br><span class="line"> # 但是频繁的事件可能导致性能问题,或者动画效果很差。</span><br><span class="line"> # 所以 brush 组件提供了 brush.throttleType,brush.throttleDelay 来解决这个问题。</span><br><span class="line"> # throttleType 取值可以是:</span><br><span class="line"> # "debounce":表示只有停止动作了(即一段时间没有操作了),才会触发事件。时间阈值由 brush.throttleDelay 指定。</span><br><span class="line"> # "fixRate":表示按照一定的频率触发事件,时间间隔由 brush.throttleDelay 指定。</span><br><span class="line"> throttle_type: str = "fixRate",</span><br><span class="line"></span><br><span class="line"> # 默认为 0 表示不开启 throttle。</span><br><span class="line"> throttle_delay: Numeric = 0,</span><br><span class="line"></span><br><span class="line"> # 在 brush_mode 为 "single" 的情况下,是否支持单击清除所有选框。</span><br><span class="line"> remove_on_click: bool = True,</span><br><span class="line"></span><br><span class="line"> # 定义在选中范围外的视觉元素。最终参数以字典的形式进行配置</span><br><span class="line"> # 可选的视觉元素有:</span><br><span class="line"> # symbol: 图元的图形类别。</span><br><span class="line"> # symbolSize: 图元的大小。</span><br><span class="line"> # color: 图元的颜色。</span><br><span class="line"> # colorAlpha: 图元的颜色的透明度。</span><br><span class="line"> # opacity: 图元以及其附属物(如文字标签)的透明度。</span><br><span class="line"> # colorLightness: 颜色的明暗度,参见 https://en.wikipedia.org/wiki/HSL_and_HSV。</span><br><span class="line"> # colorSaturation: 颜色的饱和度,参见 https://en.wikipedia.org/wiki/HSL_and_HSV。</span><br><span class="line"> # colorHue: 颜色的色调,参见 https://en.wikipedia.org/wiki/HSL_and_HSV。</span><br><span class="line"> out_of_brush: dict = None,</span><br><span class="line">)</span><br></pre></td></tr></table></figure></li><li><p><code>DataZoomOpts:区域缩放配置项</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br></pre></td><td class="code"><pre><span class="line">class DataZoomOpts(</span><br><span class="line"> # 是否显示 组件。如果设置为 false,不会显示,但是数据过滤的功能还存在。</span><br><span class="line"> is_show: bool = True,</span><br><span class="line"></span><br><span class="line"> # 组件类型,可选 "slider", "inside"</span><br><span class="line"> type_: str = "slider",</span><br><span class="line"></span><br><span class="line"> # 拖动时,是否实时更新系列的视图。如果设置为 false,则只在拖拽结束的时候更新。</span><br><span class="line"> is_realtime: bool = True,</span><br><span class="line"></span><br><span class="line"> # 数据窗口范围的起始百分比。范围是:0 ~ 100。表示 0% ~ 100%。</span><br><span class="line"> range_start: Union[Numeric, None] = 20,</span><br><span class="line"></span><br><span class="line"> # 数据窗口范围的结束百分比。范围是:0 ~ 100</span><br><span class="line"> range_end: Union[Numeric, None] = 80,</span><br><span class="line"></span><br><span class="line"> # 数据窗口范围的起始数值。如果设置了 start 则 startValue 失效。</span><br><span class="line"> start_value: Union[int, str, None] = None,</span><br><span class="line"></span><br><span class="line"> # 数据窗口范围的结束数值。如果设置了 end 则 endValue 失效。</span><br><span class="line"> end_value: Union[int, str, None] = None,</span><br><span class="line"></span><br><span class="line"> # 布局方式是横还是竖。不仅是布局方式,对于直角坐标系而言,也决定了,缺省情况控制横向数轴还是纵向数轴</span><br><span class="line"> # 可选值为:'horizontal', 'vertical'</span><br><span class="line"> orient: str = "horizontal",</span><br><span class="line"></span><br><span class="line"> # 设置 dataZoom-inside 组件控制的 x 轴(即 xAxis,是直角坐标系中的概念,参见 grid)。</span><br><span class="line"> # 不指定时,当 dataZoom-inside.orient 为 'horizontal'时,默认控制和 dataZoom 平行的第一个 xAxis</span><br><span class="line"> # 如果是 number 表示控制一个轴,如果是 Array 表示控制多个轴。</span><br><span class="line"> xaxis_index: Union[int, Sequence[int], None] = None,</span><br><span class="line"></span><br><span class="line"> # 设置 dataZoom-inside 组件控制的 y 轴(即 yAxis,是直角坐标系中的概念,参见 grid)。</span><br><span class="line"> # 不指定时,当 dataZoom-inside.orient 为 'horizontal'时,默认控制和 dataZoom 平行的第一个 yAxis</span><br><span class="line"> # 如果是 number 表示控制一个轴,如果是 Array 表示控制多个轴。</span><br><span class="line"> yaxis_index: Union[int, Sequence[int], None] = None,</span><br><span class="line"></span><br><span class="line"> # 是否锁定选择区域(或叫做数据窗口)的大小。</span><br><span class="line"> # 如果设置为 true 则锁定选择区域的大小,也就是说,只能平移,不能缩放。</span><br><span class="line"> is_zoom_lock: bool = False,</span><br><span class="line"></span><br><span class="line"> # dataZoom-slider 组件离容器左侧的距离。</span><br><span class="line"> # left 的值可以是像 20 这样的具体像素值,可以是像 '20%' 这样相对于容器高宽的百分比,</span><br><span class="line"> # 也可以是 'left', 'center', 'right'。</span><br><span class="line"> # 如果 left 的值为 'left', 'center', 'right',组件会根据相应的位置自动对齐。</span><br><span class="line"> pos_left: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # dataZoom-slider 组件离容器上侧的距离。</span><br><span class="line"> # top 的值可以是像 20 这样的具体像素值,可以是像 '20%' 这样相对于容器高宽的百分比,</span><br><span class="line"> # 也可以是 'top', 'middle', 'bottom'。</span><br><span class="line"> # 如果 top 的值为 'top', 'middle', 'bottom',组件会根据相应的位置自动对齐。</span><br><span class="line"> pos_top: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # dataZoom-slider 组件离容器右侧的距离。</span><br><span class="line"> # right 的值可以是像 20 这样的具体像素值,可以是像 '20%' 这样相对于容器高宽的百分比。</span><br><span class="line"> # 默认自适应。</span><br><span class="line"> pos_right: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # dataZoom-slider组件离容器下侧的距离。</span><br><span class="line"> # bottom 的值可以是像 20 这样的具体像素值,可以是像 '20%' 这样相对于容器高宽的百分比。</span><br><span class="line"> # 默认自适应。</span><br><span class="line"> pos_bottom: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # dataZoom 的运行原理是通过数据过滤以及在内部设置轴的显示窗口来达到数据窗口缩放的效果。</span><br><span class="line"> # 'filter':当前数据窗口外的数据,被过滤掉。即会影响其他轴的数据范围。</span><br><span class="line"> # 每个数据项,只要有一个维度在数据窗口外,整个数据项就会被过滤掉。</span><br><span class="line"> # 'weakFilter':当前数据窗口外的数据,被过滤掉。即会影响其他轴的数据范围。</span><br><span class="line"> # 每个数据项,只有当全部维度都在数据窗口同侧外部,整个数据项才会被过滤掉。</span><br><span class="line"> # 'empty':当前数据窗口外的数据,被设置为空。即不会影响其他轴的数据范围。</span><br><span class="line"> # 'none': 不过滤数据,只改变数轴范围。</span><br><span class="line"> filter_mode: str = "filter"</span><br><span class="line">)</span><br></pre></td></tr></table></figure></li><li><p><code>ToolBox 工具箱</code></p><ol><li><p><code>ToolBoxFeatureSaveAsImagesOpts:工具箱保存图片配置项</code></p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">class ToolBoxFeatureSaveAsImageOpts(</span><br><span class="line"> # 保存的图片格式。支持 'png' 和 'jpeg'。</span><br><span class="line"> type_: str = "png",</span><br><span class="line"></span><br><span class="line"> # 保存的文件名称,默认使用 title.text 作为名称。</span><br><span class="line"> name: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 保存的图片背景色,默认使用 backgroundColor,如果backgroundColor不存在的话会取白色。</span><br><span class="line"> background_color: str = "auto",</span><br><span class="line"></span><br><span class="line"> # 如果图表使用了 echarts.connect 对多个图表进行联动,则在导出图片时会导出这些联动的图表。该配置项决定了图表与图表之间间隙处的填充色。</span><br><span class="line"> connected_background_color: str = "#fff",</span><br><span class="line"></span><br><span class="line"> # 保存为图片时忽略的组件列表,默认忽略工具栏。</span><br><span class="line"> exclude_components: Optional[Sequence[str]] = None,</span><br><span class="line"></span><br><span class="line"> # 是否显示该工具。</span><br><span class="line"> is_show: bool = True,</span><br><span class="line"></span><br><span class="line"> # 提示语</span><br><span class="line"> title: str = "保存为图片",</span><br><span class="line"></span><br><span class="line"> # 可以通过 'image://url' 设置为图片,其中 URL 为图片的链接,或者 dataURI。</span><br><span class="line"> icon: Optional[JSFunc] = None,</span><br><span class="line"></span><br><span class="line"> # 保存图片的分辨率比例,默认跟容器相同大小,如果需要保存更高分辨率的,可以设置为大于 1 的值,例如 2。</span><br><span class="line"> pixel_ratio: Numeric = 1,</span><br><span class="line">):</span><br></pre></td></tr></table></figure></li><li><p><code>ToolBoxFeatureRestoreOpts:工具箱还原配置项</code></p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">class ToolBoxFeatureRestoreOpts(</span><br><span class="line"> # 是否显示该工具。</span><br><span class="line"> is_show: bool = True, </span><br><span class="line"></span><br><span class="line"> # 提示语</span><br><span class="line"> title: str = "还原", </span><br><span class="line"></span><br><span class="line"> # 可以通过 'image://url' 设置为图片,其中 URL 为图片的链接,或者 dataURI。</span><br><span class="line"> icon: Optional[JSFunc] = None</span><br><span class="line">):</span><br></pre></td></tr></table></figure></li><li><p><code>ToolBoxFeatureDataViewOpts:工具箱数据视图工具</code></p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line">class ToolBoxFeatureDataViewOpts(</span><br><span class="line"> # 是否显示该工具。</span><br><span class="line"> is_show: bool = True, </span><br><span class="line"></span><br><span class="line"> # 提示语</span><br><span class="line"> title: str = "还原", </span><br><span class="line"></span><br><span class="line"> # 可以通过 'image://url' 设置为图片,其中 URL 为图片的链接,或者 dataURI。</span><br><span class="line"> icon: Optional[JSFunc] = None</span><br><span class="line"></span><br><span class="line"> # 是否不可编辑(只读)。默认为 False</span><br><span class="line"> is_read_only: bool = False,</span><br><span class="line"></span><br><span class="line"> # 自定义 dataView 展现函数,用以取代默认的 textarea 使用更丰富的数据编辑。可以返回 dom 对象或者 html 字符串。</span><br><span class="line"> option_to_content: Optional[JSFunc] = None,</span><br><span class="line"></span><br><span class="line"> # 在使用 optionToContent 的情况下,如果支持数据编辑后的刷新,需要自行通过该函数实现组装 option 的逻辑。</span><br><span class="line"> content_to_option: Optional[JSFunc] = None,</span><br><span class="line"></span><br><span class="line"> # 数据视图上有三个话术,默认是['数据视图', '关闭', '刷新']。</span><br><span class="line"> lang: Optional[Sequence[str]] = None,</span><br><span class="line"></span><br><span class="line"> # 数据视图浮层背景色。</span><br><span class="line"> background_color: str = "#fff",</span><br><span class="line"></span><br><span class="line"> # 数据视图浮层文本输入区背景色。</span><br><span class="line"> text_area_color: str = "#fff",</span><br><span class="line"></span><br><span class="line"> # 数据视图浮层文本输入区边框颜色。</span><br><span class="line"> text_area_border_color: str = "#333",</span><br><span class="line"></span><br><span class="line"> # 文本颜色。</span><br><span class="line"> text_color: str = "#000",</span><br><span class="line"></span><br><span class="line"> # 按钮颜色。</span><br><span class="line"> button_color: str = "#c23531",</span><br><span class="line"></span><br><span class="line"> # 按钮文本颜色。</span><br><span class="line"> button_text_color: str = "#fff",</span><br><span class="line">):</span><br></pre></td></tr></table></figure></li><li><p><code>ToolBoxFeatureDataZoomOpts:工具箱区域缩放配置项</code></p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line">class ToolBoxFeatureDataZoomOpts(</span><br><span class="line"> # 是否显示该工具。</span><br><span class="line"> is_show: bool = True,</span><br><span class="line"></span><br><span class="line"> # 提示语</span><br><span class="line"> zoom_title: str = "区域缩放",</span><br><span class="line"></span><br><span class="line"> # 提示语</span><br><span class="line"> back_title: str = "区域缩放还原",</span><br><span class="line"></span><br><span class="line"> # 可以通过 'image://url' 设置为图片,其中 URL 为图片的链接,或者 dataURI。</span><br><span class="line"> zoom_icon: Optional[JSFunc] = None,</span><br><span class="line"></span><br><span class="line"> # 可以通过 'image://url' 设置为图片,其中 URL 为图片的链接,或者 dataURI。</span><br><span class="line"> back_icon: Optional[JSFunc] = None,</span><br><span class="line"></span><br><span class="line"> # 指定哪些 xAxis 被控制。如果缺省则控制所有的 x 轴。</span><br><span class="line"> # 如果设置为 false 则不控制任何x轴。如果设置成 3 则控制 axisIndex 为 3 的 x 轴。</span><br><span class="line"> # 如果设置为 [0, 3] 则控制 axisIndex 为 0 和 3 的 x 轴。</span><br><span class="line"> xaxis_index: Union[Numeric, Sequence, bool] = None,</span><br><span class="line"></span><br><span class="line"> # 指定哪些 yAxis 被控制。如果缺省则控制所有的 y 轴。</span><br><span class="line"> # 如果设置为 false 则不控制任何y轴。如果设置成 3 则控制 axisIndex 为 3 的 y 轴。</span><br><span class="line"> # 如果设置为 [0, 3] 则控制 axisIndex 为 0 和 3 的 y 轴。</span><br><span class="line"> yaxis_index: Union[Numeric, Sequence, bool] = None,</span><br><span class="line"></span><br><span class="line"> # dataZoom 的运行原理是通过数据过滤以及在内部设置轴的显示窗口来达到数据窗口缩放的效果。</span><br><span class="line"> # 'filter':当前数据窗口外的数据,被过滤掉。即会影响其他轴的数据范围。</span><br><span class="line"> # 每个数据项,只要有一个维度在数据窗口外,整个数据项就会被过滤掉。</span><br><span class="line"> # 'weakFilter':当前数据窗口外的数据,被过滤掉。即会影响其他轴的数据范围。</span><br><span class="line"> # 每个数据项,只有当全部维度都在数据窗口同侧外部,整个数据项才会被过滤掉。</span><br><span class="line"> # 'empty':当前数据窗口外的数据,被设置为空。即不会影响其他轴的数据范围。</span><br><span class="line"> # 'none': 不过滤数据,只改变数轴范围。</span><br><span class="line"> filter_mode: str = "filter",</span><br><span class="line">):</span><br></pre></td></tr></table></figure></li><li><p><code>ToolBoxFeatureMagicTypeOpts:工具箱动态类型切换配置项</code></p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line">class ToolBoxFeatureMagicTypeOpts(</span><br><span class="line"> # 是否显示该工具。</span><br><span class="line"> is_show: bool = True,</span><br><span class="line"></span><br><span class="line"> # 启用的动态类型</span><br><span class="line"> # 包括'line'(切换为折线图), 'bar'(切换为柱状图),</span><br><span class="line"> # 'stack'(切换为堆叠模式), 'tiled'(切换为平铺模式)。</span><br><span class="line"> type_: Optional[Sequence] = None, </span><br><span class="line"></span><br><span class="line"> # 各个类型的标题文本,可以分别配置。</span><br><span class="line"> line_title: str = "切换为折线图",</span><br><span class="line"></span><br><span class="line"> # 各个类型的标题文本,可以分别配置。</span><br><span class="line"> bar_title: str = "切换为柱状图",</span><br><span class="line"></span><br><span class="line"> # 各个类型的标题文本,可以分别配置。</span><br><span class="line"> stack_title: str = "切换为堆叠",</span><br><span class="line"></span><br><span class="line"> # 各个类型的标题文本,可以分别配置。</span><br><span class="line"> tiled_title: str = "切换为平铺",</span><br><span class="line"></span><br><span class="line"> # 各个类型的 icon path,可以分别配置。</span><br><span class="line"> line_icon: Optional[JSFunc] = None,</span><br><span class="line"></span><br><span class="line"> # 各个类型的 icon path,可以分别配置。</span><br><span class="line"> bar_icon: Optional[JSFunc] = None,</span><br><span class="line"></span><br><span class="line"> # 各个类型的 icon path,可以分别配置。</span><br><span class="line"> stack_icon: Optional[JSFunc] = None,</span><br><span class="line"></span><br><span class="line"> # 各个类型的 icon path,可以分别配置。</span><br><span class="line"> tiled_icon: Optional[JSFunc] = None,</span><br><span class="line">):</span><br></pre></td></tr></table></figure></li><li><p><code>ToolBoxFeatureBrushOpts:工具箱选框组件配置项</code></p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line">class ToolBoxFeatureBrushOpts(</span><br><span class="line"> # 使用的按钮,取值:</span><br><span class="line"> # 'rect':开启矩形选框选择功能。</span><br><span class="line"> # 'polygon':开启任意形状选框选择功能。</span><br><span class="line"> # 'lineX':开启横向选择功能。</span><br><span class="line"> # 'lineY':开启纵向选择功能。</span><br><span class="line"> # 'keep':切换『单选』和『多选』模式。后者可支持同时画多个选框。前者支持单击清除所有选框。</span><br><span class="line"> # 'clear':清空所有选框。</span><br><span class="line"> type_: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 每个按钮的 icon path。</span><br><span class="line"> rect_icon: Optional[JSFunc] = None,</span><br><span class="line"></span><br><span class="line"> # 每个按钮的 icon path。</span><br><span class="line"> polygon_icon: Optional[JSFunc] = None,</span><br><span class="line"></span><br><span class="line"> # 每个按钮的 icon path。</span><br><span class="line"> line_x_icon: Optional[JSFunc] = None,</span><br><span class="line"></span><br><span class="line"> # 每个按钮的 icon path。</span><br><span class="line"> line_y_icon: Optional[JSFunc] = None,</span><br><span class="line"></span><br><span class="line"> # 每个按钮的 icon path。</span><br><span class="line"> keep_icon: Optional[JSFunc] = None,</span><br><span class="line"></span><br><span class="line"> # 每个按钮的 icon path。</span><br><span class="line"> clear_icon: Optional[JSFunc] = None,</span><br><span class="line"></span><br><span class="line"> # 标题文本。</span><br><span class="line"> rect_title: str = "矩形选择",</span><br><span class="line"></span><br><span class="line"> # 标题文本。</span><br><span class="line"> polygon_title: str = "圈选",</span><br><span class="line"></span><br><span class="line"> # 标题文本。</span><br><span class="line"> line_x_title: str = "横向选择",</span><br><span class="line"></span><br><span class="line"> # 标题文本。</span><br><span class="line"> line_y_title: str = "纵向选择",</span><br><span class="line"></span><br><span class="line"> # 标题文本。</span><br><span class="line"> keep_title: str = "保持选择",</span><br><span class="line"></span><br><span class="line"> # 标题文本。</span><br><span class="line"> clear_title: str = "清除选择",</span><br><span class="line">):</span><br></pre></td></tr></table></figure></li><li><p><code>ToolBoxFeatureOpts:工具箱工具配置项</code></p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">class ToolBoxFeatureOpts(</span><br><span class="line"> # 保存为图片</span><br><span class="line"> save_as_image: Union[ToolBoxFeatureSaveAsImageOpts, dict] = ToolBoxFeatureSaveAsImageOpts(),</span><br><span class="line"></span><br><span class="line"> # 配置项还原 </span><br><span class="line"> restore: Union[ToolBoxFeatureRestoreOpts, dict] = ToolBoxFeatureRestoreOpts(),</span><br><span class="line"></span><br><span class="line"> # 数据视图工具,可以展现当前图表所用的数据,编辑后可以动态更新</span><br><span class="line"> data_view: Union[ToolBoxFeatureDataViewOpts, dict] = ToolBoxFeatureDataViewOpts(),</span><br><span class="line"></span><br><span class="line"> # 数据区域缩放。(目前只支持直角坐标系的缩放)</span><br><span class="line"> data_zoom: Union[ToolBoxFeatureDataZoomOpts, dict] = ToolBoxFeatureDataZoomOpts(),</span><br><span class="line"></span><br><span class="line"> # 动态类型切换。</span><br><span class="line"> magic_type: Union[ToolBoxFeatureMagicTypeOpts, dict] = ToolBoxFeatureMagicTypeOpts(),</span><br><span class="line"></span><br><span class="line"> # 选框组件的控制按钮。</span><br><span class="line"> brush: Union[ToolBoxFeatureBrushOpts, dict] = ToolBoxFeatureBrushOpts(),</span><br><span class="line">):</span><br></pre></td></tr></table></figure><hr></li><li><p><code>工具箱选择:工具箱配置项</code></p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line">class ToolboxOpts(</span><br><span class="line"> # 是否显示工具栏组件</span><br><span class="line"> is_show: bool = True,</span><br><span class="line"></span><br><span class="line"> # 工具栏 icon 的布局朝向。</span><br><span class="line"> # 可选:'horizontal', 'vertical'</span><br><span class="line"> orient: str = "horizontal",</span><br><span class="line"></span><br><span class="line"> # 工具栏组件离容器左侧的距离。</span><br><span class="line"> # left 的值可以是像 20 这样的具体像素值,可以是像 '20%' 这样相对于容器高宽的百分比,</span><br><span class="line"> # 也可以是 'left', 'center', 'right'。</span><br><span class="line"> # 如果 left 的值为'left', 'center', 'right',组件会根据相应的位置自动对齐</span><br><span class="line"> pos_left: str = "80%",</span><br><span class="line"></span><br><span class="line"> # 工具栏组件离容器右侧的距离。</span><br><span class="line"> # right 的值可以是像 20 这样的具体像素值,可以是像 '20%' 这样相对于容器高宽的百分比。</span><br><span class="line"> pos_right: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 工具栏组件离容器上侧的距离。</span><br><span class="line"> # top 的值可以是像 20 这样的具体像素值,可以是像 '20%' 这样相对于容器高宽的百分比,</span><br><span class="line"> # 也可以是 'top', 'middle', 'bottom'。</span><br><span class="line"> # 如果 top 的值为'top', 'middle', 'bottom',组件会根据相应的位置自动对齐。</span><br><span class="line"> pos_top: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 工具栏组件离容器下侧的距离。</span><br><span class="line"> # bottom 的值可以是像 20 这样的具体像素值,可以是像 '20%' 这样相对于容器高宽的百分比。</span><br><span class="line"> pos_bottom: Optional[str] = None,</span><br><span class="line"></span><br><span class="line"> # 各工具配置项,参考 `global_options.ToolBoxFeatureOpts`</span><br><span class="line"> feature: Union[ToolBoxFeatureOpts, dict] = ToolBoxFeatureOpts(),</span><br><span class="line">):</span><br></pre></td></tr></table></figure></li></ol></li></ol>]]></content>
<tags>
<tag> Pyecharts </tag>
</tags>
</entry>
<entry>
<title>linux_vi编辑器</title>
<link href="/2020/07/29/linux-vi-bian-ji-qi/"/>
<url>/2020/07/29/linux-vi-bian-ji-qi/</url>
<content type="html"><![CDATA[<h2 id="linux-vi-编辑器"><a href="#linux-vi-编辑器" class="headerlink" title="linux vi 编辑器"></a>linux vi 编辑器</h2><ol><li><h3 id="命令模式"><a href="#命令模式" class="headerlink" title="命令模式"></a>命令模式</h3><code>vi启动后默认进入的是命令模式,按一下[Esc]键可以返回命令模式。在命令模式中输入字幕“i”就可以进入vi的输入模式编辑文件。</code></li></ol><ul><li><h4 id="vi-的启动和退出"><a href="#vi-的启动和退出" class="headerlink" title="vi 的启动和退出"></a>vi 的启动和退出</h4><ul><li><h5 id="启动"><a href="#启动" class="headerlink" title="启动"></a>启动</h5><p> <code>直接输入命令vi不指定文件名,由于这是一个没有命名的空文件,显示了vi的版本信息。使用vi打开文件,如果文件存在打开存在文件;如果不存在的话,将以我们指定的参数作为文件名。</code></p></li><li><h5 id="光标移动"><a href="#光标移动" class="headerlink" title="光标移动"></a>光标移动</h5><ul><li><code>方向键</code></li><li><code>向上移动光标:k</code></li><li><code>向左移动光标:h</code> </li><li><code>向右移动光标:l</code></li><li><code>向下移动光标:j</code></li></ul></li><li><h5 id="翻页"><a href="#翻页" class="headerlink" title="翻页"></a>翻页</h5><ul><li><code>Ctrl+F 向前翻整页</code> </li><li><code>Ctrl+U 向前翻半页</code></li><li><code>Ctrl+B 向后翻整页</code></li><li><code>Ctrl+D 向后翻半页</code></li></ul></li><li><h5 id="行内快速跳转"><a href="#行内快速跳转" class="headerlink" title="行内快速跳转"></a>行内快速跳转</h5><ul><li><code>^ 移动到本行行首</code></li><li><code>$ 移动到本行行尾</code></li></ul></li><li><h5 id="在命令模式下,使用以下命令可以快速的在行间跳转"><a href="#在命令模式下,使用以下命令可以快速的在行间跳转" class="headerlink" title="在命令模式下,使用以下命令可以快速的在行间跳转"></a>在命令模式下,使用以下命令可以快速的在行间跳转</h5><ul><li><code>1G 跳转到文件首行</code></li><li><code>G 跳转到尾行</code></li><li><code>#G 跳转到文件的#行</code></li></ul></li><li><h5 id="删除操作-命令模式"><a href="#删除操作-命令模式" class="headerlink" title="删除操作 (命令模式)"></a>删除操作 (命令模式)</h5><ul><li><code>x 删除光标处的单个字符</code></li><li><code>dd 删除光标所在行</code></li><li><code>dw 删除当前字符到单词尾包括空格的所有字符</code></li><li><code>#x 例如3x删除光标处向右的三个字符</code></li><li><code>#dd 例如3dd从当前行开始向下删除三行文本</code></li></ul></li><li><h5 id="复制操作"><a href="#复制操作" class="headerlink" title="复制操作"></a>复制操作</h5><ul><li><p><code>yy 命令复制当前整行的内容到vi缓冲区</code></p></li><li><p><code>yw 复制当前光标所在位置到单词尾字符的内容到vi缓存区,相当于复制一个单词</code></p></li><li><p><code>y$ 复制光标所在位置到行尾内容到缓存区</code></p></li><li><p><code>y^ 复制光标所在位置到行首内容到缓存区</code></p><p> <code>#yy例如:5yy就是复制5行 #yw例如:2yw就是复制两个单词</code></p></li></ul></li><li><h5 id="恢复文件"><a href="#恢复文件" class="headerlink" title="恢复文件"></a>恢复文件</h5><p> <code>vi在编辑某一个文件时,会生成一个临时文件,这个文件以 . 开头并以 .swp结尾。正常退出该文件自动删除,如果意外退出例如忽然断电,该文件不会删除,我们在下次编辑时可以选择一下命令处理</code></p><ul><li><code>O 只读打开,不改变文件内容</code></li><li><code>E 继续编辑文件,不恢复.swp文件保存的内容</code></li><li><code>R 将恢复上次编辑以后未保存文件内容</code></li><li><code>Q 退出vi</code></li><li><code>D 删除.swp文件+ 或者使用vi -r 文件名来恢复未保存的内容</code></li></ul></li></ul></li></ul><ol start="2"><li><h3 id="输入模式"><a href="#输入模式" class="headerlink" title="输入模式"></a>输入模式</h3><p> <code>在这个模式中我们可以编辑、修改、输入等编辑工作,在编辑器最后一行显示一个“--INSERT--”标志着vi进入了输入模式。当需要保存文件时,先返回命令模式,再进入末行模式保存。</code></p><ul><li><code>Home 光标到行首</code></li><li><code>End 光标到行尾</code></li><li><code>Page Up 和 Page Down 上下翻页</code></li><li><code>Delect 删除光标位置的字符</code></li><li><h4 id="编辑操作-进入输入模式命令"><a href="#编辑操作-进入输入模式命令" class="headerlink" title="编辑操作 进入输入模式命令"></a>编辑操作 进入输入模式命令</h4><ul><li><code>i 插入命令</code> </li><li><code>a 附加命令</code> </li><li><code>o 打开命令</code> </li><li><code>c 修改命令</code></li><li><code>r 取代命令</code></li><li><code>s 替换命令</code></li><li><code>Esc 退出命令</code></li></ul></li></ul></li><li><h3 id="末行模式"><a href="#末行模式" class="headerlink" title="末行模式"></a>末行模式</h3><p> <code>在命令模式输入": "即可进入该模式</code></p></li></ol><ul><li><h4 id="查找和替换"><a href="#查找和替换" class="headerlink" title="查找和替换"></a>查找和替换</h4><ul><li><h5 id="查找"><a href="#查找" class="headerlink" title="查找"></a>查找</h5><ul><li><p><code>至上而下的查找</code><br> <br> <code>/ 要查找的字符窜,其中/代表从光标所在位置起开始查找,例如:/ work</code></p></li><li><p><code>至下而上的查找</code></p><p> <code>?要查找的字符窜 例如:/ work</code></p></li><li><p><code>替换</code> <code>如果替换的范围较大时,在所有的命令尾加一个c命令,强制每个替换需要用户进行确认,例如:s/old/new/c 或s/old/new/gc</code></p><ul><li><code>:s/old/new 用new替换行中首次出现的old</code></li><li><code>: s/old/new/g 用new替换行中所有出现的old</code></li><li><code>:#,# s/old/new/g 用new替换从第#行到第#行中出现的old</code></li><li><code>:% s/old/new/g 用new替换整篇中出现的old</code></li></ul></li></ul></li><li><h5 id="保存文件"><a href="#保存文件" class="headerlink" title="保存文件"></a>保存文件</h5><p> <code>如果文件已经有文件名,我们在末行模式直接输入w保存,如果没有文件名,输入w 文件名进行保存或另存为。例如:w test其中test是文件名。</code></p></li><li><h5 id="退出"><a href="#退出" class="headerlink" title="退出"></a>退出</h5><ul><li><code>q! : 强制退出,不保存当前文件内容</code></li><li><code>q : 正常退出,不保存需改</code></li><li><code>wq : 保存退出</code></li><li><code>wq!: 保存强制退出</code></li></ul></li><li><h5 id="显示行号和取消行号-末行模式使用"><a href="#显示行号和取消行号-末行模式使用" class="headerlink" title="显示行号和取消行号(末行模式使用)"></a>显示行号和取消行号(末行模式使用)</h5><ul><li><code>set nu 显示行号</code></li><li><code>set nonu 取消行号</code></li></ul></li><li><h5 id="撤销操作"><a href="#撤销操作" class="headerlink" title="撤销操作"></a>撤销操作</h5><ul><li><code>u 命令取消最近一次的操作,可以使用多次来恢复原有的操作</code></li><li><code>U 取消所有操作</code></li><li><code>Ctrl+R 可以恢复对使用u命令的操作</code></li></ul></li></ul></li></ul>]]></content>
<tags>
<tag> Linux_study </tag>
</tags>
</entry>
<entry>
<title>linux_常用命令</title>
<link href="/2020/07/29/linux-chang-yong-ming-ling/"/>
<url>/2020/07/29/linux-chang-yong-ming-ling/</url>
<content type="html"><![CDATA[<h2 id="基础命令"><a href="#基础命令" class="headerlink" title="基础命令"></a>基础命令</h2><p><strong><em>命令格式:</em></strong></p><p><code>命令字 [命令选项] [命令参数]</code></p><p><code>Command [option] [arguments]</code></p><p><strong><em>文件类型</em></strong></p><ul><li>“ * “在文件名后: <code>可执行文件</code></li><li>“ @ “在文件名后: <code>符号链接</code></li><li>“ / “在文件名后: <code>目录名</code></li></ul><hr><h3 id="1-ls:"><a href="#1-ls:" class="headerlink" title="1. ls:"></a>1. ls:</h3><p><code>列出目录内容</code> </p><ul><li><p>语法</p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">ls [-laAbBcCdDfFgGHiklLmnNopqQrRsStuUvxX] </span><br><span class="line"> [-I <范本样式>] </span><br><span class="line"> [-T <跳格字数>] </span><br><span class="line"> [-w <每列字符数>] </span><br><span class="line"> [--block-size=<区块大小>] </span><br><span class="line"> [--color=<使用时机>] </span><br><span class="line"> [--format=<列表格式>] </span><br><span class="line"> [-full-time] </span><br><span class="line"> [--help]</span><br><span class="line"> [--indicator-style=<标注样式>]</span><br><span class="line"> [--quoting-style=<引号样式>]</span><br><span class="line"> [--show-control-chars]</span><br><span class="line"> [--sort=<排列方式>]</span><br><span class="line"> [--time=<时间戳记>]</span><br><span class="line"> [--version]</span><br><span class="line"> [文件或目录...]</span><br></pre></td></tr></table></figure></li><li><p>常用命令</p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">ls -a 列出所有文件,包括隐藏文件</span><br><span class="line">ls -l 详细列出目录下文件的权限、所有者、文件大小等详细信息 <简写为: ll></span><br><span class="line">ls -F 列出目录下文件名及其目录类型</span><br><span class="line">ls -t 依照最后修改文件时间列出文件名</span><br><span class="line">ls -R 列出当前目录和其子目录的文件名</span><br><span class="line">ls -color=never 不以颜色方式显示</span><br><span class="line">ls -color=always 均以颜色方式显示</span><br><span class="line">ls -color=auto 系统自动判断</span><br></pre></td></tr></table></figure></li></ul><hr><h3 id="2-cd"><a href="#2-cd" class="headerlink" title="2. cd"></a>2. cd</h3><p><code>切换用户当前目录</code></p><ul><li> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">cd [目的目录 (绝对路径或相对路径) ]</span><br><span class="line">cd ~ 返回用户宿主目录</span><br><span class="line">cd . 用户的当前目录</span><br><span class="line">cd .. 返回上一级目录</span><br></pre></td></tr></table></figure></li></ul><hr><h3 id="3-pwd"><a href="#3-pwd" class="headerlink" title="3. pwd"></a>3. pwd</h3><p><code>查看用户当前所在位置</code></p><hr><h3 id="4-mkdir"><a href="#4-mkdir" class="headerlink" title="4. mkdir"></a>4. mkdir</h3><p><code>创建目录,可在创建目录的同时设置目录的权限</code></p><ul><li>语法 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">mkdir [-p] [--help] [--version] [-m <目录属性>] [目录名称]</span><br></pre></td></tr></table></figure><ul><li>参数<ul><li>-m<目录属性> 或 –mode<目录属性> : <code>建立目录同时设置目录的权限</code></li><li>-p 或 –parents: <code>若所要建立目录的上层目录目前尚未建立,一并建立</code></li><li>–help: <code>显示帮助</code></li><li>–verbose: <code>显示执行时的详细信息</code></li><li>–version: <code>显示版本信息</code></li></ul></li></ul></li><li>同时建立多个目录 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">mkdir dir_name1 dir_name2 dir_name3</span><br></pre></td></tr></table></figure></li></ul><hr><h3 id="5-rmdir"><a href="#5-rmdir" class="headerlink" title="5. rmdir"></a>5. rmdir</h3><p><code>删除空目录,所删除目录必需为空</code> </p><ul><li>语法 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">rmdir [-p] [--help] [--ignore-fail-on-non-empty] [--verbose] [--version] [目录...]</span><br></pre></td></tr></table></figure><ul><li>参数<ul><li>-p 或 –parents: <code>删除指定目录后,若该目录的上层目录已变为空目录,则一并删除</code> </li><li>–help: <code>在线帮助</code></li><li>–ignore-fail-on-non-empty: <code>忽略非空目录的错误信息</code></li><li>–verbose: <code>显示指令执行过程</code></li><li>–version: <code>显示版本信息</code></li></ul></li></ul></li><li>同时删除多个空目录 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">rmdir dir_name1 dir_name2 dir_name3</span><br></pre></td></tr></table></figure></li></ul><hr><h3 id="6-cp"><a href="#6-cp" class="headerlink" title="6. cp"></a>6. cp</h3><p><code>复制文件或目录,可以同时输入多个文件或目录复制进入最后一个目录下 (如果最后一个目录存在的情况下)</code></p><ul><li>语法 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">cp [-abdfilpPrRsuvx]</span><br><span class="line"> [-S <备份字尾字符串>]</span><br><span class="line"> [-V <备份方式>]</span><br><span class="line"> [--help]</span><br><span class="line"> [--spares=<使用时机>]</span><br><span class="line"> [--version]</span><br><span class="line"> [源文件或目录]</span><br><span class="line"> [目标文件或目录]</span><br><span class="line"> [目的目录]</span><br></pre></td></tr></table></figure><ul><li>部分参数<ul><li>-b 或 –backup: <code>删除,覆盖目标文件之前的备份,备份文件会在字未添加备份字符串</code></li><li>-f 或 –force: <code>强制复制文件或目录,无论文件或目录是否存在</code></li><li>-l 或 –link: <code>对源文件建立硬连接而不是复制</code></li><li>-P 或 –preserve: <code>保留源文件或目录属性</code></li><li>-p 或 –parents: <code>保留源文件或目录的路径</code></li><li>-r 或 –recursive: <code>递归处理,将指定目录下文件与子目录一并处理</code></li></ul></li></ul></li><li>常用命令<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">cp 被复制文件或目录 复制进目录</span><br></pre></td></tr></table></figure></li></ul><hr><h3 id="7-rm"><a href="#7-rm" class="headerlink" title="7. rm"></a>7. rm</h3><p><code>删除文件或目录,删除目录必须加上 "-r"</code></p><ul><li>语法 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">rm [-dfirv] [--help] [--version] [文件或目录...]</span><br></pre></td></tr></table></figure><ul><li>参数<ul><li>-d 或 –directory: <code>直接把欲删除的目录的硬连接数据删成0,删除该目录。</code> </li><li>-f 或 –force: <code>强制删除文件或目录。</code></li><li>-i 或 –interactive: <code>删除既有文件或目录之前先询问用户。</code> </li><li>-r 或 -R 或 –recursive: <code>递归处理,将指定目录下的所有文件及子目录一并处理。</code> </li><li>-v 或 –verbose <code>显示指令执行过程。</code> </li><li>–help: <code>在线帮助。</code> </li><li>–version: <code>显示版本信息。</code></li></ul></li></ul></li><li>常用命令<ul><li>rm -r: <code>循环删除到没有</code></li><li>rm -f: <code>强制删除</code></li><li>rm -rf: <code>强制删除文件</code></li></ul></li></ul><hr><h3 id="8-mv"><a href="#8-mv" class="headerlink" title="8. mv"></a>8. mv</h3><p><code>对文件或目录进行重命名或移动</code></p><ul><li>语法 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">mv [-bfiuv] [--help] [-S <附加字尾>] [-V <方法>] [源文件或目录] [目标文件或目录]</span><br></pre></td></tr></table></figure><ul><li>参数<ul><li>-b 或 –backup: <code>若需覆盖文件,则覆盖前先行备份。</code> </li><li>-f 或 –force: <code>若目标文件或目录与现有的文件或目录重复,则直接覆盖现有的文 件或目录。</code></li><li>-i 或 –interactive: <code>覆盖前先行询问用户。</code></li><li>-S<附加字尾> 或 –suffix=<附加字尾>: <code>与-b参数一并使用,可指定备份文件的所要附加的字尾。</code></li><li>-u 或 –update: <code>在移动或更改文件名时,若目标文件已存在,且其文件日期比源文件新,则不覆盖目标文件。</code> </li><li>-v 或 –verbose: <code>执行时显示详细的信息。</code> </li><li>-V=<方法>或 –version-control=<方法>: <code>与-b参数一并使用,可指定备份的方法。</code> </li><li>–help: <code>显示帮助。</code></li><li>–version: <code>显示版本信息。</code></li></ul></li></ul></li></ul><hr><h3 id="9-cat"><a href="#9-cat" class="headerlink" title="9. cat"></a>9. cat</h3><p><code>用于最简单的文本查看,显示文件没有停顿,从第一行开始,最后只显示最后一行,不适合长文件</code></p><ul><li>语法 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">cat [-AbeEnstTuv] [--hellp] [--version] filename</span><br></pre></td></tr></table></figure><ul><li>参数<ul><li>-n 或 –number: <code>由 1 开始对所有输出的行数编号</code></li><li>-b 或 –number-nonblank 和 -n 相似: <code>对于空白行不编号</code></li><li>-s 或 –squeeze-blank: <code>当遇到有连续两行以上的空白行,就代换为一行的空白行</code></li><li>-v 或 –show-nonprinting</li></ul></li></ul></li><li>常用命令 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">把 textfile1 的档案内容加上行号输入 textfile2 档案:</span><br><span class="line">cat -n textfile1 > textfile2</span><br><span class="line">把 textfile1 和 textfile2 的档案内容加上行号(空白行不加)之后将内容附加到 textfile3 里:</span><br><span class="line">cat -b textfile1 textfile2 >> textfile3</span><br></pre></td></tr></table></figure></li></ul><hr><h3 id="10-more-less"><a href="#10-more-less" class="headerlink" title="10. more/less"></a>10. more/less</h3><p><code>与 cat 类似,用于大文件,分屏显示文件内容 (less最后一行显示当前阅读文件名称)</code></p><ul><li>语法 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">more [文件名]</span><br><span class="line">less [文件名]</span><br></pre></td></tr></table></figure></li></ul><hr><h3 id="11-find"><a href="#11-find" class="headerlink" title="11. find"></a>11. find</h3><p><code>查找符合条件的文件或目录,速率低下</code></p><ul><li>语法 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">find [目录...]</span><br><span class="line"> [-amin <分钟>]</span><br><span class="line"> [-anewer <参考文件或目录>]</span><br><span class="line"> [-atime <24小时数>]</span><br><span class="line"> [-cmin <分钟>]</span><br><span class="line"> [-user <拥有者名称>]</span><br><span class="line"> ...</span><br></pre></td></tr></table></figure></li></ul><hr><h3 id="12-diff"><a href="#12-diff" class="headerlink" title="12. diff"></a>12. diff</h3><p><code>逐行比较文件的差异,需要指定比较目录,会比较目录中相同文件名的文件,但不会比较子目录</code></p><ul><li>语法 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">diff [-abBcdefHilnNpPqrstTuvwy]</span><br><span class="line"> [-<行数>]</span><br><span class="line"> [-C <行数>]</span><br><span class="line"> [-I <字符或字符串>]</span><br><span class="line"> [-S <文件>]</span><br><span class="line"> [-W <宽度>]</span><br><span class="line"> [-x <文件或目录>]</span><br><span class="line"> [-X <文件>]</span><br><span class="line"> [文件或目录1]</span><br><span class="line"> [文件或目录2]</span><br></pre></td></tr></table></figure><ul><li>参数<ul><li>-<行数>: <code>指定比较行数 (必须与-c, -u一起使用)</code></li><li>-b 或–ignore-space-change: <code>不检查空格字符的不同。</code></li><li>-B 或 –ignore-blank-lines: <code>不检查空白行</code></li><li>-c: <code>显示全部内文,并标出不同</code></li><li>-u: <code>以合并的方式显示文件内容不同</code></li></ul></li></ul></li></ul><hr><h3 id="13-which"><a href="#13-which" class="headerlink" title="13. which"></a>13. which</h3><p><code>查找文件,which指令会在环境变量$PATH设置的目录里查找符合条件的文件。</code></p><ul><li>语法 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">which [文件...]</span><br></pre></td></tr></table></figure><ul><li>参数<ul><li>-n<文件名长度>: <code>指定文件名长度,指定的长度必须大于或等于所有文件中最长的文件名。</code> </li><li>-p<文件名长度>: <code>与-n参数相同,但此处的<文件名长度>包括了文件的路径。</code></li><li>-w: <code>指定输出时栏位的宽度。</code> </li><li>-V: <code>显示版本信息。</code></li></ul></li></ul></li></ul><hr><h3 id="14-ftp"><a href="#14-ftp" class="headerlink" title="14. ftp"></a>14. ftp</h3><p><code>设置文件系统相关功能,文件传输协议</code></p><ul><li>语法 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ftp [-dignv] [主机名称或IP地址]</span><br></pre></td></tr></table></figure><ul><li>参数<ul><li>-d: <code>详细显示指令执行过程,便于排错或分析程序执行的情形。</code></li><li>-i: <code>关闭互动模式,不询问任何问题。</code></li><li>-g: <code>关闭本地主机文件名称支持特殊字符的扩充特性。</code></li><li>-n: <code>不使用自动登陆。</code></li><li>-v: <code>显示指令执行过程。</code></li></ul></li></ul></li></ul><hr><h3 id="15-kill"><a href="#15-kill" class="headerlink" title="15. kill"></a>15. kill</h3><p><code>删除执行中的程序或工作</code></p><ul><li>语法 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">kill [-s <信息名称或编号>] [程序] 或 kill [-1 <信息编号>]</span><br></pre></td></tr></table></figure><ul><li>参数<ul><li>-l <信息编号>: <code>若不加<信息编号>选项,则-l参数会列出全部的信息名称。</code></li><li>-s <信息名称或编号>: <code>指定要送出的信息。</code> </li><li>【程序】: <code>[程序]可以是程序的PID或是PGID,也可以是工作编号</code></li></ul></li></ul></li></ul><hr><h3 id="16-sudo"><a href="#16-sudo" class="headerlink" title="16. sudo"></a>16. sudo</h3><p><code>以其他身份来执行命令,预设的身份为root</code></p><ul><li>语法 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sudo [-bhHpV] [-s <shell>] [-u <用户>] [指令] 或 sudo [-klv]</span><br></pre></td></tr></table></figure><ul><li>参数<ul><li>-b: <code>在后台执行指令。</code> </li><li>-h: <code>显示帮助</code> </li><li>-H: <code>将HOME环境变量设为新身份的HOME环境变量。</code> </li><li>-k: <code>结束密码的有效期限,也就是下次再执行sudo时便需要输入密码。</code> </li><li>-l: <code>列出目前用户可执行与无法执行的指令。</code> </li><li>-p: <code>改变询问密码的提示符号。</code> </li><li>-s<shell>: <code>执行指定的shell。</code> </shell></li><li>-u<用户>: <code>以指定的用户作为新的身份。若不加上此参数,则预设以root作为新的身份。</code></li><li>-v: <code>延长密码有效期限5分钟。</code> </li><li>-V: <code>显示版本信息。</code></li></ul></li></ul></li></ul><hr><h3 id="17-su"><a href="#17-su" class="headerlink" title="17. su"></a>17. su</h3><p><code>临时变更用户身份,必须输入账号密码</code></p><ul><li>语法 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">su [-flmp] [--help] [--version] [-] [-c <指令>] [-s <shell>][用户帐号]</span><br></pre></td></tr></table></figure><ul><li>参数<ul><li>-c<指令>或–command=<指令>: <code>执行完指定的指令后,即恢复原来的身份。</code></li><li>-f或–fast: <code>适用于csh与tsch,使shell不用去读取启动文件。</code> </li><li>-.-l或–login: <code>改变身份时,也同时变更工作目录,以及HOME,SHELL,USER,LOGNAME。此外,也会变更PATH变量。</code></li><li>-m,-p或–preserve-environment: <code>变更身份时,不要变更环境变量。</code> </li><li>-s<shell>或–shell=<shell>: <code>指定要执行的shell。</code></shell></shell></li><li>–help: <code>显示帮助。</code> </li><li>–version: <code>显示版本信息。</code> </li><li>【用户帐号】: <code>指定要变更的用户。若不指定此参数,则预设变更为root。</code></li></ul></li></ul></li></ul><hr><h3 id="18-who"><a href="#18-who" class="headerlink" title="18. who"></a>18. who</h3><p><code>显示目前登录系统的用户信息,目前有那些用户登入系统,单独执行who指令会列出登入帐号,使用的 终端机,登入时间以及从何处登入或正在使用哪个X显示器。</code></p><ul><li>语法 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">who [-Himqsw] [--help] [--version] [am i] [记录文件]</span><br></pre></td></tr></table></figure><ul><li>参数<ul><li>-H或–heading: <code>显示各栏位的标题信息列。</code> </li><li>-i或-u或–idle: <code>显示闲置时间,若该用户在前一分钟之内有进行任何动作,将标示成"."号,如果该用户已超过24小时没有任何动作,则标示出"old"字符串。</code></li><li>-m: <code>此参数的效果和指定"am i"字符串相同。显示自身用户名称</code></li><li>-q或–count: <code>只显示登入系统的帐号名称和总人数。</code></li><li>-s: <code>此参数将忽略不予处理,仅负责解决who指令其他版本的兼容性问题。</code> </li><li>-w或-T或–mesg或–message或–writable: <code>显示用户的信息状态栏。</code> </li><li>–help: <code>在线帮助。</code> </li><li>–version: <code>显示版本信息</code></li></ul></li></ul></li></ul><hr><h3 id="19-tree"><a href="#19-tree" class="headerlink" title="19. tree"></a>19. tree</h3><p><code>以树状图列出指定目录下的所有文件,包括子目录里的文件。</code></p><ul><li>语法 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">tree [-aACdDfFgilnNpqstux] [-I <范本样式>] [-P <范本样式>] [目录...]</span><br></pre></td></tr></table></figure><ul><li>参数<ul><li>-a: <code>显示所有文件和目录</code></li><li>-C: <code>在文件和目录清单加上色彩,便于区分各种类型。</code></li><li>-D: <code>列出文件或目录更改时间</code></li><li>-s: <code>列出文件或目录大小</code></li></ul></li></ul></li></ul><hr><h3 id="20-gzip"><a href="#20-gzip" class="headerlink" title="20. gzip"></a>20. gzip</h3><p><code>将文件压缩成 .gz 文件</code> </p><ul><li>语法 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">gzip [-acdfhlLnNqrtvV] [-S <压缩字尾字符串>] [-<压缩效率>] [--best/fast][文件...] 或 gzip [-acdfhlLnNqrtvV] [-S <压缩字尾字符串>] [-<压缩效率>] [--best/fast] [目录]</span><br></pre></td></tr></table></figure><ul><li>参数<ul><li>-d或–decompress或—-uncompress: <code>解开压缩文件。</code></li><li>-f或–force: <code>强行压缩文件。不理会文件名称或硬连接是否存在以及该文件是否为符号连接。</code></li><li>-l或–list: <code>列出压缩文件的相关信息。</code></li><li>-n或–no-name: <code>压缩文件时,不保存原来的文件名称及时间戳记。</code> </li><li>-N或–name: <code>压缩文件时,保存原来的文件名称及时间戳记。</code></li><li>-r或–recursive: <code>递归处理,将指定目录下的所有文件及子目录一并处理。</code></li><li>-<压缩效率>: <code>压缩效率是一个介于1-9的数值,预设值为"6",指定愈大的数值,压缩效率就会愈高</code></li><li>–best: <code>此参数的效果和指定"-9"参数相同。</code></li><li>–fast: <code>此参数的效果和指定"-1"参数相同。</code></li></ul></li></ul></li></ul><hr><h3 id="21-tar"><a href="#21-tar" class="headerlink" title="21. tar"></a>21. tar</h3><p><code>建立,还原备份文件的工具程序,它可以加入,解开备份文件内的文件。</code></p><ul><li>语法 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">tar [-ABcdgGhiklmMoOpPrRsStuUvwWxzZ]</span><br><span class="line"> [-b <区块数目>]</span><br><span class="line"> [-C <目的目录>]</span><br><span class="line"> [-f <备份文件>]</span><br><span class="line"> [-F <Script文件>]</span><br><span class="line"> [-K <文件>]</span><br><span class="line"> [-L <媒体容量>]</span><br><span class="line"> [-N <日期时间>]</span><br><span class="line"> [-T <范本文件>]</span><br><span class="line"> [-V <卷册名称>]</span><br><span class="line"> [-X <范本文件>]</span><br><span class="line"> [-<设备编号><存储密度>]</span><br><span class="line"> [--after-date=<日期时间>]</span><br><span class="line"> [--atime-preserve]</span><br><span class="line"> [--backuup=<备份方式>]</span><br><span class="line"> [--checkpoint]</span><br><span class="line"> [--concatenate]</span><br><span class="line"> [--confirmation]</span><br><span class="line"> [--delete]</span><br><span class="line"> [--exclude=<范本样式>]</span><br><span class="line"> ...</span><br><span class="line"> [文件或目录...]</span><br></pre></td></tr></table></figure><ul><li>参数<ul><li>-c或–create: <code>建立新的备份文件。</code></li><li>-f<备份文件>或–file=<备份文件>: <code>指定备份文件。</code></li><li>-v或–verbose: <code>显示指令执行过程。</code></li></ul></li></ul></li></ul><hr><h3 id="22-zip"><a href="#22-zip" class="headerlink" title="22. zip"></a>22. zip</h3><p><code>压缩后产生 .zip 压缩文件</code></p><ul><li>语法 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">zip [-AcdDfFghjJKlLmoqrSTuvVwXyz$]</span><br><span class="line"> [-b <工作目录>]</span><br><span class="line"> [-ll]</span><br><span class="line"> [-n <字尾字符串>]</span><br><span class="line"> [-t <日期时间>]</span><br><span class="line"> [-<压缩效率>]</span><br><span class="line"> [压缩文件]</span><br><span class="line"> [文件...]</span><br><span class="line"> [-i <范本样式>]</span><br><span class="line"> [-x <范本样式>]</span><br></pre></td></tr></table></figure><ul><li>参数<ul><li>-A: <code>调整可执行的自动解压缩文件</code></li><li>-b<工作目录>: <code>指定暂时存放文件的目录。</code></li><li>-F: <code>尝试修复已损坏的压缩文件。</code></li><li>-J: <code>删除压缩文件前面不必要的数据。</code></li><li>-m: <code>将文件压缩并加入压缩文件后,删除原始文件,即把文件移到压缩文件中。</code></li><li>-r: <code>递归处理,将指定目录下的所有文件和子目录一并处理。</code></li><li>-z: <code>替压缩文件加上注释。</code></li><li>-<压缩效率>: <code>压缩效率是一个介于1-9的数值。</code></li></ul></li></ul></li></ul><hr><h3 id="23-pstree"><a href="#23-pstree" class="headerlink" title="23. pstree"></a>23. pstree</h3><p><code>以树状图显示程序,清楚地表达程序间的相互关系。如果不指定程序识别码或用户名称,则会把系统启动时的第一个程序视为基层,并显示之后的所有程序。若指定用户名称,便会以隶属该用户的第一个程序当作基层,然后显示该用户的所有程序。</code></p><ul><li>语法 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">pstree [-acGhlnpuUV] [-H <程序识别码>] [<程序识别码>/<用户名称>]</span><br></pre></td></tr></table></figure><ul><li>参数<ul><li>-a: <code>显示每个程序的完整指令,包含路径,参数或是常驻服务的标示。</code> </li><li>-c: <code>不使用精简标示法。</code> </li><li>-G: <code>使用VT100终端机的列绘图字符。</code> </li><li>-h: <code>列出树状图时,特别标明现在执行的程序。</code> </li><li>-H<程序识别码>: <code>此参数的效果和指定"-h"参数类似,但特别标明指定的程序。</code> </li><li>-l: <code>采用长列格式显示树状图。</code> </li><li>-n: <code>用程序识别码排序。预设是以程序名称来排序。</code> </li><li>-p: <code>显示程序识别码。</code> </li><li>-u: <code>显示用户名称。</code></li><li>-U: <code>使用UTF-8列绘图字符。</code> </li><li>-V: <code>显示版本信息。</code></li></ul></li></ul></li></ul><hr><h3 id="24-ps"><a href="#24-ps" class="headerlink" title="24. ps"></a>24. ps</h3><p><code>报告程序执行状况的指令,可以搭配kill指令随时中断,删除不必要的程序。</code></p><ul><li>语法 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ps [-aAcdefHjlmNVwy] [acefghLnrsSTuvxX] ...</span><br></pre></td></tr></table></figure><ul><li>参数<ul><li>-a: <code>显示所有终端机下执行的程序,除了阶段作业领导者之外。</code></li><li>a: <code>显示现行终端机下的所有程序,包括其他用户的程序。</code></li><li>-A: <code>显示所有程序。</code></li><li>c: <code>列出程序时,显示每个程序真正的指令名称,而不包含路径,参数或常驻服务的标示。</code></li><li>e: <code>列出程序时,显示每个程序所使用的环境变量。</code></li><li>-H: <code>显示树状结构,表示程序间的相互关系。</code></li></ul></li></ul></li></ul>]]></content>
<tags>
<tag> Linux_study </tag>
</tags>
</entry>
<entry>
<title>css_note</title>
<link href="/2020/07/27/css-note/"/>
<url>/2020/07/27/css-note/</url>
<content type="html"><![CDATA[<h1 id="CSS-Study"><a href="#CSS-Study" class="headerlink" title="CSS Study"></a>CSS Study</h1><h2 id="一、CSS-初步"><a href="#一、CSS-初步" class="headerlink" title="一、CSS 初步"></a>一、CSS 初步</h2><hr><h3 id="添加-CSS"><a href="#添加-CSS" class="headerlink" title="添加 CSS"></a>添加 CSS</h3><p><code>把 styles.css 和 index.html 联结起来,可以在HTML文档中,<head>语句模块里面加上下面的代码:</code></p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><link rel="styesheet" href="styles.css></span><br></pre></td></tr></table></figure><hr><h2 id="二、构建-CSS-块"><a href="#二、构建-CSS-块" class="headerlink" title="二、构建 CSS 块"></a>二、构建 CSS 块</h2><hr><h3 id="层叠与继承"><a href="#层叠与继承" class="headerlink" title="层叠与继承"></a>层叠与继承</h3><h4 id="1-冲突规则"><a href="#1-冲突规则" class="headerlink" title="1. 冲突规则"></a>1. 冲突规则</h4><ol><li><h3 id="层叠"><a href="#层叠" class="headerlink" title="层叠"></a>层叠</h3> <code>Stylesheets cascade(样式表层叠) — 简单的说,css规则的顺序很重要;当应用两条同级别的规则到一个元素的时候,写在后面的就是实际使用的规则。</code></li><li><h3 id="优先级"><a href="#优先级" class="headerlink" title="优先级"></a>优先级</h3><ul><li><h4 id="一个元素选择器不是很具体-—-会选择页面上该类型的所有元素-—-所以它的优先级就会低一些。"><a href="#一个元素选择器不是很具体-—-会选择页面上该类型的所有元素-—-所以它的优先级就会低一些。" class="headerlink" title="一个元素选择器不是很具体 — 会选择页面上该类型的所有元素 — 所以它的优先级就会低一些。"></a><code>一个元素选择器不是很具体 — 会选择页面上该类型的所有元素 — 所以它的优先级就会低一些。</code></h4></li><li><h4 id="一个类选择器稍微具体点-—-它会选择该页面中有特定-class-属性值的元素-—-所以它的优先级就要高一点。"><a href="#一个类选择器稍微具体点-—-它会选择该页面中有特定-class-属性值的元素-—-所以它的优先级就要高一点。" class="headerlink" title="一个类选择器稍微具体点 — 它会选择该页面中有特定 class 属性值的元素 — 所以它的优先级就要高一点。"></a><code>一个类选择器稍微具体点 — 它会选择该页面中有特定 class 属性值的元素 — 所以它的优先级就要高一点。</code></h4></li></ul></li><li><h3 id="继承"><a href="#继承" class="headerlink" title="继承"></a>继承</h3> <code>如果设置一个元素的 color 和 font-family ,每个在里面的元素也都会有相同的属性,除非直接在元素上设置属性。</code><h4 id="2-理解继承"><a href="#2-理解继承" class="headerlink" title="2. 理解继承"></a>2. 理解继承</h4></li><li><h3 id="控制继承"><a href="#控制继承" class="headerlink" title="控制继承"></a>控制继承</h3> <code>CSS 为控制继承提供了四个特殊的通用属性值。每个css属性都接收这些值。</code><ul><li><h4 id="inherit设置该属性会使子元素属性和父元素相同。实际上,就是-quot-开启继承-quot"><a href="#inherit设置该属性会使子元素属性和父元素相同。实际上,就是-quot-开启继承-quot" class="headerlink" title="inherit设置该属性会使子元素属性和父元素相同。实际上,就是 "开启继承"."></a>inherit<code>设置该属性会使子元素属性和父元素相同。实际上,就是 "开启继承".</code></h4></li><li><h4 id="initial-设置属性值和浏览器默认样式相同。如果浏览器默认样式中未设置且该属性是自然继承的,那么会设置为-inherit-。"><a href="#initial-设置属性值和浏览器默认样式相同。如果浏览器默认样式中未设置且该属性是自然继承的,那么会设置为-inherit-。" class="headerlink" title="initial 设置属性值和浏览器默认样式相同。如果浏览器默认样式中未设置且该属性是自然继承的,那么会设置为 inherit 。"></a>initial <code>设置属性值和浏览器默认样式相同。如果浏览器默认样式中未设置且该属性是自然继承的,那么会设置为 inherit 。</code></h4></li><li><h4 id="unset-将属性重置为自然值,也就是如果属性是自然继承那么就是-inherit,否则和-initial一样"><a href="#unset-将属性重置为自然值,也就是如果属性是自然继承那么就是-inherit,否则和-initial一样" class="headerlink" title="unset 将属性重置为自然值,也就是如果属性是自然继承那么就是 inherit,否则和 initial一样"></a>unset <code>将属性重置为自然值,也就是如果属性是自然继承那么就是 inherit,否则和 initial一样</code></h4></li></ul></li><li><h3 id="重设所有属性值"><a href="#重设所有属性值" class="headerlink" title="重设所有属性值"></a>重设所有属性值</h3> <code>CSS 的 shorthand 属性 all 可以用于同时将这些继承值中的一个应用于(几乎)所有属性。它的值可以是其中任意一个(inherit, initial, unset, or revert)。这是一种撤销对样式所做更改的简便方法,以便回到之前已知的起点。</code></li></ol><hr><h3 id="CSS-选择器"><a href="#CSS-选择器" class="headerlink" title="CSS 选择器"></a>CSS 选择器</h3><h4 id="1-选择器列表"><a href="#1-选择器列表" class="headerlink" title="1. 选择器列表"></a>1. 选择器列表</h4><p><code>单独的选择器可以被混编为一个“选择器列表”,这样,规则就可以应用到所有的单个选择器上,如果任何一个选择器无效 (存在语法错误),那么整条规则都会被忽略。</code></p><pre><code><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">h1, .special { </span><br><span class="line"> color: blue; </span><br><span class="line">}</span><br></pre></td></tr></table></figure></code></pre><h4 id="2-选择器的种类"><a href="#2-选择器的种类" class="headerlink" title="2. 选择器的种类"></a>2. 选择器的种类</h4><ol><li><h3 id="类型、类、id选择器"><a href="#类型、类、id选择器" class="headerlink" title="类型、类、id选择器"></a>类型、类、id选择器</h3> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">h1 {} /*类型选择器*/</span><br><span class="line">.box {} /*类选择器*/</span><br><span class="line">#unique {} /*ID 选择器*/</span><br></pre></td></tr></table></figure></li><li><h3 id="标签属性选择器"><a href="#标签属性选择器" class="headerlink" title="标签属性选择器"></a>标签属性选择器</h3> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">a[title] { } /*根据一个元素上的某个标签的属性的存在以选择元素的不同方式*/</span><br><span class="line">a[href="https://example.com"] { } /*根据一个有特定值的标签属性是否存在来选择*/</span><br></pre></td></tr></table></figure></li><li><h3 id="伪类和伪元素"><a href="#伪类和伪元素" class="headerlink" title="伪类和伪元素"></a>伪类和伪元素</h3> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">/*包含了伪类,用来样式化一个元素的特定状态*/</span><br><span class="line">a:hover { } /*:hover伪类会在鼠标指针悬浮到一个元素上的时候选择这个元素:*/</span><br><span class="line">/*包含了伪元素,选择一个元素的某个部分而不是元素自己*/</span><br><span class="line">p::first-line { } /*::first-line是会选择一个元素(下面的情况中是<p>)中的第一行,类似<span>包在了第一个被格式化的行外面,然后选择这个<span>。*/</span><br></pre></td></tr></table></figure></li><li><h3 id="运算符"><a href="#运算符" class="headerlink" title="运算符"></a>运算符</h3> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">/*将其他选择器组合起来,更复杂的选择元素*/</span><br><span class="line">article > p { } /*用运算符(>)选择了<article>元素的初代子元素。*/</span><br></pre></td></tr></table></figure></li></ol><hr><h3 id="盒模型"><a href="#盒模型" class="headerlink" title="盒模型"></a>盒模型</h3><h4 id="1-块级盒子(Block-box)-和-内联盒子(Inline-box)"><a href="#1-块级盒子(Block-box)-和-内联盒子(Inline-box)" class="headerlink" title="1. 块级盒子(Block box) 和 内联盒子(Inline box)"></a>1. 块级盒子(Block box) 和 内联盒子(Inline box)</h4><ol><li><h3 id="块级盒子(Block-box)标题-lt-h1-gt-等-和段落-lt-p-gt-默认情况下都是块级的盒子。"><a href="#块级盒子(Block-box)标题-lt-h1-gt-等-和段落-lt-p-gt-默认情况下都是块级的盒子。" class="headerlink" title="块级盒子(Block box)标题(<h1>等)和段落(<p>)默认情况下都是块级的盒子。"></a>块级盒子(Block box)<code>标题(<h1>等)和段落(<p>)默认情况下都是块级的盒子。</code></h3><ul><li><h4 id="盒子会在内联的方向上扩展并占据父容器在该方向上的所有可用空间,在绝大数情况下意味着盒子会和父容器一样宽"><a href="#盒子会在内联的方向上扩展并占据父容器在该方向上的所有可用空间,在绝大数情况下意味着盒子会和父容器一样宽" class="headerlink" title="盒子会在内联的方向上扩展并占据父容器在该方向上的所有可用空间,在绝大数情况下意味着盒子会和父容器一样宽"></a><code>盒子会在内联的方向上扩展并占据父容器在该方向上的所有可用空间,在绝大数情况下意味着盒子会和父容器一样宽</code></h4></li><li><h4 id="每个盒子都会换行"><a href="#每个盒子都会换行" class="headerlink" title="每个盒子都会换行"></a><code>每个盒子都会换行</code></h4></li><li><h4 id="width-和-height-属性可以发挥作用"><a href="#width-和-height-属性可以发挥作用" class="headerlink" title="width 和 height 属性可以发挥作用"></a><code>width 和 height 属性可以发挥作用</code></h4></li><li><h4 id="内边距(padding)-外边距(margin)-和-边框(border)-会将其他元素从当前盒子周围“推开”"><a href="#内边距(padding)-外边距(margin)-和-边框(border)-会将其他元素从当前盒子周围“推开”" class="headerlink" title="内边距(padding), 外边距(margin) 和 边框(border) 会将其他元素从当前盒子周围“推开”"></a><code>内边距(padding), 外边距(margin) 和 边框(border) 会将其他元素从当前盒子周围“推开”</code></h4></li></ul></li><li><h3 id="内联盒子(Inline-box)-用做链接的-lt-a-gt-元素、-lt-span-gt-、-lt-em-gt-以及-lt-strong-gt-都是默认处于-inline-状态"><a href="#内联盒子(Inline-box)-用做链接的-lt-a-gt-元素、-lt-span-gt-、-lt-em-gt-以及-lt-strong-gt-都是默认处于-inline-状态" class="headerlink" title="内联盒子(Inline box) 用做链接的 <a> 元素、 <span>、 <em> 以及 <strong> 都是默认处于 inline 状态"></a>内联盒子(Inline box) <code>用做链接的 <a> 元素、 <span>、 <em> 以及 <strong> 都是默认处于 inline 状态</code></h3><ul><li><h4 id="盒子不会产生换行。"><a href="#盒子不会产生换行。" class="headerlink" title="盒子不会产生换行。"></a><code>盒子不会产生换行。</code></h4></li><li><h4 id="width-和-height-属性将不起作用。"><a href="#width-和-height-属性将不起作用。" class="headerlink" title="width 和 height 属性将不起作用。"></a><code>width 和 height 属性将不起作用。</code></h4></li><li><h4 id="垂直方向的内边距、外边距以及边框会被应用但是不会把其他处于-inline-状态的盒子推开。"><a href="#垂直方向的内边距、外边距以及边框会被应用但是不会把其他处于-inline-状态的盒子推开。" class="headerlink" title="垂直方向的内边距、外边距以及边框会被应用但是不会把其他处于 inline 状态的盒子推开。"></a><code>垂直方向的内边距、外边距以及边框会被应用但是不会把其他处于 inline 状态的盒子推开。</code></h4></li><li><h4 id="水平方向的内边距、外边距以及边框会被应用而且也会把其他处于-inline-状态的盒子推开。"><a href="#水平方向的内边距、外边距以及边框会被应用而且也会把其他处于-inline-状态的盒子推开。" class="headerlink" title="水平方向的内边距、外边距以及边框会被应用而且也会把其他处于 inline 状态的盒子推开。"></a><code>水平方向的内边距、外边距以及边框会被应用而且也会把其他处于 inline 状态的盒子推开。</code></h4><h4 id="2-CSS-盒模型"><a href="#2-CSS-盒模型" class="headerlink" title="2. CSS 盒模型"></a>2. CSS 盒模型</h4><code>完整的 CSS 盒模型应用于块级盒子,内联盒子只使用盒模型中定义的部分内容。模型定义了盒的每个部分 —— margin, border, padding, and content</code></li></ul></li><li><h3 id="盒模型的各个部分"><a href="#盒模型的各个部分" class="headerlink" title="盒模型的各个部分"></a>盒模型的各个部分</h3><p> <img src="/2020/07/27/css-note/box-model.png" alt="BOX"></p><ul><li><h4 id="Content-box-这个区域是用来显示内容,大小可以通过设置-width-和-height"><a href="#Content-box-这个区域是用来显示内容,大小可以通过设置-width-和-height" class="headerlink" title="Content box: 这个区域是用来显示内容,大小可以通过设置 width 和 height."></a>Content box: <code>这个区域是用来显示内容,大小可以通过设置 width 和 height.</code></h4></li><li><h4 id="Padding-box-包围在内容区域外部的空白区域;-大小通过-padding-相关属性设置。"><a href="#Padding-box-包围在内容区域外部的空白区域;-大小通过-padding-相关属性设置。" class="headerlink" title="Padding box: 包围在内容区域外部的空白区域; 大小通过 padding 相关属性设置。"></a>Padding box: <code>包围在内容区域外部的空白区域; 大小通过 padding 相关属性设置</code>。</h4></li><li><h4 id="Border-box-边框盒包裹内容和内边距。大小通过-border-相关属性设置。"><a href="#Border-box-边框盒包裹内容和内边距。大小通过-border-相关属性设置。" class="headerlink" title="Border box: 边框盒包裹内容和内边距。大小通过 border 相关属性设置。"></a>Border box: <code>边框盒包裹内容和内边距。大小通过 border 相关属性设置。</code></h4></li><li><h4 id="Margin-box-这是最外面的区域,是盒子和其他元素之间的空白区域。大小通过-margin-相关属性设置。"><a href="#Margin-box-这是最外面的区域,是盒子和其他元素之间的空白区域。大小通过-margin-相关属性设置。" class="headerlink" title="Margin box: 这是最外面的区域,是盒子和其他元素之间的空白区域。大小通过 margin 相关属性设置。"></a>Margin box: <code>这是最外面的区域,是盒子和其他元素之间的空白区域。大小通过 margin 相关属性设置。</code></h4></li></ul></li><li><h3 id="标准盒模型"><a href="#标准盒模型" class="headerlink" title="标准盒模型"></a>标准盒模型</h3><p> <code>在标准模型 (标准模型宽度 = 410px (350 + 25 + 25 + 5 + 5),高度 = 210px (150 + 25 + 25 + 5 + 5),padding 加 border 再加 content box) 中,如果你给盒设置 width 和 height,实际设置的是 content box。 padding 和 border 再加上设置的宽高一起决定整个盒子的大小。</code></p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">.box {</span><br><span class="line"> width: 350px;</span><br><span class="line"> height: 150px;</span><br><span class="line"> margin: 25px;</span><br><span class="line"> padding: 25px;</span><br><span class="line"> border: 5px solid black;</span><br><span class="line"> }</span><br></pre></td></tr></table></figure></li><li><h3 id="替代(IE)盒模型"><a href="#替代(IE)盒模型" class="headerlink" title="替代(IE)盒模型"></a>替代(IE)盒模型</h3><p> <code>css还有一个替代盒模型。使用这个模型,所有宽度都是可见宽度,所以内容宽度是该宽度减去边框和填充部分。使用替代模型,通过为其设置 box-sizing: border-box 来实现。</code></p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">.box { </span><br><span class="line"> box-sizing: border-box; </span><br><span class="line">}</span><br></pre></td></tr></table></figure></li><li><h3 id="使用调试工具来查看盒模型-F12"><a href="#使用调试工具来查看盒模型-F12" class="headerlink" title="使用调试工具来查看盒模型 (F12)"></a>使用调试工具来查看盒模型 (F12)</h3><h4 id="3-外边距,内边距,边框-详细"><a href="#3-外边距,内边距,边框-详细" class="headerlink" title="3. 外边距,内边距,边框 (详细)"></a>3. 外边距,内边距,边框 (详细)</h4></li><li><h3 id="外边距"><a href="#外边距" class="headerlink" title="外边距"></a>外边距</h3><p> <code>外边距属性值可以为正也可以为负。设置负值会导致和其他内容重叠。无论使用标准模型还是替代模型,外边距总是在计算可见部分后额外添加。</code></p><ol><li><h4 id="使用margin属性一次控制一个元素的所有边距,或者"><a href="#使用margin属性一次控制一个元素的所有边距,或者" class="headerlink" title="使用margin属性一次控制一个元素的所有边距,或者"></a>使用margin属性一次控制一个元素的所有边距,或者</h4></li><li><h4 id="每边单独使用等价的普通属性控制:"><a href="#每边单独使用等价的普通属性控制:" class="headerlink" title="每边单独使用等价的普通属性控制:"></a>每边单独使用等价的普通属性控制:</h4><ul><li><h4 id="margin"><a href="#margin" class="headerlink" title="margin"></a>margin</h4></li><li><h4 id="margin-top"><a href="#margin-top" class="headerlink" title="margin-top"></a>margin-top</h4></li><li><h4 id="margin-right"><a href="#margin-right" class="headerlink" title="margin-right"></a>margin-right</h4></li><li><h4 id="margin-bottom"><a href="#margin-bottom" class="headerlink" title="margin-bottom"></a>margin-bottom</h4></li><li><h4 id="margin-left"><a href="#margin-left" class="headerlink" title="margin-left"></a>margin-left</h4><h2 id="参数"><a href="#参数" class="headerlink" title="参数"></a>参数</h2></li></ul></li></ol><ul><li><h4 id="lt-length-gt-指定一个固定的宽度。可以为负数。"><a href="#lt-length-gt-指定一个固定的宽度。可以为负数。" class="headerlink" title="<length>:指定一个固定的宽度。可以为负数。"></a><code><length>:指定一个固定的宽度。可以为负数。</code></h4></li><li><h4 id="lt-percentage-gt-相对于该元素的包含块的宽度(相对于该块的百分比)。该值可以为负数。"><a href="#lt-percentage-gt-相对于该元素的包含块的宽度(相对于该块的百分比)。该值可以为负数。" class="headerlink" title="<percentage> 相对于该元素的包含块的宽度(相对于该块的百分比)。该值可以为负数。"></a><code><percentage> 相对于该元素的包含块的宽度(相对于该块的百分比)。该值可以为负数。</code></h4></li><li><h4 id="margin-auto-该盒子是水平居中的-上下边距为0"><a href="#margin-auto-该盒子是水平居中的-上下边距为0" class="headerlink" title="margin: auto; /* 该盒子是水平居中的, 上下边距为0 */"></a>margin: auto; /* 该盒子是水平居中的, 上下边距为0 */</h4> <code>浏览器会自动选择一个合适的margin来应用。它可以用于将一个块居中。比如,div { width:50%; margin:0 auto; } 会把这个div容器水平居中。</code><ul><li><h5 id="只有一个-值时,这个值会被指定给全部的-四个边"><a href="#只有一个-值时,这个值会被指定给全部的-四个边" class="headerlink" title="只有一个 值时,这个值会被指定给全部的 四个边."></a><code>只有一个 值时,这个值会被指定给全部的 四个边.</code></h5></li><li><h5 id="两个-值时,第一个值被匹配给-上和下-第二个值被匹配给-左和右"><a href="#两个-值时,第一个值被匹配给-上和下-第二个值被匹配给-左和右" class="headerlink" title="两个 值时,第一个值被匹配给 上和下, 第二个值被匹配给 左和右."></a><code>两个 值时,第一个值被匹配给 上和下, 第二个值被匹配给 左和右.</code></h5></li><li><h5 id="三个-值时,第一个值被匹配给-上-第二个值被匹配给-左和右-第三个值被匹配给-下"><a href="#三个-值时,第一个值被匹配给-上-第二个值被匹配给-左和右-第三个值被匹配给-下" class="headerlink" title="三个 值时,第一个值被匹配给 上, 第二个值被匹配给 左和右, 第三个值被匹配给 下."></a><code>三个 值时,第一个值被匹配给 上, 第二个值被匹配给 左和右, 第三个值被匹配给 下.</code></h5></li><li><h5 id="四个-值时,会依次按-上、右、下、左-的顺序匹配-即顺时针顺序"><a href="#四个-值时,会依次按-上、右、下、左-的顺序匹配-即顺时针顺序" class="headerlink" title="四个 值时,会依次按 上、右、下、左 的顺序匹配 (即顺时针顺序)."></a><code>四个 值时,会依次按 上、右、下、左 的顺序匹配 (即顺时针顺序).</code></h5></li></ul></li></ul></li><li><h3 id="边框"><a href="#边框" class="headerlink" title="边框"></a>边框</h3><p> <code>边框是在边距和填充框之间绘制的。标准的盒模型,边框的大小将添加到框的宽度和高度。替代盒模型,那么边框的大小会使内容框更小,因为它会占用一些可用的宽度和高度。有四个边框,每个边框都有样式、宽度和颜色,我们可能需要对它们进行操作。</code></p><ol><li><h4 id="border属性一次设置所有四个边框的宽度、颜色和样式。"><a href="#border属性一次设置所有四个边框的宽度、颜色和样式。" class="headerlink" title="border属性一次设置所有四个边框的宽度、颜色和样式。"></a><code>border属性一次设置所有四个边框的宽度、颜色和样式。</code></h4> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">.box { </span><br><span class="line"> border: 1px solid black; </span><br><span class="line">}</span><br></pre></td></tr></table></figure> <strong>等价于</strong> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">.box { </span><br><span class="line"> border-width: 1px; </span><br><span class="line"> border-style: solid; </span><br><span class="line"> border-color: black; </span><br><span class="line">}</span><br></pre></td></tr></table></figure></li><li><h4 id="分别设置每边的宽度、颜色和样式,可以使用:"><a href="#分别设置每边的宽度、颜色和样式,可以使用:" class="headerlink" title="分别设置每边的宽度、颜色和样式,可以使用:"></a><code>分别设置每边的宽度、颜色和样式,可以使用:</code></h4><ul><li><h5 id="border-top"><a href="#border-top" class="headerlink" title="border-top"></a>border-top</h5></li><li><h5 id="border-right"><a href="#border-right" class="headerlink" title="border-right"></a>border-right</h5></li><li><h5 id="border-bottom"><a href="#border-bottom" class="headerlink" title="border-bottom"></a>border-bottom</h5></li><li><h5 id="border-left"><a href="#border-left" class="headerlink" title="border-left"></a>border-left</h5></li></ul></li><li><h4 id="设置所有边的颜色、样式或宽度,请使用以下属性:"><a href="#设置所有边的颜色、样式或宽度,请使用以下属性:" class="headerlink" title="设置所有边的颜色、样式或宽度,请使用以下属性:"></a><code>设置所有边的颜色、样式或宽度,请使用以下属性:</code></h4><ul><li><h5 id="border-width"><a href="#border-width" class="headerlink" title="border-width"></a>border-width</h5></li><li><h5 id="border-style"><a href="#border-style" class="headerlink" title="border-style"></a>border-style</h5></li><li><h5 id="border-color"><a href="#border-color" class="headerlink" title="border-color"></a>border-color</h5></li></ul></li><li><h4 id="设置单边的颜色、样式或宽度,可以使用最细粒度的普通属性之一"><a href="#设置单边的颜色、样式或宽度,可以使用最细粒度的普通属性之一" class="headerlink" title="设置单边的颜色、样式或宽度,可以使用最细粒度的普通属性之一"></a><code>设置单边的颜色、样式或宽度,可以使用最细粒度的普通属性之一</code></h4><ul><li><h5 id="border-top-width"><a href="#border-top-width" class="headerlink" title="border-top-width"></a>border-top-width</h5></li><li><h5 id="border-top-style"><a href="#border-top-style" class="headerlink" title="border-top-style"></a>border-top-style</h5></li><li><h5 id="border-top-color"><a href="#border-top-color" class="headerlink" title="border-top-color"></a>border-top-color</h5></li><li><h5 id="border-right-width"><a href="#border-right-width" class="headerlink" title="border-right-width"></a>border-right-width</h5></li><li><h5 id="border-right-style"><a href="#border-right-style" class="headerlink" title="border-right-style"></a>border-right-style</h5></li><li><h5 id="border-right-color"><a href="#border-right-color" class="headerlink" title="border-right-color"></a>border-right-color</h5></li><li><h5 id="border-bottom-width"><a href="#border-bottom-width" class="headerlink" title="border-bottom-width"></a>border-bottom-width</h5></li><li><h5 id="border-bottom-style"><a href="#border-bottom-style" class="headerlink" title="border-bottom-style"></a>border-bottom-style</h5></li><li><h5 id="border-bottom-color"><a href="#border-bottom-color" class="headerlink" title="border-bottom-color"></a>border-bottom-color</h5></li><li><h5 id="border-left-width"><a href="#border-left-width" class="headerlink" title="border-left-width"></a>border-left-width</h5></li><li><h5 id="border-left-style"><a href="#border-left-style" class="headerlink" title="border-left-style"></a>border-left-style</h5></li><li><h5 id="border-left-color"><a href="#border-left-color" class="headerlink" title="border-left-color"></a>border-left-color</h5></li></ul></li><li><h4 id="圆角-border-radius"><a href="#圆角-border-radius" class="headerlink" title="圆角 border-radius"></a>圆角 border-radius</h4> <code>border-radius属性和与方框的每个角相关的长边来实现方框的圆角。可以使用两个长度或百分比作为值,第一个值定义水平半径,第二个值定义垂直半径。在很多情况下,您将只传递一个值,这两个值都将使用。</code><ul><li><h5 id="使一个盒子的四个角都有10px的圆角半径:"><a href="#使一个盒子的四个角都有10px的圆角半径:" class="headerlink" title="使一个盒子的四个角都有10px的圆角半径:"></a><code>使一个盒子的四个角都有10px的圆角半径:</code></h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">.box { </span><br><span class="line"> border-radius: 10px; </span><br><span class="line">}</span><br></pre></td></tr></table></figure></li><li><h5 id="右上角的水平半径为1em,垂直半径为10%:"><a href="#右上角的水平半径为1em,垂直半径为10%:" class="headerlink" title="右上角的水平半径为1em,垂直半径为10%:"></a><code>右上角的水平半径为1em,垂直半径为10%:</code></h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">.box { </span><br><span class="line"> border-top-right-radius: 1em 10%; </span><br><span class="line">}</span><br></pre></td></tr></table></figure></li><li><h5 id="由方形变为圆"><a href="#由方形变为圆" class="headerlink" title="由方形变为圆"></a><code>由方形变为圆</code></h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">.box{</span><br><span class="line"> border-radius: 50%;</span><br><span class="line">}</span><br></pre></td></tr></table></figure></li></ul></li></ol></li><li><h3 id="内边距"><a href="#内边距" class="headerlink" title="内边距"></a>内边距</h3><p> <code>内边距位于边框和内容区域之间。与外边距不同,不能有负数量的内边距,所以值必须是0或正的值。应用于元素的任何背景都将显示在内边距后面,内边距通常用于将内容推离边框。</code></p><ol><li><h4 id="使用padding简写属性控制元素所有边"><a href="#使用padding简写属性控制元素所有边" class="headerlink" title="使用padding简写属性控制元素所有边"></a><code>使用padding简写属性控制元素所有边</code></h4></li><li><h4 id="每边单独使用等价的普通属性:"><a href="#每边单独使用等价的普通属性:" class="headerlink" title="每边单独使用等价的普通属性:"></a><code>每边单独使用等价的普通属性:</code></h4><ul><li><h5 id="padding-top"><a href="#padding-top" class="headerlink" title="padding-top"></a>padding-top</h5></li><li><h5 id="padding-right"><a href="#padding-right" class="headerlink" title="padding-right"></a>padding-right</h5></li><li><h5 id="padding-bottom"><a href="#padding-bottom" class="headerlink" title="padding-bottom"></a>padding-bottom</h5></li><li><h5 id="padding-left"><a href="#padding-left" class="headerlink" title="padding-left"></a>padding-left</h5><h4 id="4-使用display-inline-block"><a href="#4-使用display-inline-block" class="headerlink" title="4. 使用display: inline-block"></a>4. 使用display: inline-block</h4><code>display有一个特殊的值,它在内联和块之间提供了一个中间状态。这对于以下情况非常有用:不希望一个项切换到新行,但希望它可以设定宽度和高度,并避免上面看到的重叠。</code></li></ul></li></ol></li></ol><ul><li><h4 id="一个元素使用-display-inline-block,实现我们需要的块级的部分效果:"><a href="#一个元素使用-display-inline-block,实现我们需要的块级的部分效果:" class="headerlink" title="一个元素使用 display: inline-block,实现我们需要的块级的部分效果:"></a><code>一个元素使用 display: inline-block,实现我们需要的块级的部分效果:</code></h4><ul><li><h5 id="设置width-和height-属性会生效。"><a href="#设置width-和height-属性会生效。" class="headerlink" title="设置width 和height 属性会生效。"></a>设置width 和height 属性会生效。</h5></li><li><h5 id="padding-margin-以及border-会推开其他元素。"><a href="#padding-margin-以及border-会推开其他元素。" class="headerlink" title="padding, margin, 以及border 会推开其他元素。"></a>padding, margin, 以及border 会推开其他元素。</h5></li></ul></li></ul><hr><h3 id="背景与边框"><a href="#背景与边框" class="headerlink" title="背景与边框"></a>背景与边框</h3><h4 id="1-CSS-的背景样式"><a href="#1-CSS-的背景样式" class="headerlink" title="1. CSS 的背景样式"></a>1. CSS 的背景样式</h4><ol><li><h3 id="背景颜色-background-color"><a href="#背景颜色-background-color" class="headerlink" title="背景颜色 background-color"></a>背景颜色 background-color</h3> <code>background-color属性定义了CSS中任何元素的背景颜色。属性接受任何有效的<color>值。背景色扩展到元素的内容和内边距的下面。</code></li><li><h3 id="背景图片-background-image"><a href="#背景图片-background-image" class="headerlink" title="背景图片 background-image"></a>背景图片 background-image</h3> <code>background-image属性允许在元素的背景中显示图像。</code><ul><li><h4 id="控制背景平铺-background-repeat"><a href="#控制背景平铺-background-repeat" class="headerlink" title="控制背景平铺 background-repeat"></a>控制背景平铺 background-repeat</h4> <code>background-repeat属性用于控制图像的平铺行为。可用的值是:</code><ul><li><h5 id="no-repeat-—-不重复。"><a href="#no-repeat-—-不重复。" class="headerlink" title="no-repeat — 不重复。"></a>no-repeat — 不重复。</h5></li><li><h5 id="repeat-x-—水平重复。"><a href="#repeat-x-—水平重复。" class="headerlink" title="repeat-x —水平重复。"></a>repeat-x —水平重复。</h5></li><li><h5 id="repeat-y-—垂直重复。"><a href="#repeat-y-—垂直重复。" class="headerlink" title="repeat-y —垂直重复。"></a>repeat-y —垂直重复。</h5></li><li><h5 id="repeat-—-在两个方向重复。"><a href="#repeat-—-在两个方向重复。" class="headerlink" title="repeat — 在两个方向重复。"></a>repeat — 在两个方向重复。</h5></li></ul></li><li><h4 id="调整背景图像的大小-background-size"><a href="#调整背景图像的大小-background-size" class="headerlink" title="调整背景图像的大小 background-size"></a>调整背景图像的大小 background-size</h4> <code>background-size属性,它可以设置长度或百分比值,来调整图像的大小以适应背景。也可以使用关键字:</code><ul><li><h5 id="cover-—浏览器将使图像足够大,使它完全覆盖了盒子区,同时仍然保持其高宽比。在这种情况下,有些图像可能会跳出盒子外"><a href="#cover-—浏览器将使图像足够大,使它完全覆盖了盒子区,同时仍然保持其高宽比。在这种情况下,有些图像可能会跳出盒子外" class="headerlink" title="cover —浏览器将使图像足够大,使它完全覆盖了盒子区,同时仍然保持其高宽比。在这种情况下,有些图像可能会跳出盒子外"></a>cover —<code>浏览器将使图像足够大,使它完全覆盖了盒子区,同时仍然保持其高宽比。在这种情况下,有些图像可能会跳出盒子外</code></h5></li><li><h5 id="contain-—-浏览器将使图像的大小适合盒子内。在这种情况下,如果图像的长宽比与盒子的长宽比不同,则可能在图像的任何一边或顶部和底部出现间隙。"><a href="#contain-—-浏览器将使图像的大小适合盒子内。在这种情况下,如果图像的长宽比与盒子的长宽比不同,则可能在图像的任何一边或顶部和底部出现间隙。" class="headerlink" title="contain — 浏览器将使图像的大小适合盒子内。在这种情况下,如果图像的长宽比与盒子的长宽比不同,则可能在图像的任何一边或顶部和底部出现间隙。"></a>contain — <code>浏览器将使图像的大小适合盒子内。在这种情况下,如果图像的长宽比与盒子的长宽比不同,则可能在图像的任何一边或顶部和底部出现间隙。</code></h5></li></ul></li><li><h4 id="背景图像定位-background-position"><a href="#背景图像定位-background-position" class="headerlink" title="背景图像定位 background-position"></a>背景图像定位 background-position</h4> <code>background-position属性允许您选择背景图像显示在其应用到的盒子中的位置。它使用的坐标系中,框的左上角是(0,0) (默认),框沿着水平(x)和垂直(y)轴定位。</code><ul><li><h5 id="常见的背景位置值有两个单独的值——一个水平值后面跟着一个垂直值。"><a href="#常见的背景位置值有两个单独的值——一个水平值后面跟着一个垂直值。" class="headerlink" title="常见的背景位置值有两个单独的值——一个水平值后面跟着一个垂直值。`"></a>常见的背景位置值有两个单独的值——一个水平值后面跟着一个垂直值。`</h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">.box { </span><br><span class="line"> background-image: url(star.png); </span><br><span class="line"> background-repeat: no-repeat; </span><br><span class="line"> background-position: top center; </span><br><span class="line">}</span><br></pre></td></tr></table></figure></li><li><h5 id="或者使用-长度值-and-百分比:"><a href="#或者使用-长度值-and-百分比:" class="headerlink" title="或者使用 长度值, and 百分比:"></a><code>或者使用 长度值, and 百分比:</code></h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">.box { </span><br><span class="line"> background-image: url(star.png); </span><br><span class="line"> background-repeat: no-repeat; </span><br><span class="line"> background-position: 20px 10%; </span><br><span class="line">}</span><br></pre></td></tr></table></figure></li><li><h5 id="混合使用关键字,长度值以及百分比"><a href="#混合使用关键字,长度值以及百分比" class="headerlink" title="混合使用关键字,长度值以及百分比"></a><code>混合使用关键字,长度值以及百分比</code></h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">.box {</span><br><span class="line"> background-image: url(star.png);</span><br><span class="line"> background-repeat: no-repeat;</span><br><span class="line"> background-position: top 20px;</span><br><span class="line">}</span><br></pre></td></tr></table></figure></li><li><h5 id="4-value语法来指示到盒子的某些边的距离"><a href="#4-value语法来指示到盒子的某些边的距离" class="headerlink" title="4-value语法来指示到盒子的某些边的距离"></a><code>4-value语法来指示到盒子的某些边的距离</code></h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">.box { </span><br><span class="line"> background-image: url(star.png); </span><br><span class="line"> background-repeat: no-repeat; </span><br><span class="line"> background-position: top 20px right 10px; </span><br><span class="line">}</span><br></pre></td></tr></table></figure></li></ul></li><li><h4 id="渐变背景"><a href="#渐变背景" class="headerlink" title="渐变背景"></a>渐变背景</h4> <code>使用background-image属性设置。</code></li><li><h4 id="多背景图像"><a href="#多背景图像" class="headerlink" title="多背景图像"></a>多背景图像</h4> <code>在单个属性值中指定多个background-image值,用逗号分隔每个值,会发生图像重叠.其他 background-*属性也可以有值逗号分隔的方式休市对应的的background-image:</code> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">background-image: url(image1.png), url(image2.png), url(image3.png), url(image1.png);</span><br><span class="line">background-repeat: no-repeat, repeat-x, repeat;</span><br></pre></td></tr></table></figure></li><li><h4 id="背景附加"><a href="#背景附加" class="headerlink" title="背景附加"></a>背景附加</h4> <code>指定如何滚动时,内容滚动。这是由background-attachment属性控制的,它可以接受以下值:</code><ul><li><h5 id="scroll-使元素的背景在页面滚动时滚动。如果滚动了元素内容,则背景不会移动。实际上,背景被固定在页面的相同位置,所以它会随着页面的滚动而滚动。"><a href="#scroll-使元素的背景在页面滚动时滚动。如果滚动了元素内容,则背景不会移动。实际上,背景被固定在页面的相同位置,所以它会随着页面的滚动而滚动。" class="headerlink" title="scroll:使元素的背景在页面滚动时滚动。如果滚动了元素内容,则背景不会移动。实际上,背景被固定在页面的相同位置,所以它会随着页面的滚动而滚动。"></a>scroll:<code>使元素的背景在页面滚动时滚动。如果滚动了元素内容,则背景不会移动。实际上,背景被固定在页面的相同位置,所以它会随着页面的滚动而滚动。</code></h5></li><li><h5 id="fixed-使元素的背景固定在视图端口上,这样当页面或元素内容滚动时,它就不会滚动。它将始终保持在屏幕上相同的位置。"><a href="#fixed-使元素的背景固定在视图端口上,这样当页面或元素内容滚动时,它就不会滚动。它将始终保持在屏幕上相同的位置。" class="headerlink" title="fixed: 使元素的背景固定在视图端口上,这样当页面或元素内容滚动时,它就不会滚动。它将始终保持在屏幕上相同的位置。"></a>fixed: <code>使元素的背景固定在视图端口上,这样当页面或元素内容滚动时,它就不会滚动。它将始终保持在屏幕上相同的位置。</code></h5></li><li><h5 id="local-这个值是后来添加的-它只在Internet-Explorer-9-中受支持,而其他的在IE4-中受支持-,因为滚动值相当混乱,在很多情况下并不能真正实现您想要的功能。局部值将背景固定在设置的元素上,因此当您滚动元素时,背景也随之滚动。"><a href="#local-这个值是后来添加的-它只在Internet-Explorer-9-中受支持,而其他的在IE4-中受支持-,因为滚动值相当混乱,在很多情况下并不能真正实现您想要的功能。局部值将背景固定在设置的元素上,因此当您滚动元素时,背景也随之滚动。" class="headerlink" title="local: 这个值是后来添加的(它只在Internet Explorer 9+中受支持,而其他的在IE4+中受支持),因为滚动值相当混乱,在很多情况下并不能真正实现您想要的功能。局部值将背景固定在设置的元素上,因此当您滚动元素时,背景也随之滚动。"></a>local: <code>这个值是后来添加的(它只在Internet Explorer 9+中受支持,而其他的在IE4+中受支持),因为滚动值相当混乱,在很多情况下并不能真正实现您想要的功能。局部值将背景固定在设置的元素上,因此当您滚动元素时,背景也随之滚动。</code></h5></li></ul></li></ul></li></ol><hr><h3 id="CSS-的值与单位"><a href="#CSS-的值与单位" class="headerlink" title="CSS 的值与单位"></a>CSS 的值与单位</h3><ol><li><h4 id="数字,长度和百分比"><a href="#数字,长度和百分比" class="headerlink" title="数字,长度和百分比"></a>数字,长度和百分比</h4><table><thead><tr><th align="center">数值类型</th><th align="center">描述</th></tr></thead><tbody><tr><td align="center">integer</td><td align="center">整数</td></tr><tr><td align="center">number</td><td align="center">表示一个小数——它可能有小数点后面的部分,也可能没有</td></tr><tr><td align="center">dimension</td><td align="center">有一个附加的单位,例如45deg、5s或10px。<dimension>是一个伞形类别,包括<length>、<angle>、<time>和<resolution>类型。</resolution></time></angle></length></dimension></td></tr><tr><td align="center">percentage</td><td align="center">表示一些其他值的一部分,例如50%。百分比值总是相对于另一个量,例如,一个元素的长度相对于其父元素的长度。</td></tr></tbody></table></li><li><h4 id="绝对长度"><a href="#绝对长度" class="headerlink" title="绝对长度"></a>绝对长度</h4><table><thead><tr><th align="center">单位</th><th align="center">名称</th></tr></thead><tbody><tr><td align="center">cm</td><td align="center">厘米</td></tr><tr><td align="center">mm</td><td align="center">毫米</td></tr><tr><td align="center">Q</td><td align="center">四分之一毫米</td></tr><tr><td align="center">in</td><td align="center">英寸</td></tr><tr><td align="center">pc</td><td align="center">十二点活字</td></tr><tr><td align="center">pt</td><td align="center">点</td></tr><tr><td align="center">px</td><td align="center">像素</td></tr></tbody></table></li><li><h4 id="相对长度与单位"><a href="#相对长度与单位" class="headerlink" title="相对长度与单位"></a>相对长度与单位</h4><table><thead><tr><th align="center">单位</th><th align="center">相对于</th></tr></thead><tbody><tr><td align="center">em</td><td align="center">在 font-size 中使用是相对于父元素的字体大小,在其他属性中使用是相对于自身的字体大小,如 width</td></tr><tr><td align="center">ex</td><td align="center">字符“x”的高度</td></tr><tr><td align="center">ch</td><td align="center">数字“0”的宽度</td></tr><tr><td align="center">rem</td><td align="center">根元素的字体大小</td></tr><tr><td align="center">lh</td><td align="center">元素的line-height</td></tr><tr><td align="center">vw</td><td align="center">视窗宽度的1%</td></tr><tr><td align="center">vh</td><td align="center">视窗高度的1%</td></tr><tr><td align="center">vmin</td><td align="center">视窗较小尺寸的1%</td></tr><tr><td align="center">vmax</td><td align="center">视图大尺寸的1%</td></tr></tbody></table></li></ol><hr><h2 id="三、样式化-文本"><a href="#三、样式化-文本" class="headerlink" title="三、样式化 文本"></a>三、样式化 文本</h2><h3 id="字体样式"><a href="#字体样式" class="headerlink" title="字体样式"></a>字体样式</h3><p><code>作用于字体的属性,会直接应用到文本中,比如使用哪种字体,字体的大小是怎样的,字体是粗体还是斜体,等等。</code></p><ol><li><h3 id="颜色"><a href="#颜色" class="headerlink" title="颜色:"></a>颜色:</h3><p> <code>color 属性设置选中元素的前景内容的颜色</code></p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">p{</span><br><span class="line"> color = red;</span><br><span class="line">}</span><br></pre></td></tr></table></figure></li><li><h3 id="字体种类"><a href="#字体种类" class="headerlink" title="字体种类:"></a>字体种类:</h3><p> <code>font-family 属性, 允许为浏览器指定一个字体 (或者一个字体的列表),然后浏览器可以将这种字体应用到选中的元素上。浏览器只会把在当前机器上可用的字体应用到当前正在访问的网站上;如果字体不可用,那么就会用浏览器默认的字体代替 default font.</code></p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">p{</span><br><span class="line">font-family: arial;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><ol><li><h4 id="网络安全字体:"><a href="#网络安全字体:" class="headerlink" title="网络安全字体:"></a>网络安全字体:</h4><p> <code>可以应用到所有系统 (Arial, Courier New, Georgia, Times New Roman, Trebuchet MS, Verdana)</code></p></li><li><h4 id="默认字体"><a href="#默认字体" class="headerlink" title="默认字体:"></a>默认字体:</h4><p> <code>serif, sans-serif, monospace, cursive,和 fantasy。</code></p><table><thead><tr><th align="center">serif</th><th align="center">sans-serif</th><th align="center">monospace</th><th align="center">cursive</th><th align="center">fantasy</th></tr></thead><tbody><tr><td align="center">有衬线的字体 (衬线一词是指字体笔画尾端的小装饰,存在于某些印刷体字体中)</td><td align="center">没有衬线的字体</td><td align="center">每个字符具有相同宽度的字体,通常用于代码列表</td><td align="center">用于模拟笔迹的字体,具有流动的连接笔画</td><td align="center">用来装饰的字体</td></tr></tbody></table></li><li><h4 id="字体栈"><a href="#字体栈" class="headerlink" title="字体栈:"></a>字体栈:</h4><p> <code>包含一个font-family属性,其值由几个用逗号分离的字体名称组成,便于浏览器多种选择</code></p> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">p{</span><br><span class="line"> font-family: "Trebuchet MS", Verdana, sans-serif;</span><br><span class="line">}</span><br></pre></td></tr></table></figure></li></ol></li><li><h3 id="字体大小"><a href="#字体大小" class="headerlink" title="字体大小:"></a>字体大小:</h3><p> <code>font-size 属性设置,可以取大多数单位,常用单位为: px, em, rem</code></p><p> <code>使用 em / rem 时以 父元素/根元素的大小为基础大小</code></p><table><thead><tr><th align="center">px(像素)</th><th align="center">em</th><th align="center">rem</th></tr></thead><tbody><tr><td align="center">绝对单位,任何情况下,页面上的文本所计算出来的像素值都是一样的</td><td align="center">1em 等于我们设计的当前元素的父元素上设置的字体大小 ,em可以调整任何东西的大小,不只是文本</td><td align="center">1rem 等于 HTML 中的根元素的字体大小 ,而不是父元素,更容易计算字体大小</td></tr></tbody></table></li><li><h3 id="字体样式,字体粗细,文本转换和文本装饰"><a href="#字体样式,字体粗细,文本转换和文本装饰" class="headerlink" title="字体样式,字体粗细,文本转换和文本装饰"></a>字体样式,字体粗细,文本转换和文本装饰</h3><ol><li><h4 id="font-style-用来打开和关闭文本-italic-斜体-。-可能的值如下:"><a href="#font-style-用来打开和关闭文本-italic-斜体-。-可能的值如下:" class="headerlink" title="font-style: 用来打开和关闭文本 italic (斜体)。 可能的值如下:"></a>font-style: <code>用来打开和关闭文本 italic (斜体)。 可能的值如下:</code></h4><ul><li><h5 id="normal-将文本设置为普通字体-将存在的斜体关闭"><a href="#normal-将文本设置为普通字体-将存在的斜体关闭" class="headerlink" title="normal: 将文本设置为普通字体 (将存在的斜体关闭)"></a>normal: <code>将文本设置为普通字体 (将存在的斜体关闭)</code></h5></li><li><h5 id="italic-如果当前字体的斜体版本可用,那么文本设置为斜体版本;如果不可用,那么会利用-oblique-状态来模拟-italics。"><a href="#italic-如果当前字体的斜体版本可用,那么文本设置为斜体版本;如果不可用,那么会利用-oblique-状态来模拟-italics。" class="headerlink" title="italic: 如果当前字体的斜体版本可用,那么文本设置为斜体版本;如果不可用,那么会利用 oblique 状态来模拟 italics。"></a>italic: <code>如果当前字体的斜体版本可用,那么文本设置为斜体版本;如果不可用,那么会利用 oblique 状态来模拟 italics。</code></h5></li><li><h5 id="oblique-将文本设置为斜体字体的模拟版本,也就是将普通文本倾斜的样式应用到文本中。"><a href="#oblique-将文本设置为斜体字体的模拟版本,也就是将普通文本倾斜的样式应用到文本中。" class="headerlink" title="oblique: 将文本设置为斜体字体的模拟版本,也就是将普通文本倾斜的样式应用到文本中。"></a>oblique: <code>将文本设置为斜体字体的模拟版本,也就是将普通文本倾斜的样式应用到文本中。</code></h5></li></ul></li><li><h4 id="font-weight-设置文字的粗体大小。这里有很多值可选-比如-light-normal-bold-extrabold-black-等等-不过事实上你很少会用到-normal-和-bold以外的值:"><a href="#font-weight-设置文字的粗体大小。这里有很多值可选-比如-light-normal-bold-extrabold-black-等等-不过事实上你很少会用到-normal-和-bold以外的值:" class="headerlink" title="font-weight: 设置文字的粗体大小。这里有很多值可选 (比如 -light, -normal, -bold, -extrabold, -black, 等等), 不过事实上你很少会用到 normal 和 bold以外的值:"></a>font-weight: <code>设置文字的粗体大小。这里有很多值可选 (比如 -light, -normal, -bold, -extrabold, -black, 等等), 不过事实上你很少会用到 normal 和 bold以外的值:</code></h4><ul><li><h5 id="normal-bold-普通-加粗字体"><a href="#normal-bold-普通-加粗字体" class="headerlink" title="normal, bold: 普通, 加粗字体"></a>normal, bold: <code>普通, 加粗字体</code></h5></li><li><h5 id="lighter-bolder-将当前元素的粗体设置为比其父元素粗体更细或更粗一步。100–900-数值粗体值,如果需要,可提供比上述关键字更精细的粒度控制。"><a href="#lighter-bolder-将当前元素的粗体设置为比其父元素粗体更细或更粗一步。100–900-数值粗体值,如果需要,可提供比上述关键字更精细的粒度控制。" class="headerlink" title="lighter, bolder: 将当前元素的粗体设置为比其父元素粗体更细或更粗一步。100–900: 数值粗体值,如果需要,可提供比上述关键字更精细的粒度控制。"></a>lighter, bolder: <code>将当前元素的粗体设置为比其父元素粗体更细或更粗一步。100–900: 数值粗体值,如果需要,可提供比上述关键字更精细的粒度控制。</code></h5></li></ul></li><li><h4 id="text-transform-允许你设置要转换的字体。值包括:"><a href="#text-transform-允许你设置要转换的字体。值包括:" class="headerlink" title="text-transform: 允许你设置要转换的字体。值包括:"></a>text-transform: <code>允许你设置要转换的字体。值包括:</code></h4><ul><li><h5 id="none-防止任何转型。"><a href="#none-防止任何转型。" class="headerlink" title="none: 防止任何转型。"></a>none: <code>防止任何转型。</code></h5></li><li><h5 id="uppercase-将所有文本转为大写。"><a href="#uppercase-将所有文本转为大写。" class="headerlink" title="uppercase: 将所有文本转为大写。"></a>uppercase: <code>将所有文本转为大写。</code></h5></li><li><h5 id="lowercase-将所有文本转为小写。"><a href="#lowercase-将所有文本转为小写。" class="headerlink" title="lowercase: 将所有文本转为小写。"></a>lowercase: <code>将所有文本转为小写。</code></h5></li><li><h5 id="capitalize-转换所有单词让其首字母大写。"><a href="#capitalize-转换所有单词让其首字母大写。" class="headerlink" title="capitalize: 转换所有单词让其首字母大写。"></a>capitalize: <code>转换所有单词让其首字母大写。</code></h5></li><li><h5 id="full-width-将所有字形转换成全角,即固定宽度的正方形,类似于等宽字体,允许拉丁字符和亚洲语言字形(如中文,日文,韩文)对齐。"><a href="#full-width-将所有字形转换成全角,即固定宽度的正方形,类似于等宽字体,允许拉丁字符和亚洲语言字形(如中文,日文,韩文)对齐。" class="headerlink" title="full-width: 将所有字形转换成全角,即固定宽度的正方形,类似于等宽字体,允许拉丁字符和亚洲语言字形(如中文,日文,韩文)对齐。"></a>full-width: <code>将所有字形转换成全角,即固定宽度的正方形,类似于等宽字体,允许拉丁字符和亚洲语言字形(如中文,日文,韩文)对齐。</code></h5></li></ul></li><li><h4 id="text-decoration-设置-取消字体上的文本装饰-你将主要使用此方法在设置链接时取消设置链接上的默认下划线-可以接受多个值,同时为一段文本设置上、下、中划线-可用值为:"><a href="#text-decoration-设置-取消字体上的文本装饰-你将主要使用此方法在设置链接时取消设置链接上的默认下划线-可以接受多个值,同时为一段文本设置上、下、中划线-可用值为:" class="headerlink" title="text-decoration: 设置/取消字体上的文本装饰 (你将主要使用此方法在设置链接时取消设置链接上的默认下划线, 可以接受多个值,同时为一段文本设置上、下、中划线) 可用值为:"></a>text-decoration: <code>设置/取消字体上的文本装饰 (你将主要使用此方法在设置链接时取消设置链接上的默认下划线, 可以接受多个值,同时为一段文本设置上、下、中划线) 可用值为:</code></h4><ul><li><h5 id="none-取消已经存在的任何文本装饰。"><a href="#none-取消已经存在的任何文本装饰。" class="headerlink" title="none: 取消已经存在的任何文本装饰。"></a>none: <code>取消已经存在的任何文本装饰。</code></h5></li><li><h5 id="underline-文本下划线"><a href="#underline-文本下划线" class="headerlink" title="underline: 文本下划线."></a>underline: <code>文本下划线.</code></h5></li><li><h5 id="overlinre-文本上划线"><a href="#overlinre-文本上划线" class="headerlink" title="overlinre: 文本上划线"></a>overlinre: <code>文本上划线</code></h5></li><li><h5 id="line-through-穿过文本的线"><a href="#line-through-穿过文本的线" class="headerlink" title="line-through: 穿过文本的线"></a>line-through: <code>穿过文本的线</code></h5></li></ul></li><li><h4 id="文字阴影-text-shadow-属性。这最多需要-4-个值-正偏移-1px向右移动,负偏移-1px向左移动"><a href="#文字阴影-text-shadow-属性。这最多需要-4-个值-正偏移-1px向右移动,负偏移-1px向左移动" class="headerlink" title="文字阴影: text-shadow 属性。这最多需要 4 个值 (正偏移 1px向右移动,负偏移 -1px向左移动) :"></a>文字阴影: <code>text-shadow 属性。这最多需要 4 个值 (正偏移 1px向右移动,负偏移 -1px向左移动) :</code></h4><ul><li><h5 id="阴影与原始文本的水平偏移,可以使用大多数的-CSS-单位-length-and-size-units-但是-px-是比较合适的。这个值必须指定。"><a href="#阴影与原始文本的水平偏移,可以使用大多数的-CSS-单位-length-and-size-units-但是-px-是比较合适的。这个值必须指定。" class="headerlink" title="阴影与原始文本的水平偏移,可以使用大多数的 CSS 单位 length and size units, 但是 px 是比较合适的。这个值必须指定。"></a><code>阴影与原始文本的水平偏移,可以使用大多数的 CSS 单位 length and size units, 但是 px 是比较合适的。这个值必须指定。</code></h5></li><li><h5 id="阴影与原始文本的垂直偏移-效果基本上就像水平偏移,除了它向上-向下移动阴影,而不是左-右。这个值必须指定。"><a href="#阴影与原始文本的垂直偏移-效果基本上就像水平偏移,除了它向上-向下移动阴影,而不是左-右。这个值必须指定。" class="headerlink" title="阴影与原始文本的垂直偏移;效果基本上就像水平偏移,除了它向上/向下移动阴影,而不是左/右。这个值必须指定。"></a><code>阴影与原始文本的垂直偏移;效果基本上就像水平偏移,除了它向上/向下移动阴影,而不是左/右。这个值必须指定。</code></h5></li><li><h5 id="模糊半径-更高的值意味着阴影分散得更广泛。如果不包含此值,则默认为0,这意味着没有模糊。可以使用大多数的-CSS-单位-length-and-size-units"><a href="#模糊半径-更高的值意味着阴影分散得更广泛。如果不包含此值,则默认为0,这意味着没有模糊。可以使用大多数的-CSS-单位-length-and-size-units" class="headerlink" title="模糊半径 - 更高的值意味着阴影分散得更广泛。如果不包含此值,则默认为0,这意味着没有模糊。可以使用大多数的 CSS 单位 length and size units."></a><code>模糊半径 - 更高的值意味着阴影分散得更广泛。如果不包含此值,则默认为0,这意味着没有模糊。可以使用大多数的 CSS 单位 length and size units.</code></h5></li><li><h5 id="阴影的基础颜色,可以使用大多数的-CSS-颜色单位-CSS-color-unit-如果没有指定,默认为-black"><a href="#阴影的基础颜色,可以使用大多数的-CSS-颜色单位-CSS-color-unit-如果没有指定,默认为-black" class="headerlink" title="阴影的基础颜色,可以使用大多数的 CSS 颜色单位 CSS color unit. 如果没有指定,默认为 black."></a><code>阴影的基础颜色,可以使用大多数的 CSS 颜色单位 CSS color unit. 如果没有指定,默认为 black.</code></h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">p{</span><br><span class="line"> text-shadow: 4px 4px 5px red;</span><br><span class="line">}</span><br></pre></td></tr></table></figure></li><li><h5 id="多种阴影-包含以逗号分隔的多个阴影值,将多个阴影应用于同一文本"><a href="#多种阴影-包含以逗号分隔的多个阴影值,将多个阴影应用于同一文本" class="headerlink" title="多种阴影 包含以逗号分隔的多个阴影值,将多个阴影应用于同一文本"></a>多种阴影 <code>包含以逗号分隔的多个阴影值,将多个阴影应用于同一文本</code></h5> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">text-shadow: -1px -1px 1px #aaa,</span><br><span class="line"> 0px 4px 1px rgba(0,0,0,0.5),</span><br><span class="line"> 4px 4px 5px rgba(0,0,0,0.7),</span><br><span class="line"> 0px 0px 7px rgba(0,0,0,0.4);</span><br></pre></td></tr></table></figure></li></ul></li></ol></li></ol><h3 id="文本布局"><a href="#文本布局" class="headerlink" title="文本布局"></a>文本布局</h3><p><code>作用于文本的间距以及其他布局功能的属性,比如,允许操纵行与字之间的空间,以及在内容框中,文本如何对齐。</code></p><ol><li><h3 id="文本对齐"><a href="#文本对齐" class="headerlink" title="文本对齐:"></a>文本对齐:</h3><p> <code>text-align 属性用来控制文本如何和它所在的内容盒子对齐。可用值如下:</code></p><ul><li><h4 id="left-左对齐文本。"><a href="#left-左对齐文本。" class="headerlink" title="left: 左对齐文本。"></a>left: <code>左对齐文本。</code></h4></li><li><h4 id="right-右对齐文本。"><a href="#right-右对齐文本。" class="headerlink" title="right: 右对齐文本。"></a>right: <code>右对齐文本。</code></h4></li><li><h4 id="center-居中文字"><a href="#center-居中文字" class="headerlink" title="center: 居中文字"></a>center: <code>居中文字</code></h4></li><li><h4 id="justify-使文本展开,改变单词之间的差距,使所有文本行的宽度相同。你需要仔细使用,它可以看起来很可怕。特别是当应用于其中有很多长单词的段落时。如果你要使用这个,你也应该考虑一起使用别的东西,比如-hyphens,打破一些更长的词语。"><a href="#justify-使文本展开,改变单词之间的差距,使所有文本行的宽度相同。你需要仔细使用,它可以看起来很可怕。特别是当应用于其中有很多长单词的段落时。如果你要使用这个,你也应该考虑一起使用别的东西,比如-hyphens,打破一些更长的词语。" class="headerlink" title="justify: 使文本展开,改变单词之间的差距,使所有文本行的宽度相同。你需要仔细使用,它可以看起来很可怕。特别是当应用于其中有很多长单词的段落时。如果你要使用这个,你也应该考虑一起使用别的东西,比如 hyphens,打破一些更长的词语。"></a>justify: <code>使文本展开,改变单词之间的差距,使所有文本行的宽度相同。你需要仔细使用,它可以看起来很可怕。特别是当应用于其中有很多长单词的段落时。如果你要使用这个,你也应该考虑一起使用别的东西,比如 hyphens,打破一些更长的词语。</code></h4></li></ul></li><li><h3 id="行高"><a href="#行高" class="headerlink" title="行高:"></a>行高:</h3><p> <code>line-height 属性设置文本每行之间的高,可以接受大多数单位,不过也可以设置一个无单位的值,作为乘数,通常这种是比较好的做法。无单位的值乘以 font-size 来获得 line-height。当行与行之间拉开空间,正文文本通常看起来更好更容易阅读。推荐的行高大约是 1.5–2 (双倍间距。) 所以要把我们的文本行高设置为字体高度的1.5倍,你可以使用这个:</code></p><pre><code><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">line-height: 1.5;</span><br></pre></td></tr></table></figure></code></pre></li><li><h3 id="字母和单词间距"><a href="#字母和单词间距" class="headerlink" title="字母和单词间距:"></a>字母和单词间距:</h3><p> <code>letter-spacing 和 word-spacing 属性允许你设置你的文本中的字母与字母之间的间距、或是单词与单词之间的间距。你不会经常使用它们,但是可能可以通过它们,来获得一个特定的外观,或者让较为密集的文字更加可读。它们可以接受大多数单位</code></p><pre><code><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">把这个样式应用到 <p> 段落的第一行:</span><br><span class="line">p::first-line {</span><br><span class="line"> letter-spacing: 2px;</span><br><span class="line"> word-spacing: 4px;</span><br><span class="line">}</span><br></pre></td></tr></table></figure></code></pre></li><li><h3 id="其他可能用到的属性"><a href="#其他可能用到的属性" class="headerlink" title="其他可能用到的属性"></a>其他可能用到的属性</h3><ul><li><h4 id="Font-样式"><a href="#Font-样式" class="headerlink" title="Font 样式:"></a>Font 样式:</h4><ul><li><h5 id="font-variant-在小型大写字母和普通文本选项之间切换。"><a href="#font-variant-在小型大写字母和普通文本选项之间切换。" class="headerlink" title="font-variant: 在小型大写字母和普通文本选项之间切换。"></a>font-variant: <code>在小型大写字母和普通文本选项之间切换。</code></h5></li><li><h5 id="font-kerning-开启或关闭字体间距选项。"><a href="#font-kerning-开启或关闭字体间距选项。" class="headerlink" title="font-kerning: 开启或关闭字体间距选项。"></a>font-kerning: <code>开启或关闭字体间距选项。</code></h5></li><li><h5 id="font-feature-settings-开启或关闭不同的-OpenType-字体特性。"><a href="#font-feature-settings-开启或关闭不同的-OpenType-字体特性。" class="headerlink" title="font-feature-settings: 开启或关闭不同的 OpenType 字体特性。"></a>font-feature-settings: <code>开启或关闭不同的 OpenType 字体特性。</code></h5></li><li><h5 id="font-variant-alternates-控制给定的自定义字体的替代字形的使用。"><a href="#font-variant-alternates-控制给定的自定义字体的替代字形的使用。" class="headerlink" title="font-variant-alternates: 控制给定的自定义字体的替代字形的使用。"></a>font-variant-alternates: <code>控制给定的自定义字体的替代字形的使用。</code></h5></li><li><h5 id="font-variant-caps-控制大写字母替代字形的使用。"><a href="#font-variant-caps-控制大写字母替代字形的使用。" class="headerlink" title="font-variant-caps: 控制大写字母替代字形的使用。"></a>font-variant-caps: <code>控制大写字母替代字形的使用。</code></h5></li><li><h5 id="font-variant-east-asian-控制东亚文字替代字形的使用-像日语和汉语。"><a href="#font-variant-east-asian-控制东亚文字替代字形的使用-像日语和汉语。" class="headerlink" title="font-variant-east-asian: 控制东亚文字替代字形的使用, 像日语和汉语。"></a>font-variant-east-asian: <code>控制东亚文字替代字形的使用, 像日语和汉语。</code></h5></li><li><h5 id="font-variant-ligatures-控制文本中使用的连写和上下文形式。"><a href="#font-variant-ligatures-控制文本中使用的连写和上下文形式。" class="headerlink" title="font-variant-ligatures: 控制文本中使用的连写和上下文形式。"></a>font-variant-ligatures: <code>控制文本中使用的连写和上下文形式。</code></h5></li><li><h5 id="font-variant-numeric-控制数字,分式和序标的替代字形的使用。"><a href="#font-variant-numeric-控制数字,分式和序标的替代字形的使用。" class="headerlink" title="font-variant-numeric: 控制数字,分式和序标的替代字形的使用。"></a>font-variant-numeric: <code>控制数字,分式和序标的替代字形的使用。</code></h5></li><li><h5 id="font-variant-position-控制位于上标或下标处,字号更小的替代字形的使用。"><a href="#font-variant-position-控制位于上标或下标处,字号更小的替代字形的使用。" class="headerlink" title="font-variant-position: 控制位于上标或下标处,字号更小的替代字形的使用。"></a>font-variant-position: <code>控制位于上标或下标处,字号更小的替代字形的使用。</code></h5></li><li><h5 id="font-size-adjust-独立于字体的实际大小尺寸,调整其可视大小尺寸。"><a href="#font-size-adjust-独立于字体的实际大小尺寸,调整其可视大小尺寸。" class="headerlink" title="font-size-adjust: 独立于字体的实际大小尺寸,调整其可视大小尺寸。"></a>font-size-adjust: <code>独立于字体的实际大小尺寸,调整其可视大小尺寸。</code></h5></li><li><h5 id="font-stretch-在给定字体的可选拉伸版本中切换。"><a href="#font-stretch-在给定字体的可选拉伸版本中切换。" class="headerlink" title="font-stretch: 在给定字体的可选拉伸版本中切换。"></a>font-stretch: <code>在给定字体的可选拉伸版本中切换。</code></h5></li><li><h5 id="text-underline-position-指定下划线的排版位置,通过使用"><a href="#text-underline-position-指定下划线的排版位置,通过使用" class="headerlink" title="text-underline-position: 指定下划线的排版位置,通过使用"></a>text-underline-position: <code>指定下划线的排版位置,通过使用</code></h5></li><li><h5 id="text-decoration-line-属性的underline-值。"><a href="#text-decoration-line-属性的underline-值。" class="headerlink" title="text-decoration-line 属性的underline 值。"></a>text-decoration-line <code>属性的underline 值。</code></h5></li><li><h5 id="text-rendering-尝试执行一些文本渲染优化。"><a href="#text-rendering-尝试执行一些文本渲染优化。" class="headerlink" title="text-rendering: 尝试执行一些文本渲染优化。"></a>text-rendering: <code>尝试执行一些文本渲染优化。</code></h5></li></ul></li><li><h4 id="文本布局样式:"><a href="#文本布局样式:" class="headerlink" title="文本布局样式:"></a>文本布局样式:</h4><ul><li><h5 id="text-indent-指定文本内容的第一行前面应该留出多少的水平空间。"><a href="#text-indent-指定文本内容的第一行前面应该留出多少的水平空间。" class="headerlink" title="text-indent: 指定文本内容的第一行前面应该留出多少的水平空间。"></a>text-indent: <code>指定文本内容的第一行前面应该留出多少的水平空间。</code></h5></li><li><h5 id="text-overflow-定义如何向用户表示存在被隐藏的溢出内容。"><a href="#text-overflow-定义如何向用户表示存在被隐藏的溢出内容。" class="headerlink" title="text-overflow: 定义如何向用户表示存在被隐藏的溢出内容。"></a>text-overflow: <code>定义如何向用户表示存在被隐藏的溢出内容。</code></h5></li><li><h5 id="white-space-定义如何处理元素内部的空白和换行。"><a href="#white-space-定义如何处理元素内部的空白和换行。" class="headerlink" title="white-space: 定义如何处理元素内部的空白和换行。"></a>white-space: <code>定义如何处理元素内部的空白和换行。</code></h5></li><li><h5 id="word-break-指定是否能在单词内部换行。"><a href="#word-break-指定是否能在单词内部换行。" class="headerlink" title="word-break: 指定是否能在单词内部换行。"></a>word-break: <code>指定是否能在单词内部换行。</code></h5></li><li><h5 id="direction-定义文本的方向-这取决于语言,并且通常最好让HTML来处理这部分,因为它是和文本内容相关联的。"><a href="#direction-定义文本的方向-这取决于语言,并且通常最好让HTML来处理这部分,因为它是和文本内容相关联的。" class="headerlink" title="direction: 定义文本的方向 (这取决于语言,并且通常最好让HTML来处理这部分,因为它是和文本内容相关联的。)"></a>direction: <code>定义文本的方向 (这取决于语言,并且通常最好让HTML来处理这部分,因为它是和文本内容相关联的。)</code></h5></li><li><h5 id="hyphens-为支持的语言开启或关闭连字符。"><a href="#hyphens-为支持的语言开启或关闭连字符。" class="headerlink" title="hyphens: 为支持的语言开启或关闭连字符。"></a>hyphens: <code>为支持的语言开启或关闭连字符。</code></h5></li><li><h5 id="line-break-对东亚语言采用更强或更弱的换行规则。"><a href="#line-break-对东亚语言采用更强或更弱的换行规则。" class="headerlink" title="line-break: 对东亚语言采用更强或更弱的换行规则。"></a>line-break: <code>对东亚语言采用更强或更弱的换行规则。</code></h5></li><li><h5 id="text-align-last-定义一个块或行的最后一行,恰好位于一个强制换行前时,如何对齐。"><a href="#text-align-last-定义一个块或行的最后一行,恰好位于一个强制换行前时,如何对齐。" class="headerlink" title="text-align-last: 定义一个块或行的最后一行,恰好位于一个强制换行前时,如何对齐。"></a>text-align-last: <code>定义一个块或行的最后一行,恰好位于一个强制换行前时,如何对齐。</code></h5></li><li><h5 id="text-orientation-定义行内文本的方向。"><a href="#text-orientation-定义行内文本的方向。" class="headerlink" title="text-orientation: 定义行内文本的方向。"></a>text-orientation: <code>定义行内文本的方向。</code></h5></li><li><h5 id="word-wrap-指定浏览器是否可以在单词内换行以避免超出范围。"><a href="#word-wrap-指定浏览器是否可以在单词内换行以避免超出范围。" class="headerlink" title="word-wrap: 指定浏览器是否可以在单词内换行以避免超出范围。"></a>word-wrap: <code>指定浏览器是否可以在单词内换行以避免超出范围。</code></h5></li><li><h5 id="writing-mode-定义文本行布局为水平还是垂直,以及后继文本流的方向。"><a href="#writing-mode-定义文本行布局为水平还是垂直,以及后继文本流的方向。" class="headerlink" title="writing-mode: 定义文本行布局为水平还是垂直,以及后继文本流的方向。"></a>writing-mode: <code>定义文本行布局为水平还是垂直,以及后继文本流的方向。</code></h5><h3 id="样式列表"><a href="#样式列表" class="headerlink" title="样式列表"></a>样式列表</h3><h4 id="列表特定样式"><a href="#列表特定样式" class="headerlink" title="列表特定样式"></a>列表特定样式</h4><code>三个属性可以在 <ul> 或 <ol> 元素上设置:</code></li></ul></li></ul></li></ol><ul><li><h5 id="list-style-type-:设置用于列表的项目符号的类型,例如无序列表的方形或圆形项目符号,或有序列表的数字,字母或罗马数字。"><a href="#list-style-type-:设置用于列表的项目符号的类型,例如无序列表的方形或圆形项目符号,或有序列表的数字,字母或罗马数字。" class="headerlink" title="list-style-type :设置用于列表的项目符号的类型,例如无序列表的方形或圆形项目符号,或有序列表的数字,字母或罗马数字。"></a>list-style-type :<code>设置用于列表的项目符号的类型,例如无序列表的方形或圆形项目符号,或有序列表的数字,字母或罗马数字。</code></h5></li><li><h5 id="list-style-position-:设置在每个项目开始之前,项目符号是出现在列表项内,还是出现在其外。"><a href="#list-style-position-:设置在每个项目开始之前,项目符号是出现在列表项内,还是出现在其外。" class="headerlink" title="list-style-position :设置在每个项目开始之前,项目符号是出现在列表项内,还是出现在其外。"></a>list-style-position :<code>设置在每个项目开始之前,项目符号是出现在列表项内,还是出现在其外。</code></h5></li><li><h5 id="list-style-image-:允许您为项目符号使用自定义图片,而不是简单的方形或圆形。"><a href="#list-style-image-:允许您为项目符号使用自定义图片,而不是简单的方形或圆形。" class="headerlink" title="list-style-image :允许您为项目符号使用自定义图片,而不是简单的方形或圆形。"></a>list-style-image :<code>允许您为项目符号使用自定义图片,而不是简单的方形或圆形。</code></h5></li></ul><ol><li><h4 id="符号样式"><a href="#符号样式" class="headerlink" title="符号样式"></a>符号样式</h4> <code>list-style-type 属性允许设置列表元素的 marker(比如圆点、符号、或者自定义计数器样式)</code><ul><li><h5 id="disc-实心圆点-默认值"><a href="#disc-实心圆点-默认值" class="headerlink" title="disc 实心圆点 (默认值)"></a>disc 实心圆点 (默认值)</h5></li><li><h5 id="circle-空心圆点"><a href="#circle-空心圆点" class="headerlink" title="circle 空心圆点"></a>circle 空心圆点</h5></li><li><h5 id="square-实心方块"><a href="#square-实心方块" class="headerlink" title="square 实心方块"></a>square 实心方块</h5></li><li><h5 id="decimal-十进制阿拉伯数字-从1开始"><a href="#decimal-十进制阿拉伯数字-从1开始" class="headerlink" title="decimal 十进制阿拉伯数字,从1开始"></a>decimal 十进制阿拉伯数字,从1开始</h5></li><li><h5 id="cjk-decimal-中日韩十进制数-例如-一-二-三-九八-九九-一〇〇"><a href="#cjk-decimal-中日韩十进制数-例如-一-二-三-九八-九九-一〇〇" class="headerlink" title="cjk-decimal 中日韩十进制数, 例如: 一, 二, 三, ..., 九八, 九九, 一〇〇"></a>cjk-decimal 中日韩十进制数, <code>例如: 一, 二, 三, ..., 九八, 九九, 一〇〇</code></h5></li><li><h5 id="decimal-leading-zero-E-g-01-02-03-…-98-99"><a href="#decimal-leading-zero-E-g-01-02-03-…-98-99" class="headerlink" title="decimal-leading-zero, E.g. 01, 02, 03, … 98, 99"></a>decimal-leading-zero, <code>E.g. 01, 02, 03, … 98, 99</code></h5></li><li><h5 id="lower-roman-E-g-i-ii-iii-iv-v…"><a href="#lower-roman-E-g-i-ii-iii-iv-v…" class="headerlink" title="lower-roman, E.g. i, ii, iii, iv, v…"></a>lower-roman, <code>E.g. i, ii, iii, iv, v…</code></h5></li><li><h5 id="upper-roman-E-g-I-II-III-IV-V…"><a href="#upper-roman-E-g-I-II-III-IV-V…" class="headerlink" title="upper-roman, E.g. I, II, III, IV, V…"></a>upper-roman, <code>E.g. I, II, III, IV, V…</code></h5></li><li><h5 id="lower-greek-E-g-α-β-γ…"><a href="#lower-greek-E-g-α-β-γ…" class="headerlink" title="lower-greek, E.g. α, β, γ…"></a>lower-greek, <code>E.g. α, β, γ…</code></h5></li><li><h5 id="lower-alpha-E-g-a-b-c-…-z"><a href="#lower-alpha-E-g-a-b-c-…-z" class="headerlink" title="lower-alpha, E.g. a, b, c, … z"></a>lower-alpha, <code>E.g. a, b, c, … z</code></h5></li><li><h5 id="upper-alpha-E-g-A-B-C-…-Z"><a href="#upper-alpha-E-g-A-B-C-…-Z" class="headerlink" title="upper-alpha, E.g. A, B, C, … Z"></a>upper-alpha, <code>E.g. A, B, C, … Z</code></h5></li></ul></li><li><h4 id="项目符号位置"><a href="#项目符号位置" class="headerlink" title="项目符号位置"></a>项目符号位置</h4> <code>list-style-position 设置在每个项目开始之前,项目符号是出现在列表项内,还是出现在其外。默认值为 outside,这使项目符号位于列表项之外。如果值设置为 inside,项目条目则位于行内。</code></li><li><h4 id="使用自定义的项目符号图片"><a href="#使用自定义的项目符号图片" class="headerlink" title="使用自定义的项目符号图片"></a>使用自定义的项目符号图片</h4> <code>list-style-image 属性允许对于项目符号使用自定义图片,在控制项目符号的位置,大小等方面是有限的,可以对无序排列更改</code> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">ul li {</span><br><span class="line"> padding-left: 2rem;</span><br><span class="line"> background-image: url(star.svg);</span><br><span class="line"> background-position: 0 0;</span><br><span class="line"> background-size: 1.6rem 1.6rem;</span><br><span class="line"> background-repeat: no-repeat;</span><br><span class="line"> }</span><br></pre></td></tr></table></figure><h5 id="管理列表计数"><a href="#管理列表计数" class="headerlink" title="管理列表计数"></a>管理列表计数</h5><code>在有序列表上进行不同的计数方式。例如: 从1以外的数字开始,或向后倒数,或者按步或多于1计数。</code></li><li><h4 id="start"><a href="#start" class="headerlink" title="start"></a>start</h4> <code>start 属性允许你从1 以外的数字开始计数。</code> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><ol start="4"></span><br><span class="line"> <li></li></span><br><span class="line"> <li></li></span><br><span class="line"> <li></li></span><br><span class="line"></ol></span><br></pre></td></tr></table></figure></li><li><h4 id="reversed"><a href="#reversed" class="headerlink" title="reversed"></a>reversed</h4> <code>reversed 属性将启动列表倒计数</code> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><ol start="4" reversed></span><br><span class="line"> <li></li></span><br><span class="line"> <li></li></span><br><span class="line"> <li></li></span><br><span class="line"></ol></span><br></pre></td></tr></table></figure></li><li><h4 id="value"><a href="#value" class="headerlink" title="value"></a>value</h4> <code>value 属性允许设置列表项指定数值</code> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><ol start="4" reversed></span><br><span class="line"> <li value=2></li></span><br><span class="line"> <li value=4></li></span><br><span class="line"> <li value=6></li></span><br><span class="line"></ol></span><br></pre></td></tr></table></figure><h3 id="样式化链接"><a href="#样式化链接" class="headerlink" title="样式化链接"></a>样式化链接</h3></li><li><h4 id="链接状态"><a href="#链接状态" class="headerlink" title="链接状态"></a>链接状态</h4> <code>链接存在时处于不同的状态,每一个状态都可以用对应的 伪类 来应用样式:</code><ul><li><h5 id="Link-没有访问过的-这是链接的默认状态,当它没有处在其他状态的时候,它可以使用-link-伪类来应用样式。"><a href="#Link-没有访问过的-这是链接的默认状态,当它没有处在其他状态的时候,它可以使用-link-伪类来应用样式。" class="headerlink" title="Link (没有访问过的): 这是链接的默认状态,当它没有处在其他状态的时候,它可以使用:link 伪类来应用样式。"></a>Link (没有访问过的): <code>这是链接的默认状态,当它没有处在其他状态的时候,它可以使用:link 伪类来应用样式。</code></h5></li><li><h5 id="Visited-这个链接已经被访问过了-存在于浏览器的历史纪录-它可以使用-visited-伪类来应用样式。"><a href="#Visited-这个链接已经被访问过了-存在于浏览器的历史纪录-它可以使用-visited-伪类来应用样式。" class="headerlink" title="Visited: 这个链接已经被访问过了(存在于浏览器的历史纪录), 它可以使用 :visited 伪类来应用样式。"></a>Visited: <code>这个链接已经被访问过了(存在于浏览器的历史纪录), 它可以使用 :visited 伪类来应用样式。</code></h5></li><li><h5 id="Hover-当用户的鼠标光标刚好停留在这个链接,它可以使用-hover-伪类来应用样式。"><a href="#Hover-当用户的鼠标光标刚好停留在这个链接,它可以使用-hover-伪类来应用样式。" class="headerlink" title="Hover: 当用户的鼠标光标刚好停留在这个链接,它可以使用 :hover 伪类来应用样式。"></a>Hover: <code>当用户的鼠标光标刚好停留在这个链接,它可以使用 :hover 伪类来应用样式。</code></h5></li><li><h5 id="Focus-一个链接当它被选中的时候-比如通过键盘的-Tab-移动到这个链接的时候,或者使用编程的方法来选中这个链接-HTMLElement-focus-它可以使用-focus-伪类来应用样式。"><a href="#Focus-一个链接当它被选中的时候-比如通过键盘的-Tab-移动到这个链接的时候,或者使用编程的方法来选中这个链接-HTMLElement-focus-它可以使用-focus-伪类来应用样式。" class="headerlink" title="Focus: 一个链接当它被选中的时候 (比如通过键盘的 Tab 移动到这个链接的时候,或者使用编程的方法来选中这个链接 HTMLElement.focus()) 它可以使用 :focus 伪类来应用样式。"></a>Focus: <code>一个链接当它被选中的时候 (比如通过键盘的 Tab 移动到这个链接的时候,或者使用编程的方法来选中这个链接 HTMLElement.focus()) 它可以使用 :focus 伪类来应用样式。</code></h5></li><li><h5 id="Active-一个链接当它被激活的时候-比如被点击的时候-,它可以使用-active-伪类来应用样式。"><a href="#Active-一个链接当它被激活的时候-比如被点击的时候-,它可以使用-active-伪类来应用样式。" class="headerlink" title="Active: 一个链接当它被激活的时候 (比如被点击的时候),它可以使用 :active 伪类来应用样式。"></a>Active: <code>一个链接当它被激活的时候 (比如被点击的时候),它可以使用 :active 伪类来应用样式。</code></h5></li></ul></li><li><h4 id="默认样式"><a href="#默认样式" class="headerlink" title="默认样式"></a>默认样式</h4><ul><li><h5 id="链接具有下划线。"><a href="#链接具有下划线。" class="headerlink" title="链接具有下划线。"></a><code>链接具有下划线。</code></h5></li><li><h5 id="未访问过的-Unvisited-的链接是蓝色的。"><a href="#未访问过的-Unvisited-的链接是蓝色的。" class="headerlink" title="未访问过的 (Unvisited) 的链接是蓝色的。"></a><code>未访问过的 (Unvisited) 的链接是蓝色的。</code></h5></li><li><h5 id="访问过的-Visited-的链接是紫色的"><a href="#访问过的-Visited-的链接是紫色的" class="headerlink" title="``访问过的 (Visited) 的链接是紫色的.`"></a>``访问过的 (Visited) 的链接是紫色的.`</h5></li><li><h5 id="悬停-Hover-在一个链接的时候鼠标的光标会变成一个小手的图标。"><a href="#悬停-Hover-在一个链接的时候鼠标的光标会变成一个小手的图标。" class="headerlink" title="悬停 (Hover) 在一个链接的时候鼠标的光标会变成一个小手的图标。`"></a>悬停 (Hover) 在一个链接的时候鼠标的光标会变成一个小手的图标。`</h5></li><li><h5 id="选中-Focus-链接的时候,链接周围会有一个轮廓,你应该可以按-tab-来选中这个页面的链接-在-Mac-上-你可能需要使用Full-Keyboard-Access-All-controls-选项,然后再按下-Ctrl-F7-,这样就可以起作用"><a href="#选中-Focus-链接的时候,链接周围会有一个轮廓,你应该可以按-tab-来选中这个页面的链接-在-Mac-上-你可能需要使用Full-Keyboard-Access-All-controls-选项,然后再按下-Ctrl-F7-,这样就可以起作用" class="headerlink" title="选中 (Focus) 链接的时候,链接周围会有一个轮廓,你应该可以按 tab 来选中这个页面的链接 (在 Mac 上, 你可能需要使用Full Keyboard Access: All controls 选项,然后再按下 Ctrl + F7 ,这样就可以起作用)"></a><code>选中 (Focus) 链接的时候,链接周围会有一个轮廓,你应该可以按 tab 来选中这个页面的链接 (在 Mac 上, 你可能需要使用Full Keyboard Access: All controls 选项,然后再按下 Ctrl + F7 ,这样就可以起作用)</code></h5></li><li><h5 id="激活-Active-链接的时候会变成红色-当你点击链接时,请尝试按住鼠标按钮。"><a href="#激活-Active-链接的时候会变成红色-当你点击链接时,请尝试按住鼠标按钮。" class="headerlink" title="激活 (Active) 链接的时候会变成红色 (当你点击链接时,请尝试按住鼠标按钮。)"></a><code>激活 (Active) 链接的时候会变成红色 (当你点击链接时,请尝试按住鼠标按钮。)</code></h5></li></ul></li><li><h4 id="将样式应用到一些链接-必需按顺序"><a href="#将样式应用到一些链接-必需按顺序" class="headerlink" title="将样式应用到一些链接 (必需按顺序)"></a>将样式应用到一些链接 (必需按顺序)</h4> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">a {</span><br><span class="line"></span><br><span class="line">}</span><br><span class="line">a:link {</span><br><span class="line"></span><br><span class="line">}</span><br><span class="line">a:visited {</span><br><span class="line"></span><br><span class="line">}</span><br><span class="line">a:focus {</span><br><span class="line"></span><br><span class="line">}</span><br><span class="line">a:hover {</span><br><span class="line"></span><br><span class="line">}</span><br><span class="line">a:active {</span><br><span class="line"></span><br><span class="line">}</span><br></pre></td></tr></table></figure></li><li><h4 id="在链接中包含图标-属性选择器——a-href-quot-http-quot-选中-lt-a-gt-元素,但是这样只会选中那些拥有-href-属性,且属性的值包含-quot-http-quot-的-lt-a-gt-的元素。指定位置为100-,使其出现在内容的右边,距离上方是0px-padding-right-,为背景图片留出空间,这样就不会和文本重叠。"><a href="#在链接中包含图标-属性选择器——a-href-quot-http-quot-选中-lt-a-gt-元素,但是这样只会选中那些拥有-href-属性,且属性的值包含-quot-http-quot-的-lt-a-gt-的元素。指定位置为100-,使其出现在内容的右边,距离上方是0px-padding-right-,为背景图片留出空间,这样就不会和文本重叠。" class="headerlink" title="在链接中包含图标 (属性选择器——a[href*="http"] 选中 <a> 元素,但是这样只会选中那些拥有 href 属性,且属性的值包含 "http" 的 <a>的元素。指定位置为100%,使其出现在内容的右边,距离上方是0px, padding-right ,为背景图片留出空间,这样就不会和文本重叠。)"></a><code>在链接中包含图标 (属性选择器——a[href*="http"] 选中 <a> 元素,但是这样只会选中那些拥有 href 属性,且属性的值包含 "http" 的 <a>的元素。指定位置为100%,使其出现在内容的右边,距离上方是0px, padding-right ,为背景图片留出空间,这样就不会和文本重叠。)</code></h4> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">a[href*="http"] {</span><br><span class="line"> background: url('https://mdn.mozillademos.org/files/12982/external-link-52.png') no-repeat 100% 0;</span><br><span class="line"> background-size: 16px 16px;</span><br><span class="line"> padding-right: 19px;</span><br><span class="line">}</span><br></pre></td></tr></table></figure></li><li><h4 id="样式化链接为按钮"><a href="#样式化链接为按钮" class="headerlink" title="样式化链接为按钮"></a>样式化链接为按钮</h4> <a href="https://developer.mozilla.org/zh-CN/docs/Learn/CSS/%E4%B8%BA%E6%96%87%E6%9C%AC%E6%B7%BB%E5%8A%A0%E6%A0%B7%E5%BC%8F/Styling_links," target="_blank" rel="noopener" title="样式化按钮">具体请看</a></li></ol>]]></content>
<tags>
<tag> 前端学习 </tag>
</tags>
</entry>
<entry>
<title>python_常用模块</title>
<link href="/2020/07/26/python-chang-yong-mo-kuai/"/>
<url>/2020/07/26/python-chang-yong-mo-kuai/</url>
<content type="html"><![CDATA[<h1 id="模块制作"><a href="#模块制作" class="headerlink" title="模块制作"></a>模块制作</h1><ol><li><p><strong>“”” “”” / #</strong> : <code>顶格写的注释被称为文档 (__doc__),在模块中的注释与帮助,可以通过 help(model_name) 查看</code></p></li><li><p><strong>import model_name / from model_name import function_name</strong></p><h1 id="常用模块"><a href="#常用模块" class="headerlink" title="常用模块"></a>常用模块</h1></li><li><p><strong>random 随机模块</strong></p><ol><li><code>random.random 随机生成 0~1 间小数</code></li><li><code>random.randint 随机生成指定范围的整数,包含上下限</code></li><li><code>random.randrange 随机生成指定 range 范围的整数,不包含上限 randrange(self, start, stop=None, step=1, _int=int)</code></li><li><code>random.choice 随机获取指定序列的一个元素</code></li><li><code>random.sample 随机获取指定序列的指定个元素 sample(self, population, k) (k:获取元素数目,population:指定序列(列表))</code></li><li><code>random.shuffle 随机打乱序列(列表)数据</code></li></ol></li><li><p><strong>pip 内置包管理工具</strong></p><ol><li><code>pip install package (-i 国内源地址 换源)</code></li><li><code>pip uninstall package</code></li><li><code>pip list 展示</code></li><li><code>pip update 更新</code></li><li><code>windows 安装不上部分包</code><ol><li><code>安装 wheel 包 (先支持安装 .whl 包)</code></li><li><code>pip install package.whl</code></li></ol></li><li><code>linux 压缩包安装</code><ol><li><code>解压</code></li><li><code>找到 setup.py</code></li><li><code>python setup.py install 安装包</code></li></ol></li></ol></li><li><p><strong>os 路径</strong></p><ol><li><code>os.system("cmd命令") 调用 cmd 并执行命令</code></li><li><code>os.chdir 修改 python 工作路径 os.chdir("D:\\")</code></li><li><code>os.mkdir 创建文件夹,在当前路径下</code></li><li><code>os.walk(pwd) 返回一个生成器(generator),遍历得到内容</code><ul><li>每次遍历所得到的返回对象都是一个三元组(path, dirs, files) <ul><li>path : 指的是当前正在遍历的这个文件夹的本身路径地址</li><li>dirs : 内容是该文件夹中所有的目录的名字 (不包括子目录),有多个以列表返回</li><li>files : 内容是指该文件夹中所有的文件 (不包括子目录),有多个以列表返回</li></ul></li></ul></li><li><code>os.csandir() 返回文件详细信息,需要遍历读取</code><ul><li>使用 stat() 方法 查看<ul><li>st_size : 文件的体积大小 (单位 :bytes /1024 == KB)</li><li>st_atime : 文件的最近访问时间</li><li>st_mtime : 文件的最近修改时间</li><li>st_ctime : Windows 下表示创建时间</li><li>st_birthtime : 只在 Mac、Linux 下可用,表示创建时间<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">import os</span><br><span class="line"></span><br><span class="line">for file in os.scandir("../"):</span><br><span class="line"> print(file, file.name, file.stat())</span><br><span class="line">"""</span><br><span class="line"><DirEntry '.git'> .git os.stat_result(st_mode=16895, st_ino=0, st_dev=0, st_nlink=0, st_uid=0, st_gid=0, st_size=0, st_atime=1598876157, st_mtime=1598876157, st_ctime=1570419765)</span><br><span class="line"><DirEntry '.idea'> .idea os.stat_result(st_mode=16895, st_ino=0, st_dev=0, st_nlink=0, st_uid=0, st_gid=0, st_size=0, st_atime=1598876405, st_mtime=1598876368, st_ctime=1598661487)</span><br><span class="line">"""</span><br></pre></td></tr></table></figure></li></ul></li></ul></li><li><code>os.path</code><ol><li><code>os.path.isdir 是否是文件夹</code></li><li><code>os.path.getsize 获取文件/夹大小</code></li><li><code>os.path.join('a', 'b') 自动拼接成路径 ==> 'a\\b'</code></li><li><code>os.path.dirname 返回文件目录,可以嵌套一直向上返回目录</code> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">>>> os.path.dirname(os.path.dirname(os.path.abspath('board')))</span><br><span class="line"> 'E:\\python_study\\Django'</span><br></pre></td></tr></table></figure></li><li><code>os.path.abspath 绝对路径</code></li></ol></li></ol></li><li><p><strong>sys 系统</strong></p><ol><li><code>sys.argv 接收调用python脚本时传递的参数,以列表格式,第一个参数是文件本身相对路径</code></li><li><code>sys.exit() 涉及到时间以及内存分配方面释放</code></li></ol></li><li><p><strong>file</strong> :<br> <code>open(file, mode='r', buffering=None, encoding=None, errors=None, newline=None, closefd=True) 读取文件</code></p></li><li><p><strong>WordCloud 词云模块</strong></p> <figure class="highlight plain"><figcaption><span>install wordcloud```</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">1. `常用参数`</span><br><span class="line"> + `width 词云图片宽度,默认400像素`</span><br><span class="line"> + `height 词云图片高度 默认200像素`</span><br><span class="line"> + `background_color 词云图片的背景颜色,默认为黑色`</span><br><span class="line"> + `font_step 字号增大的步进间隔 默认1号`</span><br><span class="line"> + `font_path 指定字体路径 默认None,对于中文可用font_path='msyh.ttc'`</span><br><span class="line"> + `mini_font_size 最小字号 默认4号`</span><br><span class="line"> + `max_font_size 最大字号 根据高度自动调节`</span><br><span class="line"> + `max_words 最大词数 默认200`</span><br><span class="line"> + `stop_words 不显示的单词 stop_words={"python","java"}`</span><br><span class="line"> + `Scale 默认值1。值越大,图像密度越大越清晰`</span><br><span class="line"> + `prefer_horizontal:默认值0.90,浮点数类型。表示在水平如果不合适,就旋转为垂直方向,水平放置的词数占0.9?`</span><br><span class="line"> + `relative_scaling:默认值0.5,浮点型。设定按词频倒序排列,上一个词相对下一位词的大小倍数。有如下取值:“0”表示大小标准只参考频率排名,“1”如果词频是2倍,大小也是2倍`</span><br><span class="line"> + `mask 指定词云形状图片,默认为矩形`</span><br><span class="line"> `通过以下代码读入外部词云形状图片(需要先pip install imageio安装imageio)`</span><br><span class="line"> ````python</span><br><span class="line"> import imageio</span><br><span class="line"> from wordcloud import WordCloud</span><br><span class="line"> mk = imageio.imread("picture.png")</span><br><span class="line"> w = WordCloud(mask=mk)</span><br><span class="line"> `</span><br></pre></td></tr></table></figure></li><li><p><strong>jieba 中文分词</strong></p> <figure class="highlight plain"><figcaption><span>install jieba```</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">1. `方法`</span><br><span class="line"> + `jieba.cut: 返回的结构是一个可迭代的 generator,可以使用 for 循环来获得分词后得到的每一个词语(unicode)` </span><br><span class="line"> + ```cut(self, sentence, cut_all=False, HMM=True, use_paddle=False)</span><br></pre></td></tr></table></figure><pre><code> + `参数` 1. `需要分词的字符串` 2. `cut_all 参数: 用来控制是否采用全模式` 3. `HMM 参数: 用来控制是否使用 HMM 模型` 4. `use_paddle 参数: 用来控制是否使用paddle模式下的分词模式,paddle模式采用延迟加载方式,通过enable_paddle接口安装paddlepaddle-tiny,并且import相关代码`+ `jieba.cut_for_search: 返回的结构是一个可迭代的 generator,可以使用 for 循环来获得分词后得到的每一个词语(unicode)` + ```cut_for_search(self, sentence, HMM=True)``` + `参数` 1. `需要分词的字符串` 2. `是否使用 HMM 模型。该方法适合用于搜索引擎构建倒排索引的分词,粒度比较细`+ `jieba.lcut: 返回list` <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">lcut</span><span class="params">(self, *args, **kwargs)</span>:</span></span><br><span class="line"> <span class="keyword">return</span> list(self.cut(*args, **kwargs))</span><br></pre></td></tr></table></figure>+ `jieba.lcut_for_search: 返回 list` <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">lcut_for_search</span><span class="params">(self, *args, **kwargs)</span>:</span></span><br><span class="line"> <span class="keyword">return</span> list(self.cut_for_search(*args, **kwargs))</span><br></pre></td></tr></table></figure></code></pre></li><li><p><strong>turtle 绘图</strong><br> <code>turtle: 绘图包</code><br> <code>fillcolor:填充颜色</code><br> <code>begin_fill/end_fill 开始/结束填充</code></p><ol><li>设置画布大小<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">turtle.screensize(canvwidth=None,canvheight=None,bg=None),</span><br><span class="line"> 参数分别为画布的宽(单位像素),高,背景颜色。</span><br><span class="line">如: turtle.screensize(800,600,"green")</span><br><span class="line"> turtle.screensize()#返回默认大小(400,300)</span><br></pre></td></tr></table></figure></li><li>画笔(画笔的属性,颜色、画线的宽度等)<ul><li>参数<ul><li><code>turtle.pensize(): 设置画笔的宽度;</code></li><li><code>turtle.pencolor(): 没有参数传入,返回当前画笔颜色,传入参数设置画笔颜色,可以是字符串如"green","red",也可以是RGB3元组。</code></li><li><code>turtle.speed(speed): 设置画笔移动速度,画笔绘制的速度范围[0,10]整数,数字越大越快。</code></li><li><code>turtle.forward(distance): 向当前画笔方向移动distance像素长度</code></li><li><code>turtle.backward(distance): 向当前画笔相反方向移动distance像素长</code>度</li><li><code>turtle.right(degree): 顺时针移动degree°</code></li><li><code>turtle.left(degree): 逆时针移动degree°</code></li><li><code>turtle.pendown(): 移动时绘制图形,缺省时也为绘制</code></li><li><code>turtle.goto(x,y): 将画笔移动到坐标为x,y的位置</code></li><li><code>turtle.penup(): 提起笔移动,不绘制图形,用于另起一个地方绘制</code></li><li><code>turtle.circle(): 画圆,半径为正(负),表示圆心在画笔的左边(右边)画圆</code></li><li><code>setx( ): 将当前x轴移动到指定位置</code></li><li><code>sety( ): 将当前y轴移动到指定位置</code></li><li><code>setheading(angle): 设置当前朝向为angle角度</code></li><li><code>home(): 设置当前画笔位置为原点,朝向东。</code></li><li><code>dot(r): 绘制一个指定直径和颜色的圆点</code></li></ul></li><li>画笔控制命令<ul><li><code>turtle.fillcolor(colorstring): 绘制图形的填充颜色</code></li><li><code>turtle.color(color1, color2): 同时设置pencolor=color1, fillcolor=color2</code></li><li><code>turtle.filling(): 返回当前是否在填充状态</code></li><li><code>turtle.begin_fill(): 准备开始填充图形</code></li><li><code>turtle.end_fill(): 填充完成</code></li><li><code>turtle.hideturtle(): 隐藏画笔的turtle形状</code></li><li><code>turtle.showturtle(): 显示画笔的turtle形状</code></li></ul></li><li>全局控制命令<ul><li><code>turtle.clear(): 清空turtle窗口,但是turtle的位置和状态不会改变</code></li><li><code>turtle.reset(): 清空窗口,重置turtle状态为起始状态</code></li><li><code>turtle.undo(): 撤销上一个turtle动作</code></li><li><code>turtle.isvisible(): 返回当前turtle是否可见</code></li><li><code>stamp(): 复制当前图形</code></li><li><code>turtle.write(s [,font=("font-name",font_size,"font_type")]): 写文本,s为文本内容,font是字体的参数,分别为字体名称,大小和类型;font为可选项,font参数也是可选项</code></li><li><code>turtle.delay(delay=None): 设置或返回以毫秒为单位的绘图延迟</code></li></ul></li></ul></li></ol></li><li><p><strong>MyQR 二维码生成器</strong> </p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">from MyQR import myqr</span><br><span class="line">import os</span><br><span class="line">myqr.run(</span><br><span class="line"> word = '', # 可以是字符串也可以是网址 (网址前加 http(s)://)</span><br><span class="line"> version = 1, # 容错率等级 1 为最高</span><br><span class="line"> level = 'H', # 控制纠错水平,范围是 L, M, Q, H,从左向右依次增高</span><br><span class="line"> picture = '', # 二维码和图片合成,二维码路径</span><br><span class="line"> colorized = 'True', # 是否为彩色二维码</span><br><span class="line"> contrast = 1.0, # 调节图片对比度, 1.0 表示原始图片,更小的值表示更低对比度,更大反之,1.0 为默认</span><br><span class="line"> brightness = 1.0, # 调节图片亮度,越大越亮,越小越暗</span><br><span class="line"> save_name = '', # 生成二维码图片名称,可以是 jpg, png, bmp, gif</span><br><span class="line"> save_dir = os.getcwd() # 控制生成位置</span><br><span class="line"> )</span><br></pre></td></tr></table></figure></li><li><p><strong>JSON</strong></p><ol><li><h5 id="json-dumps-将-Python-对象编码成-JSON-字符串-将json格式的字符串-str-转换为字典类型-dict-的数据"><a href="#json-dumps-将-Python-对象编码成-JSON-字符串-将json格式的字符串-str-转换为字典类型-dict-的数据" class="headerlink" title="json.dumps() 将 Python 对象编码成 JSON 字符串,将json格式的字符串(str)转换为字典类型(dict)的数据"></a>json.dumps() <code>将 Python 对象编码成 JSON 字符串,将json格式的字符串(str)转换为字典类型(dict)的数据</code></h5></li><li><h5 id="json-loads-将已编码的-JSON-字符串解码为-Python-对象-返回来,是将字典类型-dict-的数据转换成json格式的字符串"><a href="#json-loads-将已编码的-JSON-字符串解码为-Python-对象-返回来,是将字典类型-dict-的数据转换成json格式的字符串" class="headerlink" title="json.loads() 将已编码的 JSON 字符串解码为 Python 对象,返回来,是将字典类型(dict)的数据转换成json格式的字符串"></a>json.loads() <code>将已编码的 JSON 字符串解码为 Python 对象,返回来,是将字典类型(dict)的数据转换成json格式的字符串</code></h5></li><li><h5 id="json-load-用于读取json格式的文件,将文件中的数据转换为字典类型-dict"><a href="#json-load-用于读取json格式的文件,将文件中的数据转换为字典类型-dict" class="headerlink" title="json.load() 用于读取json格式的文件,将文件中的数据转换为字典类型(dict)"></a>json.load() <code>用于读取json格式的文件,将文件中的数据转换为字典类型(dict)</code></h5></li><li><h5 id="json-dump-主要用于存入json格式的文件,将字典类型转换为json形式的字符串"><a href="#json-dump-主要用于存入json格式的文件,将字典类型转换为json形式的字符串" class="headerlink" title="json.dump() 主要用于存入json格式的文件,将字典类型转换为json形式的字符串"></a>json.dump() <code>主要用于存入json格式的文件,将字典类型转换为json形式的字符串</code></h5><p> eg: </p><ol><li><code>将 python 数据结构转换为 JSON</code> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">import json</span><br><span class="line">data = {</span><br><span class="line"> "name": 'ACME',</span><br><span class="line"> "shares": 100,</span><br><span class="line"> "price": 524.24</span><br><span class="line">}</span><br><span class="line">json_str = json.dumps(data)</span><br></pre></td></tr></table></figure></li><li><code>将一个 JSON 编码字符串转换回一个 Python 数据结构</code> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">data = json.loads(json_str)</span><br></pre></td></tr></table></figure></li><li><code>编码解码 JSON 数据</code> <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"># 写入一个 json 数据</span><br><span class="line">with open("data.json", 'w') as f:</span><br><span class="line"> json.dump(data, f)</span><br><span class="line"></span><br><span class="line">with open('data.json', 'r') as f:</span><br><span class="line"> data = json.load(f)</span><br></pre></td></tr></table></figure></li></ol></li></ol></li><li><p><strong>Requests</strong><br> <code>import requests</code></p><ol><li><p>请求</p><ol><li><p>请求方式</p><ul><li><p><strong>GET</strong></p><p> <code>requests.get(url)</code></p><ul><li>传递 URL 参数 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">payload = {'key1': 'value1', 'key2': ['value2', 'value3']}</span><br><span class="line">r = requests.get("http://httpbin.org/get", params=payload)</span><br><span class="line"># http://httpbin.org/get?key1=value1&key2=value2&key2=value3</span><br></pre></td></tr></table></figure></li></ul></li><li><p><strong>POST</strong></p><p> <code>r = requests.post(url, data={"key": "value"}</code></p><ul><li>传递 data 参数 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">payload = {'key1': 'value1', 'key2': 'value2'}</span><br><span class="line">r = requests.post("http://httpbin.org/post", data=payload)</span><br><span class="line">print(r.text)</span><br><span class="line">{</span><br><span class="line"> ...</span><br><span class="line"> "form": {</span><br><span class="line"> "key2": "value2",</span><br><span class="line"> "key1": "value1"</span><br><span class="line"> },</span><br><span class="line"> ...</span><br><span class="line">}</span><br></pre></td></tr></table></figure></li></ul></li><li><p><strong>PUT</strong></p><p> <code>r = requests.put(url, data={"key": "value"})</code></p></li><li><p><strong>DELETE</strong></p><p> <code>r = requests.delete(url)</code></p></li><li><p><strong>HEAD</strong></p><p> <code>r = requests.head(url)</code></p></li><li><p><strong>OPTIONS</strong></p><p> <code>r = requests.options(url)</code></p></li></ul></li><li><p>请求头</p><ul><li>fake_useragent 模块随机产生请求头 <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"> <span class="keyword">from</span> fake_useragent <span class="keyword">import</span> UserAgent</span><br><span class="line"> headers = {<span class="string">"User-Agent"</span>: str(UserAgent().random)}</span><br><span class="line"> ``` </span><br><span class="line">+ 传递浏览器参数</span><br></pre></td></tr></table></figure>headers = {‘User-Agent’: ‘Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36’}<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">3. Cookie</span><br><span class="line"> + 响应中的 Cookie</span><br><span class="line"> ```python</span><br><span class="line"> import requests</span><br><span class="line"> r = requests.get('https://requests.readthedocs.io/zh_CN/latest/user/quickstart.html')</span><br><span class="line"> print(r.cookies)</span><br><span class="line"> # <RequestsCookieJar[Cookie(version=0, name='__cfduid', value='d22fadb1898ae38e2bc7f811090da41db1595923633', </span><br><span class="line"> # port=None, port_specified=False, domain='.readthedocs.io', domain_specified=True, domain_initial_dot=True, path='/', path_specified=True, secure=False, expires=1598515633, discard=False, comment=None, comment_url=None, rest={'HttpOnly': None, 'SameSite': 'Lax'}, rfc2109=False)]></span><br></pre></td></tr></table></figure></li><li>发送 Cookies<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> requests</span><br><span class="line">url = <span class="string">'https://requests.readthedocs.io/zh_CN/latest/user/quickstart.html'</span></span><br><span class="line">cookies = dict(Cookie=<span class="string">"(version=0, name='__cfduid', value='d22fadb1898ae38e2bc7f811090da41db1595923633', port=None, port_specified=False, domain='.readthedocs.io', domain_specified=True, domain_initial_dot=True, path='/', path_specified=True, secure=False, expires=1598515633, discard=False, comment=None, comment_url=None, rest={'HttpOnly': None, 'SameSite': 'Lax'})"</span>)</span><br></pre></td></tr></table></figure></li></ul></li></ol></li><li><p>响应</p><ul><li><code>响应状态码 r.status_code</code></li><li><code>响应头 r.headers</code></li></ul><ol><li><p>二进制响应内容 r.content (对于非文本请求)</p><p> Requests 会自动为你解码 gzip 和 deflate 传输编码的响应数据。</p> <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 以请求返回的二进制数据创建一张图片</span></span><br><span class="line"><span class="keyword">import</span> requests</span><br><span class="line"><span class="keyword">from</span> PIL <span class="keyword">import</span> Image</span><br><span class="line"><span class="keyword">from</span> io <span class="keyword">import</span> BytesIO</span><br><span class="line">r = requests.get(url=<span class="string">''</span>)</span><br><span class="line">i = Image.open(BytesIO(r.content))</span><br></pre></td></tr></table></figure></li><li><p>JSON 响应内容 r.json</p> <figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"> <span class="keyword">import</span> requests</span><br><span class="line"> r = requests.get(url=<span class="string">''</span>)</span><br><span class="line"> r.json()</span><br><span class="line"> ``` </span><br><span class="line"><span class="number">3.</span> 原始响应内容(原始套接字响应) r.raw</span><br><span class="line"> ```python</span><br><span class="line"> <span class="keyword">import</span> requests</span><br><span class="line"> r = requests.get(url=<span class="string">''</span>, stream=<span class="literal">True</span>)</span><br><span class="line"> print(r.raw)</span><br><span class="line"> <span class="comment"># <requests.packages.urllib3.response.HTTPResponse object at 0x101194810></span></span><br><span class="line"> print(r.raw.read(<span class="number">10</span>))</span><br><span class="line"> <span class="comment"># '\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03'</span></span><br></pre></td></tr></table></figure></li></ol></li></ol></li></ol><ol start="12"><li><p>Socket() 模块</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">socket.socket(socket_family, socket_type, protocol=0)</span><br><span class="line">socket_family: socket.AF_UNIX 或 socket.AF_INET</span><br><span class="line">socket_type: socket.SOCK_STREAM (TCP) 或 socket.SOCK_DGRAM (UDP)</span><br><span class="line">protocol: 通常省略,默认为 0</span><br></pre></td></tr></table></figure><p>TCP/IP 套接字 tcpSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)<br>UDP/IP 套接字 udpSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)<br>{如果使用 from socket import * 大大缩短代码 tcpSocket = socket(AF_INET, SOCK_STREAM)}</p><ul><li>套接字内置方法<ol><li>服务器套接字<ul><li><code>s.bind()</code> 将地址 (主机名,端口号对) 绑定到套接字</li><li><code>s.listen()</code> 设置并启动 TCP 监听器</li><li><code>s.accept()</code> 被动接受 TCP 客户端连接,一直等待直到连接到达 (阻塞)</li></ul></li><li>客户端套接字<ul><li><code>s.connect()</code> 主动发起 TCP 服务器连接</li><li><code>s.connect_ex()</code> 类似 connect ,但以错误码形式返回问题,不是抛出异常</li></ul></li><li>普通套接字方法<ul><li><code>s.recv()</code> 接收 TCP 消息</li><li><code>s.recv_into()</code> 接收 TCP 消息到指定缓冲区</li><li><code>s.send()</code> 发送 TCP 消息</li><li><code>s.sendall()</code> 完整地发送 TCP 消息</li><li><code>s.recvfrom()</code> 接收 UDP 消息</li><li><code>s.recvfrom_into()</code> 接收 UDP 消息到指定缓冲区</li><li><code>s.sendto()</code> 发送 UDP 消息</li><li><code>s.getpeername()</code> 连接到套接字 (TCP) 的远程地址</li><li><code>s.getsockname()</code> 当前套接字的地址</li><li><code>s.getsockopt()</code> 返回给定套接字选项的值</li><li><code>s.setsockopt()</code> 设置给定套接字选项的值</li><li><code>s.shutdown()</code> 关闭连接</li><li><code>s.close()</code> 关闭套接字</li><li><code>s.detach()</code> 在未关闭文件描述的情况下关闭套接字,返回文件描述符</li><li><code>s.ioctl()</code> 控制套接字的模式 (仅支持 Windows)</li></ul></li><li>面向阻塞的套接字方法<ul><li><code>s.setblocking()</code> 设置套接字的阻塞或非阻塞模式</li><li><code>s.settimeout()</code> 设置阻塞套接字操作的超时时间</li><li><code>s.gettimeout()</code> 或去阻塞套接字操作的超时时间</li></ul></li><li>面向文件的套接字方法<ul><li><code>s.fileno()</code> 套接字的文件描述符</li><li><code>s.makefile()</code> 创建与套接字关联的文件对象</li></ul></li><li>数据属性<ul><li><code>s.family</code> 套接字家族</li><li><code>s.type</code> 套接字类型</li><li><code>s.proto</code> 套接字协议</li></ul></li></ol></li></ul></li><li><p>glob 模块 (采取类似于“正则”的方式,进行文件匹配)</p><table><thead><tr><th align="center">模式</th><th align="center">意义</th></tr></thead><tbody><tr><td align="center">*</td><td align="center">匹配所有</td></tr><tr><td align="center">?</td><td align="center">匹配任何单个字符</td></tr><tr><td align="center">[seq]</td><td align="center">匹配 seq 中的任何字符</td></tr><tr><td align="center">[!seq]</td><td align="center">匹配任何不在 seq 中的字符</td></tr></tbody></table><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">import glob</span><br><span class="line"></span><br><span class="line">for i in glob.glob("../*.txt"):</span><br><span class="line"> print(i)</span><br><span class="line">"""</span><br><span class="line">..\Django_note.txt</span><br><span class="line">..\Email_Token.txt</span><br><span class="line">..\jQuery_note.txt</span><br><span class="line">"""</span><br><span class="line">for i in glob.glob("../*a*.txt"):</span><br><span class="line"> print(i)</span><br><span class="line">"""</span><br><span class="line">..\Django_note.txt</span><br><span class="line">..\Email_Token.txt</span><br><span class="line">"""</span><br></pre></td></tr></table></figure></li><li><p>shutil 模块 (复制、移动文件及文件夹) <code>import shutil</code></p><ol><li><p><code>shutil.copy("要复制的文件", "要复制到的位置")</code> 复制文件 </p></li><li><p><code>shutil.copytree("要复制的文件夹", "要复制到的新文件夹的位置")</code> 复制文件夹,相当于重命名文件夹</p><ul><li>将某个文件夹移动到另外一个文件夹(该文件夹必须是新文件夹),不能是已经存在了的文件夹;</li></ul></li><li><p><code>shutil.move("要移动的文件/文件夹", "要移动到的位置")</code> 移动文件/文件夹</p><ul><li>文件夹或者文件被移动后,原始文件就没有了</li></ul></li><li><p><code>shutil.rmtree("要删除的文件夹")</code> 删除文件夹</p></li></ol></li></ol><ol start="15"><li>创建和解压压缩包 (.zip 格式) <code>import zipfile</code></li></ol><pre><code>1. 读取压缩包内文件 [有时候需要在 filename.encoding("cp437").decode("utf-8")] <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">import zipfile</span><br><span class="line"></span><br><span class="line">with zipfile.ZipFile("../ziji.zip", "r") as zipobj:</span><br><span class="line"> print(zipobj.namelist())</span><br><span class="line">"""</span><br><span class="line">['1.gif', '__pycache__/', '__pycache__/taichi.cpython-37.pyc', 'build/',]</span><br><span class="line">"""</span><br><span class="line">with zipfile.ZipFile("../ziji.zip", "r") as zipobj:</span><br><span class="line"> for filename in zipobj.namelist():</span><br><span class="line"> print(filename.encode("gbk").decode("gbk"))</span><br></pre></td></tr></table></figure>2. 读取压缩包内的文件信息 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">import zipfile</span><br><span class="line"></span><br><span class="line">with zipfile.ZipFile("../ziji.zip", "r") as zipobj:</span><br><span class="line"> for filename in zipobj.namelist():</span><br><span class="line"> info = zipobj.getinfo(filename)</span><br><span class="line"> new_filename = filename.encode("cp437").decode("utf-8")</span><br><span class="line"> print(new_filename, info.file_size/1024/1024, info.compress_size/1024/1024)</span><br><span class="line">"""</span><br><span class="line">1.gif 0.3085899353027344 0.3054962158203125</span><br><span class="line">build/taichi/Analysis-00.toc 0.1259927749633789 0.011130332946777344</span><br><span class="line">"""</span><br></pre></td></tr></table></figure>3. 解压压缩包 + `extract("压缩包内要解压的文件名", "要解压到的位置")` : 将压缩包内单个文件解压出来 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">import zipfile</span><br><span class="line"> </span><br><span class="line">with zipfile.ZipFile("../ziji.zip", "r") as zipobj:</span><br><span class="line"> zipobj.extract("1.gif", "./")</span><br></pre></td></tr></table></figure> + `extractall("解压到的位置")` : 将压缩包内所有文件解压出来4. 创建压缩包 + 对某些文件,创建压缩包 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">import zipfile</span><br><span class="line"></span><br><span class="line">file_list = ["practice_16_20.py", "Practice_21-24.py"]</span><br><span class="line">with zipfile.ZipFile(r".测试用压缩包.zip", "w") as zipobj:</span><br><span class="line"> for file in file_list:</span><br><span class="line"> zipobj.write(file)</span><br></pre></td></tr></table></figure> + 向已存在压缩包内添加文件 <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">import zipfile</span><br><span class="line"></span><br><span class="line">file_list = ["test1.txt", "test2.py"]</span><br><span class="line">with zipfile.ZipFile(r".测试用压缩包.zip", "a") as zipobj:</span><br><span class="line"> for file in file_list:</span><br><span class="line"> zipobj.write(file)</span><br></pre></td></tr></table></figure></code></pre>]]></content>
<tags>
<tag> Python 模块 </tag>
</tags>
</entry>
<entry>
<title>Hello World</title>
<link href="/2020/07/26/hello-world/"/>
<url>/2020/07/26/hello-world/</url>
<content type="html"><![CDATA[<p>Welcome to <a href="https://hexo.io/" target="_blank" rel="noopener">Hexo</a>! This is your very first post. Check <a href="https://hexo.io/docs/" target="_blank" rel="noopener">documentation</a> for more info. If you get any problems when using Hexo, you can find the answer in <a href="https://hexo.io/docs/troubleshooting.html" target="_blank" rel="noopener">troubleshooting</a> or you can ask me on <a href="https://github.com/hexojs/hexo/issues" target="_blank" rel="noopener">GitHub</a>.</p><h2 id="Quick-Start"><a href="#Quick-Start" class="headerlink" title="Quick Start"></a>Quick Start</h2><h3 id="Create-a-new-post"><a href="#Create-a-new-post" class="headerlink" title="Create a new post"></a>Create a new post</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo new <span class="string">"My New Post"</span></span><br></pre></td></tr></table></figure><p>More info: <a href="https://hexo.io/docs/writing.html" target="_blank" rel="noopener">Writing</a></p><h3 id="Run-server"><a href="#Run-server" class="headerlink" title="Run server"></a>Run server</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo server</span><br></pre></td></tr></table></figure><p>More info: <a href="https://hexo.io/docs/server.html" target="_blank" rel="noopener">Server</a></p><h3 id="Generate-static-files"><a href="#Generate-static-files" class="headerlink" title="Generate static files"></a>Generate static files</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo generate</span><br></pre></td></tr></table></figure><p>More info: <a href="https://hexo.io/docs/generating.html" target="_blank" rel="noopener">Generating</a></p><h3 id="Deploy-to-remote-sites"><a href="#Deploy-to-remote-sites" class="headerlink" title="Deploy to remote sites"></a>Deploy to remote sites</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo deploy</span><br></pre></td></tr></table></figure><p>More info: <a href="https://hexo.io/docs/one-command-deployment.html" target="_blank" rel="noopener">Deployment</a></p>]]></content>
</entry>
</search>