adding a weasyprint notebook

master
manetta 4 years ago
parent 52a5f396b5
commit ebac31033e

@ -0,0 +1,283 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Weasyprint"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from weasyprint import HTML, CSS\n",
"from weasyprint.fonts import FontConfiguration"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"https://weasyprint.readthedocs.io/en/latest/tutorial.html"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# If you use @font-face in your stylesheet, you would need Weasyprint's FontConfiguration()\n",
"font_config = FontConfiguration()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## HTML"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# small example HTML object\n",
"html = HTML(string='<h1>hello</h1>')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"or ..."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# making an HTML object using our mini-datasets\n",
"import json\n",
"\n",
"f = open('json-dataset.json').read()\n",
"dataset = json.loads(f)\n",
"print(dataset)\n",
"\n",
"content = ''\n",
"\n",
"for word, value in dataset.items():\n",
" content += f'<em>{ word }</em> (<strong>{ value }</strong>)<br />'\n",
" \n",
"html = HTML(string=content)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"or ..."
]
},
{
"cell_type": "code",
"execution_count": 83,
"metadata": {},
"outputs": [],
"source": [
"# making an HTML object using our mini-datasets to insert a layer into a text\n",
"import json, nltk\n",
"\n",
"f = open('json-dataset.json').read()\n",
"dataset = json.loads(f)\n",
"#print(dataset)\n",
"\n",
"txt = open('txt/language.txt').read()\n",
"words = nltk.word_tokenize(txt)\n",
"#print(words)\n",
"\n",
"content = ''\n",
"\n",
"content += '<h1>Language and Software Studies, by Florian Cramer</h1>'\n",
"\n",
"for word in words:\n",
" if word in dataset:\n",
" content += f'<em>{ word }</em> (<strong>{ value }</strong>) '\n",
" else:\n",
" content += f' { word } '\n",
"\n",
"html = HTML(string=content)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## CSS"
]
},
{
"cell_type": "code",
"execution_count": 115,
"metadata": {},
"outputs": [],
"source": [
"css = CSS(string='''\n",
" @page{\n",
" size: A4;\n",
" margin: 15mm;\n",
" background-color: lightgrey;\n",
" font-family: monospace;\n",
" font-size: 8pt;\n",
" color: red;\n",
" border:1px dotted red;\n",
" \n",
" @top-left{\n",
" content: \"natural\";\n",
" }\n",
" @top-center{\n",
" content: \"language\";\n",
" }\n",
" @top-right{\n",
" content: \"artificial\";\n",
" }\n",
" @top-middle{\n",
" content: \"\"\n",
" }\n",
" @left-top{\n",
" content: \"computer control\";\n",
" }\n",
" @right-top{\n",
" content: \"markup\";\n",
" }\n",
" @bottom-left{\n",
" content: \"formal\";\n",
" }\n",
" @bottom-center{\n",
" content: \"programming\";\n",
" }\n",
" @bottom-right{\n",
" content: \"machine\";\n",
" }\n",
" }\n",
" body{\n",
" font-family: serif;\n",
" font-size: 12pt;\n",
" line-height: 1.4;\n",
" color: magenta;\n",
" }\n",
" h1{\n",
" width: 100%;\n",
" text-align: center;\n",
" font-size: 250%;\n",
" line-height: 1.25;\n",
" color: orange;\n",
" }\n",
" strong{\n",
" color: blue;\n",
" }\n",
" em{\n",
" color: green;\n",
" }\n",
"''', font_config=font_config)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## PDF"
]
},
{
"cell_type": "code",
"execution_count": 116,
"metadata": {},
"outputs": [],
"source": [
"html.write_pdf('weasyprint-test.pdf', stylesheets=[css], font_config=font_config)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Previewing the PDF"
]
},
{
"cell_type": "code",
"execution_count": 117,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
" <iframe\n",
" width=\"1024\"\n",
" height=\"600\"\n",
" src=\"weasyprint-test.pdf\"\n",
" frameborder=\"0\"\n",
" allowfullscreen\n",
" ></iframe>\n",
" "
],
"text/plain": [
"<IPython.lib.display.IFrame at 0x7f2e5dcdcb38>"
]
},
"execution_count": 117,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from IPython.display import IFrame\n",
"IFrame(\"weasyprint-test.pdf\", width=1024, height=600)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Loading…
Cancel
Save