title: How to base64 encode a html_string
body: <h2>AIM</h2>
  
  <p>I am attempting to encode a <code>folium</code> choropleth as <code>StringIO</code>. I am basing my answer of a <a href="https://stackoverflow.com/questions/53127869/how-to-create-an-bytesio-img-and-pass-to-template/53133673?noredirect=1#comment93294028_53133673">related query</a>. I have checked the answers <a href="https://stackoverflow.com/questions/21689365/python-3-typeerror-must-be-str-not-bytes-with-sys-stdout-write">here</a> and <a href="https://stackoverflow.com/questions/45482272/typeerror-a-bytes-like-object-is-required-not-str-python-2-to-3">here</a>.</p>
  
  <h2>ERROR</h2>
  
  <pre><code>AttributeError: 'bytes' object has no attribute 'encode'
  </code></pre>
  
  <h2>CODE</h2>
  
  <p><strong>views.py</strong></p>
  
  <pre><code>def get_choropleth(self, request):
      # make choropleth ('m')
      html_string = m.get_root().render()
      f = StringIO(html_string)
      choropleth = base64.b64decode(f.read())
      choropleth = choropleth.encode('utf8') # causing error
      return {'choropleth':choropleth}
  </code></pre>