0

I'm writing some code with Python and Vincent to display some map data.

The example from the docs looks like this:

import vincent

county_topo = r'us_counties.topo.json'
state_topo = r'us_states.topo.json'

geo_data = [{'name': 'counties',
             'url': county_topo,
             'feature': 'us_counties.geo'},
            {'name': 'states',
             'url': state_topo,
             'feature': 'us_states.geo'}]

vis = vincent.Map(geo_data=geo_data, scale=3000, projection='albersUsa')
del vis.marks[1].properties.update
vis.marks[0].properties.update.fill.value = '#084081'
vis.marks[1].properties.enter.stroke.value = '#fff'
vis.marks[0].properties.enter.stroke.value = '#7bccc4'
vis.to_json('map.json', html_out=True, html_path='map_template.html')

Running this code outputs an html file, but it's formatted improperly. It's in some kind of python string representation, b'<html>....</html>'.

If I remove the quotes and the leading b, the html page works as expected when run through the built in python server.

What's wrong with my output statement?

Brandon
  • 3,573
  • 6
  • 19
  • 21

1 Answers1

0

From the Docs:

A prefix of 'b' or 'B' is ignored in Python 2; it indicates that the literal should become a bytes literal in Python 3 (e.g. when code is automatically converted with 2to3). A 'u' or 'b' prefix may be followed by an 'r' prefix.

You can slice it using:

with open('map_template.html', 'w') a f:
    html = f.read()[2:-1]
    f.truncate()
    f.write(html)

This will open your html file,

b'<html><head><title>MyFile</title></head></html>' 

And remove the first 2 and last character, giving you:

    <html><head><title>MyFile</title></head></html>
Tui Popenoe
  • 2,098
  • 2
  • 23
  • 44