使用flask框架开发web网站《情话王子》

简介: 使用flask框架开发web网站《情话王子》

不会跟女神聊天?今天用Python开发一个web网站

实现情话自由,让你没有把持不住的妹~

这里使用flask框架进行快速开发。

Flask简介


 Flask是一个轻量级的可定制框架,使用Python语言编写,较其他同类型框架更为灵活、轻便、安全且容易上手。它可以很好地结合MVC模式进行开发,开发人员分工合作,小型团队在短时间内就可以完成功能丰富的中小型网站或Web服务的实现。另外,Flask还有很强的定制性,用户可以根据自己的需求来添加相应的功能,在保持核心功能简单的同时实现功能的丰富与扩展,其强大的插件库可以让用户实现个性化的网站定制,开发出功能强大的网站。

 

 

环境准备

安装第三方库

  • 1. 安装flask


pip install flask


2. 安装requests


pip install requests


 

实战开发

 

后端代码


导入


from flask import Flask, render_template, request, redirect
import time, requests, re


实例化flask对象


app = Flask(__name__)


注册路由,并绑定路由地址处理函数


@app.route('/', methods=['POST', 'GET'])
def index():
    date = time.strftime('%H:%M:%S', time.localtime())
    url = 'https://api.xcboke.cn/api/yulu?c=1001&encode=text'
    text = requests.get(url).text
    text = re.split('\s+|<br>', text)
    texts = [re.sub('“|"|”|', '', x) for x in text]
    print(texts)
    text_list.append(texts)
    size = len(texts)
    return render_template('index.html', date=date, data=text_list, size=size)


ps:这里使用了api接口来获取情话,将请求结果用正则来分隔出聊天对话,并使用列表推导式将其中的不规则数据进行校正,形成数据集,并将它添加进列表当中。并列表数据传输给前端用于显示。



 

前端部分


我们在项目中建立如下的目录结构:



css样式表1:reset.min.css:


html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, b, u, i, center, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td, article, aside, canvas, details, embed, figure, figcaption, footer, header, hgroup, menu, nav, output, ruby, section, summary, time, mark, audio, video {
    margin: 0;
    padding: 0;
    border: 0;
    font-size: 100%;
    font: inherit;
    vertical-align: baseline
}
article, aside, details, figcaption, figure, footer, header, hgroup, menu, nav, section {
    display: block
}
body {
    line-height: 1
}
ol, ul {
    list-style: none
}
blockquote, q {
    quotes: none
}
blockquote:before, blockquote:after, q:before, q:after {
    content: '';
    content: none
}
table {
    border-collapse: collapse;
    border-spacing: 0
}


css样式表2 : style.css


