Skip to content

Instantly share code, notes, and snippets.

🌆
Improving

Will Koehrsen WillKoehrsen

View GitHub Profile
View main.js
define(['base/js/namespace', 'base/js/events'], function (Jupyter, events) {
// Template cells including markdown and imports
var setUp = function () {
Jupyter.notebook.insert_cell_at_index('markdown', 0)
.set_text(`# Introduction
State notebook purpose here`)
Jupyter.notebook.insert_cell_at_index('markdown', 1).set_text(`### Imports
Import libraries and write settings here.`)
// Define imports and settings
Jupyter.notebook.insert_cell_at_index('code', 2)
View start.py
import pandas as pd
import numpy as np
# Pandas options
pd.options.display.max_columns = 30
pd.options.display.max_rows = 20
from IPython import get_ipython
ipython = get_ipython()
View reusable.py
def show_stats_by_tag(tag):
return(df.groupby(f'<tag>{tag}').describe()[['views', 'reads']])
stats = interact(show_stats_by_tag,
tag=widgets.Dropdown(options=['Towards Data Science', 'Education',
'Machine Learning', 'Python', 'Data Science']))
View dependent_widgets.py
# Create widgets
directory = widgets.Dropdown(options=['images', 'nature', 'assorted'])
images = widgets.Dropdown(options=os.listdir(directory.value))
# Updates the image options based on directory value
def update_images(*args):
images.options = os.listdir(directory.value)
# Tie the image options to directory value
directory.observe(update_images, 'value')
View datepicker_stats.py
# Create interactive version of function with DatePickers
interact(stats_for_article_published_between,
start_date=widgets.DatePicker(value=pd.to_datetime('2018-01-01')),
end_date=widgets.DatePicker(value=pd.to_datetime('2019-01-01')))
View scatterplot_interact.py
import cufflinks as cf
@interact
def scatter_plot(x=list(df.select_dtypes('number').columns),
y=list(df.select_dtypes('number').columns)[1:],
theme=list(cf.themes.THEMES.keys()),
colorscale=list(cf.colors._scales_names.keys())):
df.iplot(kind='scatter', x=x, y=y, mode='markers',
xTitle=x.title(), yTitle=y.title(),
View dataframe_widgets.py
# Correlation between two columns with dropdown
@interact
def correlations(column1=list(df.select_dtypes('number').columns),
column2=list(df.select_dtypes('number').columns)):
print(f"Correlation: {df[column1].corr(df[column2])}")
# Stats of a column with dropdown
@interact
def describe(column=list(df.columns)):
print(df[column].describe())
View interact_with_specifications.py
# Interact with specification of arguments
@interact
def show_articles_more_than(column=['claps', 'views', 'fans', 'reads'],
x=(10, 100000, 10)):
return df.loc[df[column] > x]
View create_soup.py
from bs4 import BeautifulSoup
# Create a soup and find the table
soup = BeautifulSoup(open(f'stats.html', 'r'))
table = soup.find_all(attrs={'class': "sortableTable-row js-statsTableRow"})
View entry_data_extraction.py
entry = table[0]
# Reading time
read_time = int(entry.find_all(attrs={'class': 'readingTime'})[0].get('title').split(' ')[0])
# Unlisted vs published
type = 'unlisted' if len(entry.find_all(text=' Unlisted')) > 0 else 'published'
# Publication
publication = entry.find_all(attrs={'class': 'sortableTable-text'})
You can’t perform that action at this time.