Is it possible to use nbconvert to export a Bokeh plot to a single html file? - jupyter-notebook

I tried to use nbconvert > HTML and it returns random bokeh logos
here is my code:
import pandas as pd
from bokeh.plotting import *
output_notebook()
df = pd.DataFrame({'rev':[234,345,657,7]})
df
line(df['rev'],df['rev'], color="red", line_width=2,
title="Archimean", legend="Archimedean")
show()

A small CSS bug made it into 0.5.0 that affects nbconvert output. There is alread a fix checked into master. We will be releasing 0.5.1 early next week.

Related

Change background color of output cell in JupyterLab

I am using pygments to highlight some syntax in a jupyter notebook. Here a minimal code to reproduce the issue:
from IPython.display import display, HTML
from pygments import highlight
from pygments.lexers import SqlLexer
from pygments.formatters import HtmlFormatter
from helpers.mystyle import GhDarkStyle
fmtter = HtmlFormatter(style=GhDarkStyle)
query = """
SELECT
*
FROM
latest.tmp
"""
display(
HTML(
'<style type="text/css">{}</style> {}'.format(
fmtter.get_style_defs(".highlight"), highlight(query, SqlLexer(), fmtter)
)
)
)
the style I use is just a copy from the official pygments repo (https://github.com/pygments/pygments/blob/master/pygments/styles/gh_dark.py) in order to have better control on the elements.
The output of the display is correct running the code in a classical jupyter notebook:
but it fails within jupyter lab:
I am not an expert with css, so it is not clear to me where the error comes from (either jupyter-lab or pygments). Investigating a bit, I find that if I explicitly add the highligh attribute to the single <span> classes generated from the highlight function, at least I get to show a different color for the line background:
which however is suboptimal in my opinion.
I use:
pygments: 2.14.0
jupyter-lab: 3.5.3
in general everything contained in jupyter/scipy-notebook:python-3.10.8 docker image

from import from python in reticulate

Any way to do the from - import from Python in Reticulate without having to import the whole module?
example: from tensorflow import keras
I know about the solution below, but it imports the whole module anyway...
keras <- import('tensorflow')$keras
I am not sure, but if you are writing your r code in an rmd file, wouldn't you be able to do it directly in a python chunk like this:
python
from ... import ...

loading bokehJS in jupyter lab

I am having a strange behavior with bokeh library.
So apparently, I am trying to create some beautiful plots with bokeh. When I try to load the bokeh in JupyterLab, I get constant, "Loading bokehJS..." message but if I try to plot with jupyter (from bokeh.io import output_notebook, show output_notebook()), I could plot it. It looks like lab has some issues? or am I not using lab properly?
comparative screenshot attached. Dark (JupyterLab), Light(Jupyter Notebook)
You likely need to install the jupyterlab_bokeh extension:
https://docs.bokeh.org/en/latest/docs/user_guide/notebook.html#jupyterlab

Jupyter Notebook cell - Repeat same imports?

I have multiple Notebook cells where im importing the same libraries.
Is there a way to import these libraries once (like in the very 1st cell) and allow all other cells to use them?
Since my notebook has several cells, each importing the same libraries, i'm starting to see alot of redundancies.
Of course! You can add all your libraries at the very top of your notebook. All of them will be available for use.
import pandas as pd
import numpy as np
import matolotlib.pyplot as plt
import seaborn as sns

Ipython Notebook: Default Initialization Python Code

I use iPython Notebook for scientific applications and I'm always plotting experimental data sets. The default image rendering and color cycling behavior of the combination of iPython Notebook and Matplotlib is pretty bad, but it's great after the following tweaks.
# ipython notebook specific
%pylab inline
# imports
from matplotlib import pyplot as plt
import matplotlib as mpl
from seaborn.apionly import set_palette
from IPython.display import set_matplotlib_formats
# configure plotting
set_matplotlib_formats('pdf', 'svg')
set_palette('Set1', n_colors=15, desat=None)
mpl.rcParams['figure.figsize']=(12.0,2.0)
I don't want to have to enter these manually in every notebook. How can I have these executed for all notebooks I'll ever create?
You can create Python files in $HOME/.ipython/profile_default/startup/ that contains the code you want executed at startup.
For example, you can create $HOME/.ipython/profile_default/startup/00-init.py:
# imports
from matplotlib import pyplot as plt
import matplotlib as mpl
from seaborn.apionly import set_palette
from IPython.display import set_matplotlib_formats
# configure plotting
set_matplotlib_formats('pdf', 'svg')
set_palette('Set1', n_colors=15, desat=None)
mpl.rcParams['figure.figsize']=(12.0,2.0)
Note that IPython magics are not supported here, so %matplotlib inline won't work. This question deals with making %matplotlib inline the default.
You should be aware that if you change the defaults for your IPython environment, your notebooks may not work on other people's IPython installations.

Resources