#!/usr/bin/env python
# coding: utf-8

# In[1]:


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()

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,'F': str},sep='\t').rename(columns={'*A': 'A'})
        self.pandaf1()
        self.dismiss_popup()
        
    def pandaf1(self):
        self.panda2rv(self.a)#complete file to print
        

    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
        

    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()


# In[ ]:





# In[ ]:




