Skip to content

Instantly share code, notes, and snippets.

View opalczynski's full-sized avatar

Sebastian Opałczyński opalczynski

View GitHub Profile
@opalczynski
opalczynski / data.py
Created October 5, 2021 14:33
Final version of data.py for CLI
from dataclasses import dataclass
from typing import Union
from constants import TaskStatusE
@dataclass
class Project:
id: Union[int, None] # this is because when creating we do not have the ID yet
name: str
@opalczynski
opalczynski / main.py
Created October 5, 2021 14:32
Final version of main.py for CLI
import typer
from database import Database
from data import Project, Task
from constants import TaskStatusE
app = typer.Typer()
project_app = typer.Typer()
task_app = typer.Typer()
project_app.add_typer(task_app, name="tasks")
@opalczynski
opalczynski / database.py
Created October 5, 2021 14:31
Final version of database.py for CLI
import os.path
import sqlite3
from constants import TaskStatusE
class Database:
PROJECT_SQL = """CREATE TABLE project (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name text,
@opalczynski
opalczynski / database.py
Created October 5, 2021 12:49
SQLite database for the tasks management
import os.path
import sqlite3
class Database:
PROJECT_SQL = """CREATE TABLE project (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name text,
description text NULL);"""
TASK_SQL = """CREATE TABLE task (
@opalczynski
opalczynski / cli_bare_bones.py
Created October 5, 2021 12:09
cli_bare_bones - CLI example for blog post
import typer
app = typer.Typer()
project_app = typer.Typer()
task_app = typer.Typer()
project_app.add_typer(task_app, name="tasks")
app.add_typer(project_app, name="projects")
@project_app.command("list")
@opalczynski
opalczynski / pdfprocess.py
Created September 16, 2021 16:54
Generate multiple PDF file from YAML data
import weasyprint
import yaml
from jinja2 import Template
def get_data(data_path="./data.yml"):
with open(data_path, "r") as f:
data = yaml.load(f, Loader=yaml.FullLoader)
# we need to process the data a bit to match the HTML template;
for invoice in data["invoices"]:
@opalczynski
opalczynski / get_data.py
Created September 16, 2021 16:52
Process the data from YAML file
def get_data(data_path="./data.yml"):
with open(data_path, "r") as f:
data = yaml.load(f, Loader=yaml.FullLoader)
# we need to process the data a bit to match the HTML template;
for invoice in data["invoices"]:
# here we simply defined customer and issuer entity in data to avoid lot of typing;
invoice["customer"] = data["customer"][invoice["customer"]]
invoice["issuer"] = data["issuer"][invoice["issuer"]]
# we also needs to add total to items:
total = 0
@opalczynski
opalczynski / data.yml
Created September 16, 2021 16:48
Sample invoice data to work with HTML template
issuer:
issuer1:
name: MyCorp
address: 71 Cherry Court, SO53 5PD SOUTHAMPTON
tax_number: UTR 12345 67890
country: United Kingdom
customer:
customer1:
name: CoolCompany
@opalczynski
opalczynski / template.html
Created September 16, 2021 16:47
Invoice template with jinja2 expressions
<html>
<head>
<meta charset="utf-8">
<link href="invoice.css" media="print" rel="stylesheet">
<title>Invoice</title>
<meta name="description" content="Invoice demo sample">
</head>
<body>
<h1>Invoice</h1>
@opalczynski
opalczynski / render.py
Last active September 16, 2021 16:44
CSS adding to weasyprint
def render_pdf():
template = get_template()
rendered = template.render(**{"name": "John"})
html = weasyprint.HTML(string=rendered, base_url="/")
css = weasyprint.CSS(filename="./template.css")
with open("output/test.pdf", "wb") as f:
html.write_pdf(f, stylesheets=[css])