【问题标题】:Plotting graphs using Python Web app by connecting to Oracle DB通过连接到 Oracle DB 使用 Python Web 应用程序绘制图形
【发布时间】:2017-09-03 06:31:38
【问题描述】:

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

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

【问题讨论】:

    标签: python oracle web-applications


    【解决方案1】:

    有几种方法可用,包括使用 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)
    

    【讨论】:

    • 非常感谢考希克。感谢您的快速帮助。我可以使用 django 开发一个网页,该网页将 1. 接受用户的输入。当用户提交表单时。 2. 从 Oracle DB 中获取数据 3. 绘制条形图并通过网页将响应作为 html 响应返回给用户。再次感谢。问候,Yogesh
    猜你喜欢
    • 2020-04-08
    • 2014-05-24
    • 2021-07-15
    • 2013-09-29
    • 2017-09-24
    • 1970-01-01
    • 2012-03-11
    • 2021-08-31
    • 2018-04-05
    相关资源
    最近更新 更多