title: Load Big File into TextInput without blocking UI (python kivy)
body: <p>I need to load a big file into a <a href="https://kivy.org/doc/stable/api-kivy.uix.textinput.html" rel="nofollow noreferrer">TextInput</a> in a python GUI app built with <a href="https://en.wikipedia.org/wiki/Kivy_(framework)" rel="nofollow noreferrer">kivy</a>. What is the most efficient way to do this without blocking the UI?</p>
  <p>For the sake of this example, I'm using a file named <code>big_file.txt</code>. <a href="https://en.wikipedia.org/wiki/War_and_Peace" rel="nofollow noreferrer">War And Peace (the book)</a> is big: 3.3 MB in plaintext, available from Project Gutenberg here:</p>
  <ul>
  <li><a href="https://www.gutenberg.org/ebooks/2600.txt.utf-8" rel="nofollow noreferrer">https://www.gutenberg.org/ebooks/2600.txt.utf-8</a></li>
  </ul>
  <p>Save that file to some directory:</p>
  <pre><code>wget -O big_file.txt https://www.gutenberg.org/ebooks/2600.txt.utf-8
  </code></pre>
  <p>Consider the following simplified application named <code>main.py</code> and located in the same directory as <code>big_file.txt</code></p>
  <pre><code>import time
  
  import kivy
  from kivy.app import App
  from kivy.uix.label import Label
  from kivy.uix.button import Button
  from kivy.uix.textinput import TextInput
  from kivy.uix.boxlayout import BoxLayout
  
  class MyApp(App):
  
      def build(self):
          self.count = 0
          layout = BoxLayout()
  
          # add the label
          self.label1 = Label(id='label1', text='0')
          layout.add_widget(self.label1)
  
          # add the button
          layout.add_widget( Button(text='Increment', on_release=self.increment) )
  
          # add the textinput
          self.textinput1 = TextInput(text='Click button to load file.')
          layout.add_widget(self.textinput1)
  
          # add the button
          layout.add_widget( Button(text='Load File', on_release=self.load_file) )
  
          return layout
  
      def load_file(self, *args):
          start = time.time()
          print( &quot;loading file&quot; )
          with open( 'big_file.txt' ) as big_file:
              self.textinput1.text = big_file.read()
          end = time.time()
          print( &quot;finished loading file in &quot; + str(end-start) + &quot; seconds&quot; )
  
      def increment(self, *args):
          self.count+=1
          self.label1.text = str(self.count)
  
  if __name__ == &quot;__main__&quot;:
      MyApp().run()
  </code></pre>
  <p>This kivy application has:</p>
  <ol>
  <li>An &quot;Increment&quot; Label</li>
  <li>An &quot;Increment&quot; Button</li>
  <li>A TextInput</li>
  <li>A &quot;Load File&quot; Button</li>
  </ol>
  <p>The problem is that it takes ~15 seconds to <code>read()</code> the 3.3 MB <code>big_file.txt</code>. And during that time, the UI is blocked.</p>
  <p>For example, after clicking the <code>Load File</code> button, the user can't click the <code>Increment</code> button for ~15 seconds -- until the file is finished being read by the <code>load_file()</code> function.</p>
  <p><a href="https://i.stack.imgur.com/fJGLl.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/fJGLl.gif" alt="screen capture of execution with UI locked for 15 seconds while loading the file" /></a></p>
  <p>How can I efficiently load the contents of the kivy <code>TextInput</code> with <code>big_file.txt</code> without blocking the UI?</p>