简体   繁体   English

在 Jupyter 中,您如何判断您使用的是哪个浏览器?

[英]In Jupyter, how do you tell which browser you are in?

In my Jupyter notebook, I need to know whether I am running in Chrome or Firefox because the code to save a figure in Altair is different depending on the browser ( https://altair-viz.github.io/user_guide/saving_charts.html ).在我的 Jupyter 笔记本中,我需要知道我是在 Chrome 还是 Firefox 中运行,因为在 Altair 中保存图形的代码因浏览器而异( https://altair-viz.github.io/user_guide/saving_charts.html )。

How can I do this?我怎样才能做到这一点?

The Jupyter kernel (ie Python backend) has no direct information about the frontend that is connected to it. Jupyter 内核(即 Python 后端)没有关于连接到它的前端的直接信息。 But you can use system tools to try to infer what browser processes are running.但是您可以使用系统工具来尝试推断正在运行的浏览器进程。 For example, the psutil module allows you to list running processes.例如, psutil模块允许您列出正在运行的进程。 I have a Chrome and Safari browsers open currently, and I get these results:我当前打开了 Chrome 和 Safari 浏览器,得到以下结果:

>>> import psutil
>>> 'Google Chrome' in (p.name() for p in psutil.process_iter())            
True
>>> 'Firefox' in (p.name() for p in psutil.process_iter())                  
False
>>> 'Safari' in (p.name() for p in psutil.process_iter())                   
True

Some caveats:一些注意事项:

  • the process name might vary from operating system to operating system: I would check on that if it's important to work consistently across platforms.进程名称可能因操作系统而异:如果跨平台一致工作很重要,我会检查这一点。
  • this does not tell you whether the user is using this particular frontend for viewing the Jupyter notebook, but whether the process of that name is running at all.这不会告诉您用户是否正在使用此特定前端来查看 Jupyter 笔记本,而是告诉您该名称的进程是否正在运行。
  • For saving altair charts, note that the browser alone is not enough: you'll also need the user to have chromedriver installed for Chrome, or geckodriver installed for Firefox.要保存 altair 图表,请注意仅浏览器是不够的:您还需要用户为 Chrome 安装 chromedriver,或为 Firefox 安装 geckodriver。 It may be better to detect whether those drivers are installed rather than detecting the currently running browser.最好检测是否安装了这些驱动程序,而不是检测当前正在运行的浏览器。 See Saving Charts for more information.有关更多信息,请参阅保存图表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM