Skip to content

Instantly share code, notes, and snippets.

View hansthen's full-sized avatar

Hans Then hansthen

  • The Netherlands
View GitHub Profile
@hansthen
hansthen / bin_counter.py
Created March 14, 2022 22:35
Determine bins from a duration
from datetime import timedelta, datetime
from collections import Counter
def bin_days(d1, d2):
counter = Counter()
d_next = d1.replace(hour = 0, minute=0, second=0) + timedelta(days=1)
weekday = d1.weekday()
counter[weekday] += (d_next - d1).total_seconds() / (24 * 60 * 60)
delta = d2 - d_next
@hansthen
hansthen / resample.psql
Last active January 7, 2023 19:39
Postgres query to resample data to a specific interval (based on something from SO, but adapted use only one table)
SELECT a.user_id, b.balance, d.as_of_date
FROM (
SELECT d AS as_of_date
FROM generate_series(timestamp '2015-12-29', '2016-01-10', interval '1 hour') d
) d
JOIN LATERAL (
select user_id, as_of_date
from balances
where as_of_date <= d.as_of_date
order by as_of_date
# code is from streamlit forums https://discuss.streamlit.io/t/ag-grid-component-with-input-support/8108/385
df=pd.DataFrame({ "Name": ['Erica', 'Rogers', 'Malcolm', 'Barrett'], "Age": [43, 35, 57, 29]})
add_row_func = """function(e)
{ let api = e.api;
let rowIndex = e.rowIndex + 1;
let vnme = e.data.Name;
let vage = e.data.Age;
@hansthen
hansthen / vimrc
Created May 9, 2023 21:37
My vimrc
set nocompatible " be iMproved, required
filetype off " required
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
" let Vundle manage Vundle, required
Plugin 'VundleVim/Vundle.vim'
Plugin 'tpope/vim-fugitive'
Plugin 'scrooloose/nerdtree'
create materialized view mview
as
select a.userid, a.balance, d.date
from (select d as date from generate_series(timestamp '2023-05-01', '2023-06-2', interval '1 day') d) d
join lateral (select userid, balance, date from balance where date <= d.date order by date desc limit 1) a
on true union select userid, balance, date from balance order by date;
create materialized view mview2
as
select a.userid, a.balance, a.date as start, b.date as fin
@hansthen
hansthen / filter select field.py
Last active July 6, 2023 20:47 — forked from mrjoes/test.py
How to customize options in QuerySelectField.High level idea:1. Hook `create_form` to change options when creating model and `edit_form` when editing model2. Provide different `query_factory` for the field3. Do filtering/population logic in the _get_parent_listAlternatively:1. Can hook `scaffold_form` and change `form.parent.kwargs['query_factor…
from flask import Flask, request, url_for
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.admin.contrib import sqlamodel
from flask.ext import admin
# Create application
app = Flask(__name__)
# Create dummy secrey key so we can use sessions
@hansthen
hansthen / tdist.py
Created October 26, 2023 07:19
Compare two timeseries for distance
import pandas as pd
import numpy as np
import math
from datetime import datetime, timedelta
df1 = pd.DataFrame()
df1["time"] = pd.date_range('08/12/2021 00:00:01', periods=6, freq="4S")
df1['values'] = [1, 2, 3, 4, 5, 6]
df2 = pd.DataFrame()
@hansthen
hansthen / gist:8696a182a32b49c6927e34315f7bb9e5
Created November 10, 2023 06:44
fullcalendar double click
eventDidMount: function (event) {
event.el.addEventListener('dblclick', () => {
console.log(event);
});
},
@hansthen
hansthen / flow.py
Created December 3, 2023 17:41
Page flow
import streamlit as st
class App():
def __init__(self, default=None):
self.page = default
self.args = []
self.kwargs = {}
def goto(self, page, *args, **kwargs):
self.page = page
@hansthen
hansthen / app.py
Created December 5, 2023 03:49
Customize styling for Aggrid
import streamlit as st
from st_aggrid import AgGrid
import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/fivethirtyeight/data/master/airline-safety/airline-safety.csv')
custom_css = {
".ag-row-even": {"background-color": "rgba(0,0,0,0.2) !important"},
".ag-header-cell": {"background-color": "orange !important"},
".ag-theme-streamlit": {"--ag-borders": "none",