import os,json,urllib.parse,asyncio,re,html
from pathlib import Path
from playwright.async_api import async_playwright
p=Path(os.environ['TMPDIR']);s=(p/'render-programs-1.html').read_text();c=json.loads(html.unescape(re.search(r'<textarea[^>]*id="table-yaml"[^>]*>(.*?)</textarea>',s,re.S).group(1)))
u='https://numberdb.org/preview?'+urllib.parse.urlencode({'table':json.dumps(c,separators=(',',':'),ensure_ascii=False)})
async def main():
 async with async_playwright() as pw:
  b=await pw.chromium.launch(headless=True,args=['--no-sandbox']);page=await b.new_page(viewport={'width':1440,'height':1100})
  await page.goto('https://numberdb.org/preview',wait_until='networkidle');await page.goto(u,wait_until='networkidle')
  await page.wait_for_timeout(1300)
  await page.locator('code.table-code').scroll_into_view_if_needed()
  await page.wait_for_timeout(700)
  r=await page.locator('code.table-code').evaluate('''e=>({text:e.textContent,visible:e.innerText,css:getComputedStyle(e).cssText,display:getComputedStyle(e).display,visibility:getComputedStyle(e).visibility,contentVisibility:getComputedStyle(e).contentVisibility,font:getComputedStyle(e).fontSize,box:e.getBoundingClientRect().toJSON(),parent:e.parentElement.outerHTML.slice(0,400),parentDisplay:getComputedStyle(e.parentElement).display,parentFont:getComputedStyle(e.parentElement).fontSize,parentBox:e.parentElement.getBoundingClientRect().toJSON()})''')
  (p/'program-render-inspection.json').write_text(json.dumps(r,indent=2)+'\n');print({k:v for k,v in r.items() if k not in ['text','visible','css','parent']})
  await page.screenshot(path=str(p/'render-program-confirmed.png'),full_page=True)
  await b.close()
asyncio.run(main())