*, *:before, *:after {
    box-sizing: border-box;
}
:root {
    --white: #fff;
    --black: #000;
    --bg: #f8f8f8;
    --grey: #999;
    --dark: #1a1a1a;
    --light: #e6e6e6;
    --wrapper: 1000px;
    --blue: #00b0ff;
}
body {
    background-color: var(--bg);
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    text-rendering: optimizeLegibility;
    font-family: 'Source Sans Pro', sans-serif;
    font-weight: 400;
    background-image: url("https://img1.baidu.com/it/u=3294168837,435411012&fm=253&fmt=auto&app=138&f=JPEG?w=400&h=400");
    background-size: cover;
    background-repeat: none;
}
.wrapper {
    position: relative;
    left: 50%;
    width: var(--wrapper);
    height: 800px;
    -webkit-transform: translate(-50%, 0);
    transform: translate(-50%, 0);
}
.container {
    position: relative;
    top: 50%;
    left: 50%;
    width: 80%;
    height: 75%;
    background-color: var(--white);
    -webkit-transform: translate(-50%, -50%);
    transform: translate(-50%, -50%);
}
.container .left {
    float: left;
    width: 37.6%;
    height: 100%;
    border: 1px solid var(--light);
    background-color: var(--white);
}
.container .left .top {
    position: relative;
    width: 100%;
    height: 96px;
    padding: 29px;
}
.container .left .top:after {
    position: absolute;
    bottom: 0;
    left: 50%;
    display: block;
    width: 80%;
    height: 1px;
    content: '';
    background-color: var(--light);
    -webkit-transform: translate(-50%, 0);
    transform: translate(-50%, 0);
}
.container .left input {
    float: left;
    width: 188px;
    height: 42px;
    padding: 0 15px;
    border: 1px solid var(--light);
    background-color: #eceff1;
    border-radius: 21px;
    font-family: 'Source Sans Pro', sans-serif;
    font-weight: 400;
}
.container .left input:focus {
    outline: none;
}
.container .left a.search {
    display: block;
    float: left;
    width: 42px;
    height: 42px;
    margin-left: 10px;
    border: 1px solid var(--light);
    background-color: var(--blue);
    background-image: url("https://s3-us-west-2.amazonaws.com/s.cdpn.io/382994/name-type.png");
    background-repeat: no-repeat;
    background-position: top 12px left 14px;
    border-radius: 50%;
}
.container .left .people {
    margin-left: -1px;
    border-right: 1px solid var(--light);
    border-left: 1px solid var(--light);
    width: calc(100% + 2px);
}
.container .left .people .person {
    position: relative;
    width: 100%;
    padding: 12px 10% 16px;
    cursor: pointer;
    background-color: var(--white);
}
.container .left .people .person:after {
    position: absolute;
    bottom: 0;
    left: 50%;
    display: block;
    width: 80%;
    height: 1px;
    content: '';
    background-color: var(--light);
    -webkit-transform: translate(-50%, 0);
    transform: translate(-50%, 0);
}
.container .left .people .person img {
    float: left;
    width: 40px;
    height: 40px;
    margin-right: 12px;
    border-radius: 50%;
}
.container .left .people .person .name {
    font-size: 14px;
    line-height: 22px;
    color: var(--dark);
    font-family: 'Source Sans Pro', sans-serif;
    font-weight: 600;
}
.container .left .people .person .time {
    font-size: 14px;
    position: absolute;
    top: 16px;
    right: 10%;
    padding: 0 0 5px 5px;
    color: var(--grey);
    background-color: var(--white);
}
.container .left .people .person .preview {
    font-size: 14px;
    display: inline-block;
    overflow: hidden !important;
    width: 70%;
    white-space: nowrap;
    text-overflow: ellipsis;
    color: var(--grey);
}
.container .left .people .person.active, .container .left .people .person:hover {
    margin-top: -1px;
    margin-left: -1px;
    padding-top: 13px;
    border: 0;
    background-color: var(--blue);
    width: calc(100% + 2px);
    padding-left: calc(10% + 1px);
}
.container .left .people .person.active span, .container .left .people .person:hover span {
    color: var(--white);
    background: transparent;
}
.container .left .people .person.active:after, .container .left .people .person:hover:after {
    display: none;
}
.container .right {
    position: relative;
    float: left;
    width: 62.4%;
    height: 100%;
}
.container .right .top {
    width: 100%;
    height: 47px;
    padding: 15px 29px;
    background-color: #eceff1;
}
.container .right .top span {
    font-size: 15px;
    color: var(--grey);
}
.container .right .top span .name {
    color: var(--dark);
    font-family: 'Source Sans Pro', sans-serif;
    font-weight: 600;
}
.container .right .chat {
    position: relative;
    display: none;
    overflow: hidden;
    padding: 0 35px 92px;
    border-width: 1px 1px 1px 0;
    border-style: solid;
    border-color: var(--light);
    height: calc(100% - 48px);
    justify-content: flex-end;
    flex-direction: column;
}
.container .right .chat.active-chat {
    display: block;
    display: flex;
}
.container .right .chat.active-chat .bubble {
    transition-timing-function: cubic-bezier(0.4, -0.04, 1, 1);
}
.container .right .chat.active-chat .bubble:nth-of-type(1) {
    -webkit-animation-duration: 0.15s;
    animation-duration: 0.15s;
}
.container .right .chat.active-chat .bubble:nth-of-type(2) {
    -webkit-animation-duration: 0.3s;
    animation-duration: 0.3s;
}
.container .right .chat.active-chat .bubble:nth-of-type(3) {
    -webkit-animation-duration: 0.45s;
    animation-duration: 0.45s;
}
.container .right .chat.active-chat .bubble:nth-of-type(4) {
    -webkit-animation-duration: 0.6s;
    animation-duration: 0.6s;
}
.container .right .chat.active-chat .bubble:nth-of-type(5) {
    -webkit-animation-duration: 0.75s;
    animation-duration: 0.75s;
}
.container .right .chat.active-chat .bubble:nth-of-type(6) {
    -webkit-animation-duration: 0.9s;
    animation-duration: 0.9s;
}
.container .right .chat.active-chat .bubble:nth-of-type(7) {
    -webkit-animation-duration: 1.05s;
    animation-duration: 1.05s;
}
.container .right .chat.active-chat .bubble:nth-of-type(8) {
    -webkit-animation-duration: 1.2s;
    animation-duration: 1.2s;
}
.container .right .chat.active-chat .bubble:nth-of-type(9) {
    -webkit-animation-duration: 1.35s;
    animation-duration: 1.35s;
}
.container .right .chat.active-chat .bubble:nth-of-type(10) {
    -webkit-animation-duration: 1.5s;
    animation-duration: 1.5s;
}
.container .right .write {
    position: absolute;
    bottom: 29px;
    left: 30px;
    height: 42px;
    padding-left: 8px;
    border: 1px solid var(--light);
    background-color: #eceff1;
    width: calc(100% - 58px);
    border-radius: 5px;
}
.container .right .write input {
    font-size: 16px;
    float: left;
    width: 347px;
    height: 40px;
    padding: 0 10px;
    color: var(--dark);
    border: 0;
    outline: none;
    background-color: #eceff1;
    font-family: 'Source Sans Pro', sans-serif;
    font-weight: 400;
}
.container .right .write .write-link.attach:before {
    display: inline-block;
    float: left;
    width: 20px;
    height: 42px;
    content: '';
    background-image: url("https://s3-us-west-2.amazonaws.com/s.cdpn.io/382994/attachment.png");
    background-repeat: no-repeat;
    background-position: center;
}
.container .right .write .write-link.smiley:before {
    display: inline-block;
    float: left;
    width: 20px;
    height: 42px;
    content: '';
    background-image: url("https://s3-us-west-2.amazonaws.com/s.cdpn.io/382994/smiley.png");
    background-repeat: no-repeat;
    background-position: center;
}
.container .right .write .write-link.send:before {
    display: inline-block;
    float: left;
    width: 20px;
    height: 42px;
    margin-left: 11px;
    content: '';
    background-image: url("https://s3-us-west-2.amazonaws.com/s.cdpn.io/382994/send.png");
    background-repeat: no-repeat;
    background-position: center;
}
.container .right .bubble {
    font-size: 16px;
    position: relative;
    display: inline-block;
    clear: both;
    margin-bottom: 8px;
    padding: 13px 14px;
    vertical-align: top;
    border-radius: 5px;
}
.container .right .bubble:before {
    position: absolute;
    top: 19px;
    display: block;
    width: 8px;
    height: 6px;
    content: '\00a0';
    -webkit-transform: rotate(29deg) skew(-35deg);
    transform: rotate(29deg) skew(-35deg);
}
.container .right .bubble.you {
    float: left;
    color: var(--white);
    background-color: var(--blue);
    align-self: flex-start;
    -webkit-animation-name: slideFromLeft;
    animation-name: slideFromLeft;
}
.container .right .bubble.you:before {
    left: -3px;
    background-color: var(--blue);
}
.container .right .bubble.me {
    float: right;
    color: var(--dark);
    background-color: #eceff1;
    align-self: flex-end;
    -webkit-animation-name: slideFromRight;
    animation-name: slideFromRight;
}
.container .right .bubble.me:before {
    right: -3px;
    background-color: #eceff1;
}
.container .right .conversation-start {
    position: relative;
    width: 100%;
    margin-bottom: 27px;
    text-align: center;
}
.container .right .conversation-start span {
    font-size: 14px;
    display: inline-block;
    color: var(--grey);
}
.container .right .conversation-start span:before, .container .right .conversation-start span:after {
    position: absolute;
    top: 10px;
    display: inline-block;
    width: 30%;
    height: 1px;
    content: '';
    background-color: var(--light);
}
.container .right .conversation-start span:before {
    left: 0;
}
.container .right .conversation-start span:after {
    right: 0;
}
@keyframes slideFromLeft {
    0% {
        margin-left: -200px;
        opacity: 0;
    }
    100% {
        margin-left: 0;
        opacity: 1;
    }
}
@-webkit-keyframes slideFromLeft {
    0% {
        margin-left: -200px;
        opacity: 0;
    }
    100% {
        margin-left: 0;
        opacity: 1;
    }
}
@keyframes slideFromRight {
    0% {
        margin-right: -200px;
        opacity: 0;
    }
    100% {
        margin-right: 0;
        opacity: 1;
    }
}
@-webkit-keyframes slideFromRight {
    0% {
        margin-right: -200px;
        opacity: 0;
    }
    100% {
        margin-right: 0;
        opacity: 1;
    }
}


