Skip to content

Instantly share code, notes, and snippets.

View NickCrews's full-sized avatar

Nick Crews NickCrews

View GitHub Profile
@tvst
tvst / is_running_in_streamlit.py
Created May 20, 2020 20:35
Function to detect if the current script is running inside Streamlit
import threading
def is_running_in_streamlit():
thread = threading.current_thread()
return type(thread).__module__.startswith('streamlit.')
@benwattsjones
benwattsjones / gmail_mbox_parser.py
Last active December 21, 2024 04:48
Quick python code to parse mbox files, specifically those used by GMail. Extracts sender, date, plain text contents etc., ignores base64 attachments.
#! /usr/bin/env python3
# ~*~ utf-8 ~*~
import mailbox
import bs4
def get_html_text(html):
try:
return bs4.BeautifulSoup(html, 'lxml').body.get_text(' ', strip=True)
except AttributeError: # message contents empty
@glombard
glombard / reference-markdown-metadata-from-jinja-template.py
Last active May 30, 2023 09:19
How to use Markdown as a filter in a Jinja2 template, and then extract the Markdown Meta property directly from the template. Assuming you want to use the Meta-data value before rendering the converted Markdown content (e.g. in the html head), the trick is to render the markdown first, save it to a variable (html_content in this example) using a…
from pprint import pprint
import jinja2
import markdown
HTML_TEMPLATE = """{% macro get_html() %}
{{ content | markdown }}
{% endmacro %}
{% set html_content = get_html() %}
Title from Markdown meta-data: {{ get_title() }}