首页 > 系统相关 >Windows+Pycharm+Flask+Vue+Element-Plus 前后端分离实现分写查询功能

Windows+Pycharm+Flask+Vue+Element-Plus 前后端分离实现分写查询功能

时间:2024-05-14 11:43:58浏览次数:17  
标签:Vue return name Windows app Element vue import data

准备工作

安装nodejs

https://nodejs.cn/download/

验证是否安装成功

安装Python

不赘述,3.7+

Pycharm 创建Flask项目

D:\pythonProject\myvue

Windows Power Shell 进入到CMD指令界面,cd D:\pythonProject\myvue 进入到myvue目录,创建vue项目文件client,然后cd到client目录,安装vue-cli

npm install -g @vue/cli

然后cd到client目录,安装element-plus

npm install element-plus --save

然后cd到client目录,安装axios(前后端分离的应用,要安装请求的库axios)

npm install --save axios

前端代码(myvue\client\scr\components\DataQuery.vue)

<template>
  <el-form :inline="true" :model="queryForm" class="form-inline" @submit.native.prevent="onSubmit">
    <el-form-item label="债券名称">
      <el-input class="input-width" v-model="queryForm.bondShortName" placeholder="请输入债券名称"></el-input>
    </el-form-item>
    <el-form-item label="LEI">
      <el-input class="input-width" v-model="queryForm.lei" placeholder="请输入LEI"></el-input>
    </el-form-item>
    <el-form-item label="估值日期">
      <el-date-picker v-model="queryForm.priceAsOf" type="date" placeholder="请选择日期"></el-date-picker>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" native-type="submit">查询</el-button>
      <el-button native-type="reset">重置</el-button>
    </el-form-item>
  </el-form>
  <div class="table-container">
    <div ref="tableWrapper" class="table-wrapper">
      <el-table :data="queryResult" style="width: 100%">
        <el-table-column fixed prop="bond_short_name" label="债券名称"></el-table-column>
        <el-table-column width="250" prop="lei" label="LEI"></el-table-column>
        <el-table-column prop="price_as_of" label="估值日期"></el-table-column>
        <el-table-column prop="isin" label="isin"></el-table-column>
        <el-table-column prop="current_coupon" label="当前票息"></el-table-column>
        <el-table-column prop="issue_price" label="发行价格"></el-table-column>
        <el-table-column prop="issue_size" label="发行规模"></el-table-column>
        <el-table-column prop="maturity" label="发行期限"></el-table-column>
      </el-table>
    </div>
  </div>
  <el-pagination
    @current-change="handlePageChange"
    :current-page="currentPage"
    :page-size="pageSize"
    :total="total"
    layout="prev, pager, next, jumper"
    background>
  </el-pagination>
</template>

<script>
import { ref, onMounted, onBeforeUnmount } from 'vue';
import axios from 'axios';
import { ElMessage, ElTable, ElTableColumn } from 'element-plus';

export default {
  components: {
    ElTable,
    ElTableColumn,
  },
  setup() {
    const queryForm = ref({ bondShortName: '', lei: '', priceAsOf: '' });
    const queryResult = ref([]);
    const currentPage = ref(1); // 当前页码
    const pageSize = ref(50); // 每页显示条数
    const total = ref(0); // 数据总数

    const loadQueryResult = () => {
      axios.get('http://localhost:5000/query', {
        params: {
          bond_short_name: queryForm.value.bondShortName,
          lei: queryForm.value.lei,
          price_as_of: queryForm.value.priceAsOf,
          page: currentPage.value, // 将当前页码传递给后端
          page_size: pageSize.value // 将每页显示条数传递给后端
        }
      })
      .then(response => {
        queryResult.value = response.data.data;
        total.value = response.data.total; // 更新总记录数
      })
      .catch(error => {
        ElMessage.error('Error fetching data');
        console.error(error);
      });
    };

    const handlePageChange = (newPage) => {
      currentPage.value = newPage; // 更新当前页码
      loadQueryResult(); // 重新加载数据
    };

    // 组件挂载时加载数据
    onMounted(loadQueryResult);

    const onSubmit = () => {
      currentPage.value = 1; // 每次查询时,重置当前页码为1
      loadQueryResult();
    };

    return {
      queryForm,
      queryResult,
      currentPage,
      pageSize,
      total,
      handlePageChange,
      onSubmit,
    };
  }
};
</script>

<style scoped>
.form-inline.input-width {
  width: 25%;
}

.table-wrapper {
  overflow-y: auto;
}
/* 表格容器样式 */
.table-container {
  height: 830px; /* 设置合适的高度 */
  overflow-y: auto; /* 添加滚动条 */
}
</style>

main.js(myvue/client/src/main.js)

import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import 'element-plus/theme-chalk/index.css'
import ElementPlus from 'element-plus'
import "@element-plus/icons-vue"
import 'element-plus/dist/index.css'

createApp(App).use(router).use(ElementPlus).mount('#app')

App.vue(myvue/client/src/App.vue)

<template>
  <div id="app">
    <data-query />
  </div>
</template>

<script>
import DataQuery from './components/DataQuery.vue';

export default {
  components: {
    DataQuery,
  },
};
</script>

后台代码(myvue\app.py)

from flask import Flask, request, jsonify
import pymysql
from flask_cors import CORS
from datetime import datetime

app = Flask(__name__)
CORS(app)

app.config['MYSQL_HOST'] = '127.0.0.1'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = '******'
app.config['MYSQL_DB'] = 'db_name'

def init_db():
    return pymysql.connect(
        host=app.config['MYSQL_HOST'],
        user=app.config['MYSQL_USER'],
        password=app.config['MYSQL_PASSWORD'],
        db=app.config['MYSQL_DB'],
        port=33061,
        charset='utf8mb4',
        cursorclass=pymysql.cursors.DictCursor
    )

def format_date(date):
    try:
        # 将整数类型的日期转换为字符串,然后解析为日期类型
        return datetime.strptime(str(date), '%Y%m%d').strftime('%Y-%m-%d')
    except ValueError:
        return date

def to_str(data):
    if isinstance(data, dict):
        return {k: to_str(v) for k, v in data.items()}
    elif isinstance(data, list):
        return [to_str(v) for v in data]
    elif isinstance(data, bytes):
        return data.decode('utf-8', errors='replace')  # 将 bytes 类型转换为 str
    else:
        return data

@app.route('/query', methods=['GET'])
def query_data():
    bond_short_name = request.args.get('bond_short_name', '').strip()
    lei = request.args.get('lei', '').strip()
    price_as_of = request.args.get('price_as_of', '').strip()
    page = int(request.args.get('page', 1))
    page_size = min(int(request.args.get('page_size', 10)), 100)  # 限制每页显示的数量最大为 100 条

    try:
        connection = init_db()
        with connection.cursor() as cursor:
            conditions = []
            params = []

            if bond_short_name:
                conditions.append("bond_short_name LIKE %s")
                params.append('%' + bond_short_name + '%')
            if lei:
                conditions.append("lei LIKE %s")
                params.append('%' + lei + '%')
            if price_as_of:
                formatted_date = format_date(price_as_of)
                conditions.append("price_as_of = %s")
                params.append(formatted_date)

            sql = "SELECT * FROM cbusd_instrument"
            if conditions:
                sql += " WHERE " + " AND ".join(conditions)

            count_sql = f"SELECT COUNT(*) FROM cbusd_instrument"
            if conditions:
                count_sql += " WHERE " + " AND ".join(conditions)

            cursor.execute(count_sql, params)
            total_records = cursor.fetchone()['COUNT(*)']

            offset = (page - 1) * page_size
            sql += " LIMIT %s OFFSET %s"
            params.extend([page_size, offset])

            cursor.execute(sql, params)
            result = cursor.fetchall()

            # 将结果中的字节转换为字符串
            result = [to_str(item) for item in result]

        return jsonify(data=result, total=total_records)

    except pymysql.err.MySQLError as e:
        return jsonify({"status": "error", "message": str(e)})
    finally:
        connection.close()

