python - 通过连接到 Oracle DB 使用 Python Web 应用程序绘制图表

标签 python oracle web-applications

我对 Python 世界还比较陌生。我想知道如何使用 Python 生态系统实现以下要求, 1. 简单的交互式网页供用户选择日期范围。 例如。订单数据的“天”范围选择。 2. 根据天数范围选择从 Oracle 11g DB 获取数据。 3. 将数据转换为条形图并在Web应用程序上呈现。

我用谷歌搜索了一下,但没有找到任何完整的信息。 提前谢谢了。 谢谢, 约格什

最佳答案

有多种方法可用,其中包括使用 python 库 sqlalchemypandas 。但是,对于我在这里描述的方法,您需要安装以下 Python 库。

  • cx_Oracle

  • ma​​tplotlib

    我不会解释从网页获取用户输入或将绘图存储为图像并在网页上显示绘图的方法。你可以谷歌或引用这些问题的各种方法:-

Extracting Fields Names of an HTML form - Python

Dynamically serving a matplotlib image to the web using python

假设您已读取用户输入以及存储在变量 st_dateend_date 中的两个日期。这是代码。

import cx_Oracle
import matplotlib.pyplot as plt


query = 'SELECT order_date, count(order_id) order_count
     FROM orders WHERE order_date BETWEEN ' + st_date + ' AND ' + end_date +
         ' GROUP BY order_date '

order_date =  []
order_count = []


#connect to database and execute query.
db = cx_Oracle.connect('user', 'password', 'localhost:1521/XE')
cursor = db.cursor()
cursor.execute(query)

#loop through the rows fetched and store the records as arrays.
for row in cursor:
    order_date.append(row[0])
    order_count.append(row[1])

#plot the bar chart

plt.bar(order_date,order_count)

关于python - 通过连接到 Oracle DB 使用 Python Web 应用程序绘制图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46020899/

相关文章:

java - 更新中缺少表达式

php - 每 1/10 秒网页上的随机词

c# - 模式对话框未关闭

python - Keras 功能 API : "Error when checking input: expected input_1 to have 4 dimensions, but got array with shape (X, Y)"

sql - oracle中的减号运算符

python - SQLAlchemy:在回滚无效事务之前无法重新连接

ORACLE 不保存日期时间。为什么?

web-applications - 如何为 Web 应用程序提供合法的许可证或 "terms of use"

python - 为什么在将属性更改为引用外部函数后不传递 self?

python - 为什么加载这个文件占用这么多内存?