js处理代码:


document.querySelector('.chat[data-chat=person2]').classList.add('active-chat');
document.querySelector('.person[data-chat=person2]').classList.add('active');
var friends = {
        list: document.querySelector('ul.people'),
        all: document.querySelectorAll('.left .person'),
        name: ''
    },
    chat = {
        container: document.querySelector('.container .right'),
        current: null,
        person: null,
        name: document.querySelector('.container .right .top .name')
    };
friends.all.forEach(function (f) {
    f.addEventListener('mousedown', function () {
        f.classList.contains('active') || setAciveChat(f);
    });
});
function setAciveChat(f) {
    friends.list.querySelector('.active').classList.remove('active');
    f.classList.add('active');
    chat.current = chat.container.querySelector('.active-chat');
    chat.person = f.getAttribute('data-chat');
    chat.current.classList.remove('active-chat');
    chat.container.querySelector('[data-chat="' + chat.person + '"]').classList.add('active-chat');
    friends.name = f.querySelector('.name').innerText;
    chat.name.innerHTML = friends.name;
}


html模板


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>情话王子</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="../static/css/reset.min.css">
    <link rel="stylesheet" href="../static/css/style.css">
</head>
<body>
<div class="wrapper">
    <div class="container">
        <div class="left">
            <div class="top">
                <input type="text" placeholder="搜索"/>
                <a href="javascript:;" class="search"></a>
            </div>
            <ul class="people">
                <li class="person" data-chat="person1">
                    <img src="../static/imgs/thomas.jpg" alt=""/>
                    <span class="name">你的精致女友</span>
                    <span class="time">{{date}}</span>
                    <span class="preview">快来学习如何撩我吧~...</span>
                </li>
            </ul>
        </div>
        <div class="right">
            <div class="chat" data-chat="person2">
                {%for i in data%}
                <div class="conversation-start">
                    <span>{{date}}</span>
                </div>
                {% if size == 1%}
                <div class="bubble me">
                    {{i[0]}}
                </div>
                {%elif size == 2%}
                <div class="bubble me">
                    {{i[0]}}
                </div>
                <div class="bubble you">
                    {{i[1]}}
                </div>
                {%elif size == 3%}
                <div class="bubble me">
                    {{ i[0] }}
                </div>
                <div class="bubble you">
                    {{ i[1] }}
                </div>
                <div class="bubble me">
                    {{ i[2] }}
                </div>
                {%endif%}
                {%endfor%}
            </div>
            <div class="write" style="text-align: center">
                <form action="" method="post">
                    <input style="border-radius: 30px;color: white;text-align: center;background-color: #00b0ff"
                           type="submit" value="获取情话">
                </form>
            </div>
        </div>
    </div>