if __name__ == '__main__':
    app.run(host='127.0.0.1', port=5000)  # 根据需要更改主机和端口

启动前端

cd D:\pythonProject\myvue>client

npm run serve

启动后台

验证后台程序是否正常

标签:Vue,return,name,Windows,app,Element,vue,import,data
From: https://www.cnblogs.com/zhouwp/p/18190752

相关文章

  • windows 安装Nginx服务
    一、版本说明  Nginx版本:1.26.0 二、下载Nginx  下载地址:https://nginx.org/en/download.html    选择一个版本,这里选择最新稳定版本  下载后解压到一个目录,注意解压目录最好不要有中文、空格    因为电脑只有一个C盘所以地址在C盘,可以选择自己习惯的安装......
  • VUE3.0 中如何使用SVG图标
    1.文件下新建SvgIcon文件夹以及子文件index.js,index.vue,svg文件夹(用于存放svg图片) 2.编写index.vue组件<template><svg:class="svgClass"aria-hidden="true"><use:xlink:href="iconName"/></svg></template><......
  • Java开发微服务SpringCloudAlibaba+Nginx+Vue+Mysql+RabbitMQ
    项目介绍随着互联网技术的飞速发展和移动设备的普及,自媒体平台已经成为人们获取信息、传播观点、实现自我价值的重要途径。自媒体平台的设计与实现,不仅需要考虑如何提供便捷的内容发布、编辑和管理功能,还需要考虑如何构建健康的内容生态,保证信息的真实性和可靠性,防止虚假信息的传......
  • 高DPI下windows远程桌面缩放问题
    第一步:打开注册表:HKEY_LOCAL_MACHINE>SOFTWARE>Microsoft>Windows>CurrentVersion>SideBySide新建DWORD(32bit)的项,命名为:PreferExternalManifest,打开PreferExternalManifest,将值设为1,16进制。第二步打开记事本输入如下代码:<?xmlversion="1.0"encoding=&quo......
  • LeetCode 2956. Find Common Elements Between Two Arrays
    原题链接在这里:https://leetcode.com/problems/find-common-elements-between-two-arrays/description/题目:Youaregiventwo 0-indexed integerarrays nums1 and nums2 ofsizes n and m,respectively.Considercalculatingthefollowingvalues:Thenumberof......
  • 记录一次Windows上简单向linux上传文件
    1直接账号密码登录上传---使用winscp(得先安装winscp)``@echooffREM设置WinSCP安装路径setWINSCP_PATH="C:\ProgramFiles(x86)\WinSCP\WinSCP.com"REM设置连接参数setHOSTNAME=192.168.1.112setUSERNAME=rootsetPASSWORD=xxxxxxxxsetREMOTE_PATH=/home/......
  • vue3编译优化之“静态提升”
    前言在上一篇vue3早已具备抛弃虚拟DOM的能力了文章中讲了对于动态节点,vue做的优化是将这些动态节点收集起来,然后当响应式变量修改后进行靶向更新。那么vue对静态节点有没有做什么优化呢?答案是:当然有,对于静态节点会进行“静态提升”。这篇文章我们来看看vue是如何进行静态提升的......
  • Vue模板语法、属性绑定、条件渲染的学习
    Vue模板语法:使用插值表达式的内容必须是有结果的内容才可以,就是需要return出来的才可以显示出来。插值表达式所表现的内容为纯文本模式如何避免即所有的逻辑操作都在js里面实现,不要再templete中实现可以完美的避免这个问题。Vue属性绑定1.使用v-bind进行属性绑定语法:v-b......
  • how BabyFile app transfer files with Windows PC though data cable
    1.WindowsPCdownload"iTunes"App.Asfollows:2.WindowsPCconnecttheiPhone/iPadwithadatacable,andthen open"iTunes"app, findandclicktheconnecteddevice.asmarkedby①inthepicture. 2.Findand clickthe"Fi......
  • 今天学了vue3的composition API
    代码量:60左右时间:1h搏客量:1<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><linkrel="icon"href="/favicon.ico"><metaname="viewport"content="wi......