python整活代码

Python 发票查询接口

本文将介绍如何使用 Python 创建一个发票查询接口。发票是商业交易中的重要文件,可以用于管理和记录商业活动。通过将发票文件存储在数据库中,并创建一个可查询的 API 接口,可以更有效地管理和使用这些信息。

设计概述

我们将使用 Flask 框架创建 API 接口,并使用 SQLite 作为数据库。前端使用 Bootstrap 提供简单而直观的用户界面。这个发票查询接口将支持以下功能:

- 添加新发票;

- 根据日期,发票类型或供应商名称查询发票;

- 查看所有发票。

实现过程

第一步:安装 Flask 和 SQLite

我们需要在本地安装 Flask 和 SQLite。使用 pip 命令安装这些库:

pip install flask

pip install sqlite3

成功安装后,我们将使用 sqlite3 接口连接到数据库。

第二步:建立数据库

我们需要创建一个新的数据库来存储发票数据。我们可以使用以下命令在 SQLite 中创建新表:

CREATE TABLE invoices (

id INTEGER PRIMARY KEY,

date TEXT,

type TEXT,

supplier TEXT,

amount FLOAT

);

第三步:建立 Flask 应用程序

我们将使用 Flask 框架建立 API 接口。输入以下代码:

from flask import Flask

from flask import render_template, request, redirect

app = Flask(__name__)

@app.route('/')

def index():

return render_template('index.html')

@app.route('/add', methods=['POST'])

def add_invoice():

date = request.form['date']

invoice_type = request.form['invoice_type']

supplier = request.form['supplier']

amount = request.form['amount']

# 简单的数据验证

if not date or not invoice_type or not supplier or not amount:

return 'Add invoice error!'

# 将数据插入到 SQLite 数据库

c = get_db().cursor()

c.execute('INSERT INTO invoices (date, type, supplier, amount) VALUES (?, ?, ?, ?)', (date, invoice_type, supplier, amount))

get_db().commit()

c.close()

return redirect('/')

@app.route('/search')

def search():

# 从查询字符串中读取 search 参数值

search = request.args.get('search')

# 在 SQLite 数据库中查询发票

invoices = []

if search:

c = get_db().cursor()

c.execute("SELECT * FROM invoices WHERE date LIKE ? OR type LIKE ? OR supplier LIKE ?", ('%'+search+'%', '%'+search+'%', '%'+search+'%'))

invoices = c.fetchall()

c.close()

return render_template('search.html', invoices=invoices)

@app.route('/invoices')

def invoices():

# 获取所有的发票

c = get_db().cursor()

c.execute("SELECT * FROM invoices")

invoices = c.fetchall()

c.close()

return render_template('invoices.html', invoices=invoices)

def get_db():

# 使用 Flask 全局变量 g 来存储 SQLite 数据库连接

if not hasattr(g, 'sqlite_db'):

g.sqlite_db = sqlite3.connect('invoices.db')

return g.sqlite_db

@app.teardown_appcontext

def close_db(error):

# 关闭数据库连接

if hasattr(g, 'sqlite_db'):

g.sqlite_db.close()

if __name__ == '__main__':

app.run()

第四步:建立用户界面

我们将使用 Bootstrap 创建基本的用户界面。在 templates 文件夹中创建三个 HTML 文件:index.html, search.html 和 invoices.html。

首先是 index.html:

Invoice Query API

Invoice Query API



All Invoices

下一步是 search.html:

Search Invoice

Search Invoice


{% for invoice in invoices %}

{% endfor %}

#DateInvoice TypeSupplierAmount
{{ invoice[0] }}{{ invoice[1] }}{{ invoice[2] }}{{ invoice[3] }}{{ invoice[4] }}

最后是 invoices.html:

All Invoices

All Invoices

{% for invoice in invoices %}

{% endfor %}

#DateInvoice TypeSupplierAmount
{{ invoice[0] }}{{ invoice[1] }}{{ invoice[2] }}{{ invoice[3] }}{{ invoice[4] }}

第五步:运行应用

使用 Python 运行应用程序。在终端中运行以下命令:

export FLASK_APP=app.py

flask run

打开浏览器并访问 http://127.0.0.1:5000。

总结

本文展示了如何使用 Flask 和 SQLite 创建一个简单的发票查询 API 接口。我们可以通过这个接口添加,查询和浏览发票数据。通过使用这个示例,你可以扩展这个应用程序,并开发更复杂的功能来管理和记录所有商业活动。同时,你可以通过了解 Flask 和 SQLite 的其他功能来增强服务器应用程序。 如果你喜欢我们三七知识分享网站的文章, 欢迎您分享或收藏知识分享网站文章 欢迎您到我们的网站逛逛喔!https://www.37seo.cn/

点赞(89) 打赏

评论列表 共有 0 条评论

暂无评论
立即
投稿
发表
评论
返回
顶部