[英]Plotting graphs using Python Web app by connecting to Oracle DB
我對Python領域比較陌生。 我不知道如何使用Python生態系統達到以下要求。1.簡單的交互式網頁供用戶選擇日期范圍。 例如,訂單數據的“天”范圍選擇。 2.根據天數范圍選擇從Oracle 11g DB提取數據。 3.將數據轉換成條形圖,並將其顯示在Web應用程序上。
我在Google上搜索了一下,但沒有找到任何完整的信息。 提前謝謝了。 謝謝,Yogesh
有幾種可用的方法,包括使用python庫sqlalchemy
或pandas
。 但是,對於我在這里描述的方法,您需要安裝以下Python庫。
matplotlib
我將不解釋從網頁獲取用戶輸入或將圖表存儲為圖像並在網頁上顯示圖表的方法。 您可以使用google或參考這些問題以了解各種方法:-
假設您已經閱讀了用戶輸入以及存儲在變量st_date
和end_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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.