python – 如何在一个简单的Kivy应用程序中将Pandas Dataframe显示为一个表?

我想构建一个简单的Kivy应用程序:

>要求用户输入整数(MTP)
>要求用户输入延迟间隔(延迟)
>在用户按下提交按钮然后从中查询URL
查询创建一个显示的pandas数据框(如
表)在Kivy应用程序的主屏幕上,然后输出
更新数据帧,直到MTP和延迟间隔结束

我将查询的所有代码作为常规python程序运行,我只是不知道如何显示用户输入和输出数据帧(以及时间戳). (见下文)

import requests
import time
from bs4 import BeautifulSoup
from datetime import datetime
import itertools
import pandas as pd

mtp = input("Input whole number of minutes to post:") #<-How to do on Kivy Screen
delay = input("Enter 15, 30 or 60 second delay:") #<-How to ask on Kivy Screen
STP = int(mtp)*60

def requestOdds():
    url = "https://WEBPAGE WITH ODDS DATA.aspx"
    r = requests.get(url)
    soup = BeautifulSoup(r.content)
    stamp = datetime.now().strftime('%m/%d %H:%M:%S')
    tbPPosts = ('rptOdds_ctl01_tdOdd', 'rptOdds_ctl02_tdOdd','rptOdds_ctl03_tdOdd','rptOdds_ctl04_tdOdd','rptOdds_ctl05_tdOdd','rptOdds_ctl06_tdOdd','rptOdds_ctl07_tdOdd','rptOdds_ctl08_tdOdd','rptOdds_ctl09_tdOdd','rptOdds_ctl10_tdOdd','rptOdds_ctl11_tdOdd','rptOdds_ctl12_tdOdd','rptOdds_ct113_tdOdd','rptOdds_ct114_tdOdd','rptOdds_ct115_tdOdd','rptOdds_ct116_tdOdd','rptOdds_ct117_tdOdd','rptOdds_ct118_tdOdd','rptOdds_ct119_tdOdd','rptOdds_ct120_tdOdd','rptOdds_ct121_tdOdd')
    mlPPosts = ('rptOdds_ctl01_tdMl','rptOdds_ctl02_tdMl','rptOdds_ctl03_tdMl','rptOdds_ctl04_tdMl','rptOdds_ctl05_tdMl','rptOdds_ctl06_tdMl','rptOdds_ctl07_tdMl','rptOdds_ctl08_tdMl','rptOdds_ctl09_tdOdd','rptOdds_ctl10_tdMl','rptOdds_ctl11_tdMl','rptOdds_ctl12_tdMl','rptOdds_ct113_tdMl','rptOdds_ct114_tdMl','rptOdds_ct115_tdMl','rptOdds_ct116_tdMl', 'rptOdds_ct117_tdMl','rptOdds_ct118_tdMl','rptOdds_ct119_tdMl','rptOdds_ct120_tdMl','rptOdds_ct121_tdMl')
    trimmed_text=[]
    trimmed_text2=[]
    tbodds = []
    mlodds = []
    odds = []

    for horse in tbPPosts:
        table = soup.find('td', {"odds-table-odd"}, id=horse)
        if table is not None:
            text = table.renderContents()
            trimmed_text = text.strip()
            tbodds.append(trimmed_text) 

    for mlhorse in mlPPosts:
        table2 = soup.find('td', {"odds-table-ml"}, id=mlhorse)
        if table2 is not None:
            text2 = table2.renderContents()
            trimmed_text2 = text2.strip()
            mlodds.append(trimmed_text2)

    mlodds = itertools.ifilterfalse(lambda x: x=='', mlodds) #faster
    tbodds = itertools.ifilterfalse(lambda x: x=='', tbodds)
    oddsDict = dict(zip(mlodds, tbodds))
    **odds = pd.DataFrame(list(oddsDict.iteritems()), columns=['ML','TB'])
    print odds, stamp** #<--How do I get these to print onto the Kivy Screen?

def getLiveOdds():
    interval = 0
    while interval <= STP:
        requestOdds()
        time.sleep(delay)
        interval +=delay

getLiveOdds()

任何帮助将不胜感激!

最佳答案 我找到了可以帮到你的东西:

Kivy档案

GraphDraw:

<GraphDraw>:

    BoxLayout:
        Button:
            text: "Hello World"
            on_press: root.graph()

逻辑

#!/usr/bin/env python
# -*- encoding: utf-8

import datetime
import pandas as pd
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
import dfgui
import pandas as pd


class Visualisation(App):
    pass

class GraphDraw(BoxLayout):
    def graph(self):
        xls = pd.read_excel('filepath')
        #df = pd.DataFrame.xls
        dfgui.show(xls)
        #print xls

if __name__ == '__main__':
    Visualisation().run()

所以你使用dfgui可以创建pandas dataframe表而不是使用Kivy.
请参阅项目dfgui:https://github.com/bluenote10/PandasDataFrameGUI

我希望它可以帮助:)

点赞