from sympy.parsing.latex import parse_latex
from sympy import symbols
from notion.client import NotionClient
import os
from notion.block import EquationBlock
RECALCULATION_KEY = "$recalculate"
EQUATION_SIGN_TINY = "="
ROUNDING = '{:.4f}'
TOKEN = os.environ["TOKEN"]
BASE_PAGE = os.environ["BASE_PAGE"]
LOG_LINK = os.environ["LOG_LINK"]
client = NotionClient(token_v2=TOKEN)
page = client.get_block(BASE_PAGE)
children = page.children
MATH = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "=", "\\"]
def format_vars(text):
print(text)
output = text.replace("\$", "\\")
print(output)
return output.split(EQUATION_SIGN_TINY)
def update(block, title, result):
no_result = block.title.split(EQUATION_SIGN_TINY)[:-1]
joined_no_result = EQUATION_SIGN_TINY.join(no_result)
joined_no_result = joined_no_result + EQUATION_SIGN_TINY
thing = joined_no_result + " " + str(result)
block.title = thing.replace("$", "\$")
local = ""
substitution_result = ""
block_link = ""
raw_result = 0
variables = {}
def default():
global local, substitution_result, link, raw_result
local = ""
substitution_result = ""
block_link = ""
raw_result = 0
def reset_global_vars(key, value):
if "{m}" in key or "{y}" in key:
cleared_key = key.replace("\\", "").strip()
variables[cleared_key] = value
else:
cleared_key = key.replace("\\", "").strip()\
.replace("y}", "*y}")\
.replace("m}", "*m}")
variables[cleared_key] = value
log_database = client.get_collection_view(LOG_LINK)
log_rows = log_database.default_query().execute()
for thing in log_rows:
thing.remove()
def log(error):
global local, substitution_result, block_link, raw_result
row = log_database.collection.add_row()
row.set_property("Error", str(error))
row.set_property("Link", block_link)
row.set_property("What is there", str(local))
row.set_property("Substitution", str(substitution_result))
row.set_property("Raw result", str(raw_result))
print(error)
def handle_block(block, is_recalculating):
global local, substitution_result, raw_result
is_equation = block.type == "equation"
is_eq_recalc = is_equation and is_recalculating
if not hasattr(block, "title"):
return is_eq_recalc
title = block.title
is_eq_recalc = RECALCULATION_KEY in title or is_eq_recalc
if (not is_recalculating) or len(title) == 0:
return is_eq_recalc
local = format_vars(title)
if len(local) == 1:
return is_eq_recalc
variable = local[0].strip()
equation = local[1].strip().replace("\\\\", "\\")\
.replace("{m}", "{1*m}")\
.replace("{y}", "{1*y}")
print(variable, " = ", equation)
equation = parse_latex(equation)
if len(equation.free_symbols) > 0:
substitution_result = equation.subs(variables)
raw_result = substitution_result.evalf()
result = ROUNDING.format(raw_result)
reset_global_vars(variable, raw_result)
update(block, local, result)
else:
reset_global_vars(variable, float(local[-1]))
return is_eq_recalc
def go_through(children):
global block_link
is_recalculating_next = False
for block in children:
block_link = ""
try:
block_link = block.get_browseable_url()
block_children = client.get_block(block_link).children
if len(block_children) > 0:
go_through(block_children)
else:
default()
is_recalculating_next = handle_block(block, is_recalculating_next)
except Exception as error:
log(error)
go_through(children)