Skip to content

PNG repr #634

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 24, 2017
Merged

PNG repr #634

merged 5 commits into from
Jun 24, 2017

Conversation

ocefpaf
Copy link
Member

@ocefpaf ocefpaf commented Jun 24, 2017

Not ready yet... just an idea for now.

See http://nbviewer.jupyter.org/gist/ocefpaf/f5a5cbdefac17985db0d89d82455e3ff

This could be used to save a png, pdf, etc.

xref.: #35

folium/map.py Outdated
@@ -246,6 +249,26 @@ def _repr_html_(self, **kwargs):
out = self._parent._repr_html_(**kwargs)
return out

def _repr_png_(self):
"""Displays the PNG Map in a Jupyter notebook."""
# https://github.com/ipython/ipython/issues/7899#issuecomment-76541102
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The notebook will run all _repr_*_ methods and, b/c this one can be slow, we don't really want it to be called without the user request. Not sure yet if this should be a _repr_png_ of just a to_png() method.

folium/map.py Outdated
driver.maximize_window()
# Ignore user map size.
driver.execute_script("document.body.style.width = '100%';")
png = driver.get_screenshot_as_png()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably want to "sleep" here for a few seconds to allow for the tiles to render.
Maybe that should be a kwargs.

with tempfile.NamedTemporaryFile(suffix=".html") as f:
fname = f.name
self.save(fname)
driver = selenium.webdriver.PhantomJS(service_log_path=os.path.devnull)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably add a try/except clause here to test for PhantomJS.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually the default error message from selenium is quite good. No need to override it.

@ocefpaf ocefpaf merged commit bb46954 into python-visualization:master Jun 24, 2017
@ocefpaf ocefpaf deleted the png_repr branch June 24, 2017 20:39
@ocefpaf ocefpaf mentioned this pull request Jun 24, 2017
sanga pushed a commit to sanga/folium that referenced this pull request Oct 24, 2017
@astrojuanlu
Copy link

Now a deprecation warning appears: UserWarning: Selenium support for PhantomJS has been deprecated, please use headless versions of Chrome or Firefox instead.

https://github.com/SeleniumHQ/selenium/blob/c5e4a585f1e3c4b3468ec93ab50d3668d47aef1b/py/CHANGES#L8

So this is likely to break soon.

@ocefpaf
Copy link
Member Author

ocefpaf commented Jan 26, 2018

Thanks for the heads up!

I'm already trying to fix this for the branca tests but I'm not sure yet on a good pattern for folium b/c we need to catch whatever the user has installed or force the user to always pass a driver, to be honest I don't like any of those solutions 😒

@nderen
Copy link

nderen commented Sep 28, 2018

Hi,

I've been trying to use this for a project, and while it works locally, if I build my application into a docker image or try to run it from a Google Cloud Compute instance, it fails. It looks like it's trying to contact one of the dependent services (there are a number of script, css, etc embeds in the the generated HTML) but one of those fails to return any data. Unfortunately, the exceptions provided do not indicate which. Tracebacks (with some of the paths salted) below.

Traceback (most recent call last):
  File "/home/<###>/.local/lib/python3.5/site-packages/urllib3/connectionpool.py", line 601, in urlopen
    chunked=chunked)
  File "/home/<###>/.local/lib/python3.5/site-packages/urllib3/connectionpool.py", line 387, in _make_request
    six.raise_from(e, None)
  File "<string>", line 2, in raise_from
  File "/home/<###>/.local/lib/python3.5/site-packages/urllib3/connectionpool.py", line 383, in _make_request
    httplib_response = conn.getresponse()
  File "/usr/lib/python3.5/http/client.py", line 1198, in getresponse
    response.begin()
  File "/usr/lib/python3.5/http/client.py", line 297, in begin
    version, status, reason = self._read_status()
  File "/usr/lib/python3.5/http/client.py", line 266, in _read_status
    raise RemoteDisconnected("Remote end closed connection without"
http.client.RemoteDisconnected: Remote end closed connection without response

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/opt/<###>/app.py", line 68, in <module>
    generate_map(token)
  File "/opt/<###>/app.py", line 44, in generate_map
    png = m._to_png(delay=60)
  File "/home/<###>/.local/lib/python3.5/site-packages/folium/folium.py", line 313, in _to_png
    driver.execute_script('document.write(\"{}\")'.format(html))
  File "/home/<###>/.local/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 635, in execute_script
    'args': converted_args})['value']
  File "/home/<###>/.local/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 318, in execute
    response = self.command_executor.execute(driver_command, params)
  File "/home/<###>/.local/lib/python3.5/site-packages/selenium/webdriver/remote/remote_connection.py", line 375, in execute
    return self._request(command_info[0], url, body=data)
  File "/home/<###>/.local/lib/python3.5/site-packages/selenium/webdriver/remote/remote_connection.py", line 402, in _request
    resp = http.request(method, url, body=body, headers=headers)
  File "/home/<###>/.local/lib/python3.5/site-packages/urllib3/request.py", line 70, in request
    **urlopen_kw)
  File "/home/<###>/.local/lib/python3.5/site-packages/urllib3/request.py", line 148, in request_encode_body
    return self.urlopen(method, url, **extra_kw)
  File "/home/<###>/.local/lib/python3.5/site-packages/urllib3/poolmanager.py", line 321, in urlopen
    response = conn.urlopen(method, u.request_uri, **kw)
  File "/home/<###>/.local/lib/python3.5/site-packages/urllib3/connectionpool.py", line 639, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "/home/<###>/.local/lib/python3.5/site-packages/urllib3/util/retry.py", line 357, in increment
    raise six.reraise(type(error), error, _stacktrace)
  File "/home/<###>/.local/lib/python3.5/site-packages/urllib3/packages/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/home/<###>/.local/lib/python3.5/site-packages/urllib3/connectionpool.py", line 601, in urlopen
    chunked=chunked)
  File "/home/<###>/.local/lib/python3.5/site-packages/urllib3/connectionpool.py", line 387, in _make_request
    six.raise_from(e, None)
  File "<string>", line 2, in raise_from
  File "/home/<###>/.local/lib/python3.5/site-packages/urllib3/connectionpool.py", line 383, in _make_request
    httplib_response = conn.getresponse()
  File "/usr/lib/python3.5/http/client.py", line 1198, in getresponse
    response.begin()
  File "/usr/lib/python3.5/http/client.py", line 297, in begin
    version, status, reason = self._read_status()
  File "/usr/lib/python3.5/http/client.py", line 266, in _read_status
    raise RemoteDisconnected("Remote end closed connection without"
urllib3.exceptions.ProtocolError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response',))

@Conengmo
Copy link
Member

Conengmo commented Sep 29, 2018

@nderen please don't post this in an old PR, open a new issue for it. You can link to this PR if needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants