The situation seems to be quite simple: I am working in a Jupyter Lab file with several Altair plots, which eventually make the file too large to run and to save. Since I don't need to see these plots every single time, I figured I could avoid this by specifying something like plotAltair = True
at the beginning of the script and then nesting each Altair plot in if
statements. As simple as this may sound, for some reason it doesn't appear to work. Am I missing out on something obvious? [edit: turns out I was]
For instance:
import altair as alt
import os
import pandas as pd
import numpy as np
lengths = np.random.randint(0,100,200)
lengths_list = lengths.tolist()
labels = [str(i) for i in lengths_list]
peak_lengths = pd.DataFrame.from_dict({'coords': labels,
'lengths': lengths_list},
orient='columns')
What works:
alt.Chart(peak_lengths).mark_bar().encode(
x = alt.X('lengths:Q', bin=True),
y='count(*):Q'
)
What doesn't work:
plotAltair = True
if plotAltair:
alt.Chart(peak_lengths).mark_bar().encode(
x = alt.X('lengths:Q', bin=True),
y='count(*):Q'
)
** Obs.: I have already attempted to use alt.data_transformers.enable('json')
as a way of reducing file size and it is also not working, but let's please not focus on this but rather on the more simple question.