</div>
<script src="../static/js/index.js"></script>
</body>
</html>


Jinjia2语法:


{%for i in data%}
  <div class="conversation-start">
<span>{{date}}</span>
</div>
{% if size == 1%}
<div class="bubble me">
{{i[0]}}
 </div>
{%elif size == 2%}
<div class="bubble me">
 {{i[0]}}
  </div>
<div class="bubble you">
{{i[1]}}
</div>
{%elif size == 3%}
<div class="bubble me">
   {{ i[0] }}
</div>
<div class="bubble you">
 {{ i[1] }}
  </div>
  <div class="bubble me">
  {{ i[2] }}
</div>
{%endif%}
 {%endfor%}


通过后端传递的列表动态添加情话数据。


完结

相关文章
|
7月前
|
算法 Java Go
【GoGin】(1)上手Go Gin 基于Go语言开发的Web框架,本文介绍了各种路由的配置信息;包含各场景下请求参数的基本传入接收
gin 框架中采用的路优酷是基于httprouter做的是一个高性能的 HTTP 请求路由器,适用于 Go 语言。它的设计目标是提供高效的路由匹配和低内存占用,特别适合需要高性能和简单路由的应用场景。
604 4
|
11月前
|
缓存 JavaScript 前端开发
鸿蒙5开发宝藏案例分享---Web开发优化案例分享
本文深入解读鸿蒙官方文档中的 `ArkWeb` 性能优化技巧,从预启动进程到预渲染,涵盖预下载、预连接、预取POST等八大优化策略。通过代码示例详解如何提升Web页面加载速度,助你打造流畅的HarmonyOS应用体验。内容实用,按需选用,让H5页面快到飞起!
|
11月前
|
JavaScript 前端开发 API
鸿蒙5开发宝藏案例分享---Web加载时延优化解析
本文深入解析了鸿蒙开发中Web加载完成时延的优化技巧,结合官方案例与实际代码,助你提升性能。核心内容包括:使用DevEco Profiler和DevTools定位瓶颈、四大优化方向(资源合并、接口预取、图片懒加载、任务拆解)及高频手段总结。同时提供性能优化黄金准则,如首屏资源控制在300KB内、关键接口响应≤200ms等,帮助开发者实现丝般流畅体验。
|
7月前
|
开发框架 前端开发 Go
【GoGin】(0)基于Go的WEB开发框架,GO Gin是什么?怎么启动?本文给你答案
Gin:Go语言编写的Web框架,以更好的性能实现类似Martini框架的APInet/http、Beego:开源的高性能Go语言Web框架、Iris:最快的Go语言Web框架,完备的MVC支持。
629 1
|
11月前
|
开发框架 JSON 中间件
Go语言Web开发框架实践:路由、中间件、参数校验
Gin框架以其极简风格、强大路由管理、灵活中间件机制及参数绑定校验系统著称。本文详解其核心功能:1) 路由管理,支持分组与路径参数;2) 中间件机制,实现全局与局部控制;3) 参数绑定,涵盖多种来源;4) 结构体绑定与字段校验,确保数据合法性;5) 自定义校验器扩展功能;6) 统一错误处理提升用户体验。Gin以清晰模块化、流程可控及自动化校验等优势,成为开发者的优选工具。
|
11月前
|
开发框架 安全 前端开发
Go Web开发框架实践:模板渲染与静态资源服务
Gin 是一个功能强大的 Go Web 框架,不仅适用于构建 API 服务,还支持 HTML 模板渲染和静态资源托管。它可以帮助开发者快速搭建中小型网站,并提供灵活的模板语法、自定义函数、静态文件映射等功能,同时兼容 Go 的 html/template 引擎,具备高效且安全的页面渲染能力。
|
11月前
|
开发框架 JSON 中间件
Go语言Web开发框架实践:使用 Gin 快速构建 Web 服务
Gin 是一个高效、轻量级的 Go 语言 Web 框架,支持中间件机制,非常适合开发 RESTful API。本文从安装到进阶技巧全面解析 Gin 的使用:快速入门示例(Hello Gin)、定义 RESTful 用户服务(增删改查接口实现),以及推荐实践如参数校验、中间件和路由分组等。通过对比标准库 `net/http`,Gin 提供更简洁灵活的开发体验。此外,还推荐了 GORM、Viper、Zap 等配合使用的工具库,助力高效开发。
|
JSON 前端开发 API
使用Python和Flask构建简易Web API
使用Python和Flask构建简易Web API
1117 86
|
JSON API 数据格式
使用Python和Flask构建简单的Web API
使用Python和Flask构建简单的Web API