Skip to content

Instantly share code, notes, and snippets.

@JaimeEnergy
JaimeEnergy / bokeh_datetime_axis.py
Created April 14, 2017 08:55
Bokeh background rectangle that covers an entire plot including panable area
def d64(start, end):
""" find the midpoints of two dates and return as numpy int64"""
midpoint = start + (end-start)/2
print(start, end, midpoint)
return np.datetime64(midpoint).astype('datetime64[ms]').view(np.int64)
xrange = Range1d(q.start_dt,q.end_dt)
plot = figure(plot_width=850, plot_height=100, x_range=xrange)
plot.xaxis[0].ticker = DatetimeTicker()
@JaimeEnergy
JaimeEnergy / pandas_from_sql_server.py
Created April 14, 2017 08:45
Connect to SQL Server, Postgres, and SQLite3 in Python
import pypyodbc
import pandas as pd
CONNECTION_STRING = """
Driver={{{DRIVER}}};
Server={SERVER};
Database={DATABASE};
""".format(**locals())
df = pd.read_sql(con=pypyodbc.connect(CONNECTION_STRING), sql=sql)
@JaimeEnergy
JaimeEnergy / prevent_vertical_scrolling.py
Created April 14, 2017 08:11
Bokeh Gantt Chart: Use CustomJS to prevent vertical scrolling
from bokeh.models.callbacks import CustomJS
jscode="""
range.set('start', parseInt(%s));
range.set('end', parseInt(%s));
"""
yrange = subplot.y_range
yrange.start, yrange.end = 0, plot_height
subplot.y_range.callback = CustomJS(
@JaimeEnergy
JaimeEnergy / months_in_interval.py
Created April 14, 2017 06:56
Generate list of months between interval in python
# http://stackoverflow.com/questions/34898525/generate-list-of-months-between-interval-in-python
from datetime import datetime, timedelta
from collections import OrderedDict
dates = ["2014-10-10", "2016-01-07"]
start, end = [datetime.strptime(_, "%Y-%m-%d") for _ in dates]
OrderedDict(((start + timedelta(_)).strftime(r"%b-%y"), None) for _ in xrange((end - start).days)).keys()
#['Oct-14', 'Nov-14', 'Dec-14', 'Jan-15', 'Feb-15', 'Mar-15', 'Apr-15',
# 'May-15', 'Jun-15', 'Jul-15', 'Aug-15', 'Sep-15', 'Oct-15', 'Nov-15',
@JaimeEnergy
JaimeEnergy / 0_reuse_code.js
Created April 14, 2017 06:52
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console