from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.factory import Factory
from kivy.properties import ObjectProperty, ListProperty
from kivy.uix.popup import Popup
from kivy.core.window import Window
from kivy.uix.label import Label 
import io
import os
import pandas as pd
import numpy as np
from plotly.offline import plot
from plotly.offline import iplot
import plotly.graph_objs as go
import cufflinks as cf
cf.go_offline()
#Window.clearcolor = (1, 1, 1, 1)


class LoadDialog(FloatLayout):
    load = ObjectProperty(None)
    cancel = ObjectProperty(None)


class SaveDialog(FloatLayout):
    save = ObjectProperty(None)
    text_input = ObjectProperty(None)
    cancel = ObjectProperty(None)


class Root(FloatLayout):
    loadfile = ObjectProperty(None)
    savefile = ObjectProperty(None)
    text_input = ObjectProperty(None)
    rv_data = ListProperty([])
    def dismiss_popup(self):
        self._popup.dismiss()

    def show_load(self):
        content = LoadDialog(load=self.load, cancel=self.dismiss_popup)
        self._popup = Popup(title="Load file", content=content,
                            size_hint=(0.9, 0.9))
        self._popup.open()

    def show_save(self):
        content = SaveDialog(save=self.save, cancel=self.dismiss_popup)
        self._popup = Popup(title="Save file", content=content,
                            size_hint=(0.9, 0.9))
        self._popup.open()

    def load(self, path, filename): 
        with open(os.path.join(path, filename[0])) as stream:
            lines = [l for l in stream if not l.startswith('##')]
        self.a=pd.read_csv(io.StringIO(''.join(lines)),dtype={'#A': str, 'B': int, 'C': str, 'D': str, 'E': str},sep='\t').rename(columns={'#A': 'A'})
        self.pandaf1()
        
        self.dismiss_popup()
	def pandaf1(self):
        self.panda2rv(self.a)#successfully print complete file 
    def panda2rv(self, frame):
        lines=[]
        tl=frame.to_string().split("\n")
        for i in range(len(tl)):
            temp=str(tl[i])
            lines.append({'text':temp})
        self.rv_data = lines
######TASKS TO PERFORM ON UPLOADED BIG DATA FILE#####
#function 1: Checkbox 1
	def func_1(x):
		return self.a[self.a.C == "|"] #it will print those value from col C which have the value |
print(func_1(self.a)) 

#function 2: drop down list
	def func_2(b):
		return  self.a[(self.a["A"]==b)]     #it will print the rows of only selected value 
            
print(func_2("USER SELECTED VALUE FROM DROP DOWN LIST"))    
######TASKS TO PERFORM ON UPLOADED BIG DATA FILE#####
###visualize the data 
#function 3: checkbox
	def func_3(y):
		return a[["D","C"]].iplot() #using iplot it will visualize the col D and C 
print(func_3(a))
###visualize the data        

    def save(self, path, filename):
        with open(os.path.join(path, filename), 'w') as stream:
            stream.write(self.text_input.text)

        self.dismiss_popup()


class EditorApp(App):
    pass


Factory.register('Root', cls=Root)
Factory.register('LoadDialog', cls=LoadDialog)
Factory.register('SaveDialog', cls=SaveDialog)


if __name__ == '__main__':
    EditorApp().run()