Skip to content

Instantly share code, notes, and snippets.

View jonperron's full-sized avatar

Jonathan Perron jonperron

View GitHub Profile
@jonperron
jonperron / testField.js
Last active February 1, 2017 10:19
Check if field content is in an array.
<script type="text/javascript">
var contentArray = [
...
];
var testField = document.getElementById('test_field');
testField.onkeyup = function(){
if(contentArray.indexOf(testField.value) != -1) {
alert("It's in the array !");
} else {
alert("It's not in the array !");
@jonperron
jonperron / gist:733c3ead188f72f0a8a6f39e3d89295d
Last active November 30, 2023 17:29
Serve pandas dataframe as csv with Django
from django.http import HttpResponse
import pandas as pd
def foo():
results = pd.Dataframe()
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename=filename.csv'
results.to_csv(path_or_buf=response,sep=';',float_format='%.2f',index=False,decimal=",")
@jonperron
jonperron / gist:6b925796b727536edbd41d48367b62be
Created December 19, 2016 16:07
Simplest way to generate PDF with Weasyprint (http://weasyprint.readthedocs.io) and a Web Framework. I use Django, but it can be surely used with others + jinja for templating. First, render an HTML page as string, then generate the pdf and serve it as response.
from weasyprint import HTML
response = HttpResponse(content_type='application/pdf')
response['Content-Disposition'] = 'attachment; filename="your_pdf.pdf"'
html_out = str(render(request,"mock_page.html",locals()))
html_out = html_out.replace('Content-Type: text/html; charset=utf-8','')
# base_url is used to locate the CSS file
HTML(string=html_out,base_url = request.build_absolute_uri()).write_pdf(response)
return response
@jonperron
jonperron / gist:ce8ac46d2855289386049420d01f5a7c
Created November 16, 2016 11:18
Jquery Datatables (https://datatables.net/) got a plugin to sort durations. Unfortunately, it does not work when the duration is very long (230:59:59 as an example). This is how I did it, based on http://stackoverflow.com/questions/30946698/custom-sort-durations-using-jquery-datatables. Original SO : http://stackoverflow.com/questions/40285331/s…
$.extend(jQuery.fn.dataTableExt.oSort, {
"duration-pre": function (s) {
var duration;
s = s.toLowerCase();
if(s === 'n/a'){
duration = -1;
} else {
d = s.match(/(?:(\d+):)?(?:(\d+):)?(?:(\d+):)?/);
duration = parseInt(d[1] ? d[1] : 0) * 60 + parseInt(d[2] ? d[2] : 0) + parseInt(d[2] ? d[2] : 0) / 60;
}
@jonperron
jonperron / gist:4676abb810252151fc65663abbe6b6c3
Created November 15, 2016 13:03
Sharing pandas dataframe as xlsx files without (too) much trouble. Using xlsxwriter from http://xlsxwriter.readthedocs.io/working_with_pandas.html
import numpy as np
import pandas as pd
import StringIO
def testExport(request):
# creating a dataframe and doing stuff about it
testDataframe = pd.DataFrame(np.random.randn(1,4), columns=list('ABCD'))
# generate output
@jonperron
jonperron / gist:e74ab46eae5e39cec5e6ea897dc60c4c
Created November 15, 2016 13:00
Python 2.x month generator, using python-dateutil (https://dateutil.readthedocs.io/)
def monthrange(start_date,end_date):
for n in range(relativedelta(end_date,start_date).years * 12 + relativedelta(end_date,start_date).months + 1):
yield start_date + n*relativedelta(months=1)