python include标签如何使用?

在讲python include标签使用之前,我们新建一个include_demo项目,截图如下:

项目新建好了,再在templates文件下新建一个index.html文件,代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        *{margin:0;padding:0;}
        ul{list-style: none;}
        a{text-decoration: none;
            color: #ffffff;}
        nav,footer,.main{width:1000px; height:40px; margin:0 auto;}
        ul{width: 1000px; height:40px; line-height: 40px;
            background-color:#000000;}
        li{width:120px; height:40px; line-height:40px; text-align: center;
        float:left;}
        .main{clear:both; line-height:40px; background-color:pink;}
        footer{height:40px;  background-color: green;}
    </style>
</head>
<body>

        <nav>
            <ul>
                <li><a href="#">首页</a></li>
                <li><a href="#">关于我们</a></li>
                <li><a href="#">产品中心</a></li>
                <li><a href="#">新闻中心</a></li>
                <li><a href="#">服务宗旨</a></li>
                <li><a href="#">联系我们</a></li>
            </ul>
        </nav>
        <div class="main">
            网站首页主体部分
        </div>
       <footer>
            网站首页footer部分
       </footer>

</body>
</html>

然后在include_demo.py页面渲染一下index模板文件,代码如下:

from flask import Flask,render_template

app = Flask(__name__)


@app.route('/')
def hello_world():
    return render_template("index.html")


if __name__ == '__main__':
    app.run(debug=True)

运行include_demo.py文件,运行结果如下:

在这里主要是为了方便讲解include标签,所有没太注重前端页面部分。

通过上面index.html文件就能发现,我将公共和私有代码部分都在一块,假设网站有几十个页面,我将所有公共代码和私有代码

都放一块,如果有一天要修改某个公共代码块,哪就得修改几十个页面,哪将是件非常蛋疼的事。为了方便管理项目,我们将页面公共、私有代码部分抽取出来。

我们新建一个header.html文件,把css样式及nav标签内容复制到header.html页面中。代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        *{margin:0;padding:0;}
        ul{list-style: none;}
        a{text-decoration: none;
            color: #ffffff;}
        nav,footer,.main{width:1000px; height:40px; margin:0 auto;}
        ul{width: 1000px; height:40px; line-height: 40px;
            background-color:#000000;}
        li{width:120px; height:40px; line-height:40px; text-align: center;
        float:left;}
        .main{clear:both; line-height:40px; background-color:pink;}
        footer{height:40px;  background-color: green;}
    </style>
</head>
<body>
      <nav>
            <ul>
                <li><a href="#">首页</a></li>
                <li><a href="#">关于我们</a></li>
                <li><a href="#">产品中心</a></li>
                <li><a href="#">新闻中心</a></li>
                <li><a href="#">服务宗旨</a></li>
                <li><a href="#">联系我们</a></li>
            </ul>
        </nav>
</body>
</html>

然后新建一个footer.html文件,把footer标签中的内容复制到该文件中。代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
     <footer>
            网站首页footer部分
     </footer>
     </footer>
</body>
</html>

我们在运行主app文件,结果如下:

(^-^),为啥没有居中,背景色也不见了??因为我们没有把样式引入进来(嗯,页面太丑了,没法看了,赶紧关了!!)

OK!我们将公共代码抽取出来后。记得在index.html文件中用include标签导入header、footer代码块,代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
   {% include "header.html" %}
        <div class="main">
            网站首页主体部分
        </div>
    {% include "footer.html" %}
</body>
</html>

再运行主app文件,结果如下:

嗯,结果是不是和之前一样,对吧!

通过上面例子,相信大部分小伙伴都明白了include标签的作用及用法。总之一句话,include标签的作用就相当于把抽取的代码复制到当前页面中。

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以回答这个问题。标签平滑是一种用于降低模型过度自信的技术。它通过将标签分布从 0 或 1 调整到介于 0 和 1 之间的值来实现。这样可以使模型更加谦虚,从而提高泛化能力。 在 Python 中,可以使用 NumPy 库来实现标签平滑。以下是一个简单的示例代码: ```python import numpy as np def smooth_labels(labels, factor=0.1): """ 标签平滑函数 :param labels: 原始标签 :param factor: 平滑因子 :return: 平滑后的标签 """ smooth_labels = (1 - factor) * labels + factor / labels.shape[1] return smooth_labels ``` 在 Caffe 中,可以使用 SoftmaxWithLoss 层来实现标签平滑。以下是一个示例网络结构: ```protobuf layer { name: "data" type: "Data" top: "data" include { phase: TRAIN } transform_param { mirror: true crop_size: 227 mean_value: 104 mean_value: 117 mean_value: 123 } data_param { source: "train_lmdb" batch_size: 64 backend: LMDB } } layer { name: "label_smoothing" type: "Python" bottom: "label" top: "label_smooth" python_param { module: "python_layers" layer: "LabelSmoothingLayer" param_str: "'factor': 0.1" } } layer { name: "fc1" type: "InnerProduct" bottom: "data" bottom: "label_smooth" top: "fc1" inner_product_param { num_output: 1000 weight_filler { type: "xavier" } bias_filler { type: "constant" value: 0 } } } layer { name: "loss" type: "SoftmaxWithLoss" bottom: "fc1" bottom: "label_smooth" top: "loss" } ``` 在这个网络中,我们使用 Python 层来实现标签平滑。LabelSmoothingLayer 的实现类似于上面的 Python 示例。然后,我们将平滑后的标签传递给 InnerProduct 层和 SoftmaxWithLoss 层。这样,我们就可以使用标签平滑来训练我们的模型了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值