Skip to content

Instantly share code, notes, and snippets.

@DinisCruz DinisCruz/bokeh.py
Last active May 15, 2019

Embed
What would you like to do?
Jupyter OSBot-Commands
#https://bokeh.pydata.org/
#http://bokeh.pydata.org/en/latest/docs/installation.html
#https://nbviewer.jupyter.org/github/bokeh/bokeh-notebooks/blob/master/quickstart/quickstart.ipynb
!pip install bokeh
from bokeh.plotting import figure
from bokeh.io import output_notebook, show
output_notebook()
from numpy import cos, linspace
x = linspace(-6, 60, 1200)
y = cos(x)
p = figure(width=500, height=500)
p.circle(x, y, size=7, color="firebrick", alpha=0.5)
show(p)
# Reall cool example
from bokeh.sampledata.autompg import autompg
grouped = autompg.groupby("yr")
mpg = grouped.mpg
avg, std = mpg.mean(), mpg.std()
years = list(grouped.groups)
american = autompg[autompg["origin"]==1]
japanese = autompg[autompg["origin"]==3]
p = figure(title="MPG by Year (Japan and US)")
p.vbar(x=years, bottom=avg-std, top=avg+std, width=0.8,
fill_alpha=0.2, line_color=None, legend="MPG 1 stddev")
p.circle(x=japanese["yr"], y=japanese["mpg"], size=10, alpha=0.5,
color="red", legend="Japanese")
p.triangle(x=american["yr"], y=american["mpg"], size=10, alpha=0.3,
color="blue", legend="American")
p.legend.location = "top_right"
show(p)
#---- get issue
from osbot_jira.api.jira_server.API_Jira import API_Jira
api_jira = API_Jira()
#---- take screenshot
from osbot_aws.apis.Lambda import Lambda
lambda_name = 'osbot_browser.lambdas.lambda_browser'
lambda_browser = Lambda(lambda_name)
payload = {"params": ['screenshot','https://news.bbc.co.uk']}
png_data = lambda_browser.invoke(payload)
from IPython.display import display_html
html = '<img style="margin:0" align="left" src="data:image/png;base64,{}"/>'.format(png_data)
display_html(html, raw=True)
# install dependencies
!pip install osbot_aws boto3 pbx_gs_python_utils osbot_jira jira
# list current servers
!jupyter notebook list
# stop server at 8888
!jupyter notebook stop 8888
#bash commands
apt-get install -y libnss3 libgconf-2-4
pip install nest_asyncio
pip install syncer, pyppeteer
pip install osbot_browser osbot_aws pbx_gs_python_utils boto3
# cell code (first version)
import nest_asyncio
nest_asyncio.apply()
import os
from osbot_browser.browser.Browser_Lamdba_Helper import Browser_Lamdba_Helper
url = 'https://www.google.com/asd'
#url = 'http://localhost:8888'
print(os.getenv('AWS_REGION'))
browser = Browser_Lamdba_Helper().setup().api_browser
browser.sync__open(url)
#print(browser.sync__url())
png_data = browser.sync__screenshot_base64(url)
from IPython.display import display_html
html = '<img style="margin:0" align="left" src="data:image/png;base64,{}"/>'.format(png_data)
display_html(html, raw=True)
### Better version
import nest_asyncio
import os
from IPython.display import display_html
from osbot_browser.browser.Browser_Lamdba_Helper import Browser_Lamdba_Helper
nest_asyncio.apply()
def open_url(url):
browser = Browser_Lamdba_Helper().setup().api_browser
browser.sync__open(url)
png_data = browser.sync__screenshot_base64(url)
html = '<img style="margin:0" align="left" src="data:image/png;base64,{}"/>'.format(png_data)
display_html(html, raw=True)
open_url('https://www.google.com/asd')
%%javascript
document.onpaste = function(event) {
var items = event.clipboardData.items;
console.log(JSON.stringify(items)); // will give you the mime types
var blob = items[0].getAsFile();
var reader = new FileReader();
reader.onload = function(event){
console.log(event.target.result)
Jupyter.notebook.cells_to_markdown()
Jupyter.notebook.get_selected_cell().set_text('<img style="border:1px solid black" align="left" src="'
+event.target.result +
'"/>')
Jupyter.notebook.get_selected_cell().execute()
};
reader.readAsDataURL(blob);
}
//--- execute code in cell
code = Jupyter.notebook.get_selected_cell()
code.set_text('40+3') ; code.execute() ;
code.output_area.element.find('.output_result').text() // after execution, this is where the answer will be
//--- other commands
Jupyter.notebook.cells_to_markdown() // change in ui the current cell to markdown
Jupyter.notebook.cells_to_code() // change in ui the current cell to markdown
Jupyter.notebook.delete_cell()
Jupyter.notebook.get_cells()
Jupyter.notebook.insert_cell_below();
Jupyter.notebook.select_next(true);
Jupyter.notebook.focus_cell();
Jupyter.notebook.get_selected_cell().set_text('40+3')
Jupyter.notebook.get_selected_cell().execute();
//--- UI changes
Jupyter.toolbar.add_buttons_group([
{
'label' : 'alert me',
'icon' : 'fa-terminal', // select your icon from
// http://fontawesome.io/icons/
'callback': function(){alert(42)}
}
// add more button here if needed.
]);
#!pip install lightning-python
from lightning import Lightning
from numpy import random
lgn = Lightning(ipython=True, local=True)
#series = random.randn(10, 50)
#lgn.line(series)
# x = random.randn(1000)
# y = random.randn(1000)
# v = random.randn(1000)
# lgn.scatter(x, y, alpha=0.5, values=v, colormap='Reds')
mat = random.rand(100,100)
mat[mat<0.97] = 0
lgn.force(mat)
#https://jupyter-notebook.readthedocs.io/en/stable/examples/Notebook/JavaScript%20Notebook%20Extensions.html
from jupyter_core.paths import jupyter_config_dir
jupyter_dir = jupyter_config_dir()
jupyter_dir
# ---- Create notebook from notebook
import nbformat as nbf
nb = nbf.v4.new_notebook()
text = """\
# My first automatic Jupyter Notebook
This is an auto-generated notebook."""
code = """\
%pylab inline
hist(normal(size=2000), bins=50);"""
nb['cells'] = [nbf.v4.new_markdown_cell(text),
nbf.v4.new_code_cell(code) ]
nbf.write(nb, 'test.ipynb')
import numpy as np
import pandas as pd
import qgrid
randn = np.random.randn
df_types = pd.DataFrame({
'A' : pd.Series(['2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04',
'2013-01-05', '2013-01-06', '2013-01-07', '2013-01-08', '2013-01-09'],index=list(range(9)),dtype='datetime64[ns]'),
'B' : pd.Series(randn(9),index=list(range(9)),dtype='float32'),
'C' : pd.Categorical(["washington", "adams", "washington", "madison", "lincoln","jefferson", "hamilton", "roosevelt", "kennedy"]),
'D' : ["test 123", "bar", "buzz", "bippity","boppity", "foo", "foo", "bar", "zoo"] })
df_types['E'] = df_types['D'] == 'foo'
qgrid_widget = qgrid.show_grid(df_types, show_toolbar=True)
qgrid_widget
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.