Skip to content

Commit f23d00a

Browse files
committed
Introduce PlaywrightManager helper module
1 parent a10e94c commit f23d00a

File tree

2 files changed

+80
-51
lines changed

2 files changed

+80
-51
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
module Charty
2+
module Backends
3+
module BackendHelpers
4+
module PlaywrightManager
5+
@_playwright_exec = nil
6+
@_browser = nil
7+
@_context = nil
8+
9+
at_exit do
10+
if @_context
11+
@_context.close
12+
@_context = nil
13+
end
14+
if @_browser
15+
@_browser.close
16+
@_browser = nil
17+
end
18+
if @_playwright_exec
19+
@_playwright_exec.stop
20+
@_playwright_exec = nil
21+
end
22+
end
23+
24+
module_function def playwright
25+
unless @_playwright_exec
26+
load_playwright
27+
path = ENV.fetch("PLAYWRIGHT_CLI_EXECUTABLE_PATH", "npx playwright")
28+
@_playwright_exec = Playwright.create(playwright_cli_executable_path: path)
29+
end
30+
@_playwright_exec.playwright
31+
end
32+
33+
module_function def launch_browser
34+
playwright.chromium.launch(headless: true)
35+
end
36+
37+
module_function def default_browser
38+
unless @_browser
39+
@_browser = launch_browser
40+
end
41+
@_browser
42+
end
43+
44+
module_function def default_context
45+
unless @_context
46+
@_context = default_browser.new_context
47+
end
48+
@_context
49+
end
50+
51+
module_function def new_page(&block)
52+
page = default_context.new_page
53+
return page unless block
54+
55+
begin
56+
return block.call(page)
57+
ensure
58+
page.close
59+
end
60+
end
61+
62+
module_function def load_playwright
63+
require "playwright"
64+
rescue LoadError
65+
$stderr.puts "ERROR: You need to install playwright and playwright-ruby-client before using Plotly renderer"
66+
raise
67+
end
68+
end
69+
end
70+
end
71+
end

lib/charty/backends/plotly.rb

Lines changed: 9 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
require_relative "plotly_helpers/html_renderer"
66
require_relative "plotly_helpers/notebook_renderer"
77
require_relative "plotly_helpers/plotly_renderer"
8+
require_relative "backend_helpers/playwright_manager"
89

910
module Charty
1011
module Backends
@@ -918,60 +919,17 @@ def self.mathjax_config
918919
end
919920
end
920921

921-
@playwright_fiber = nil
922-
923-
def self.ensure_playwright
924-
if @playwright_fiber.nil?
925-
begin
926-
require "playwright"
927-
rescue LoadError
928-
$stderr.puts "ERROR: You need to install playwright and playwright-ruby-client before using Plotly renderer"
929-
raise
930-
end
922+
def self.render_image(input, output, format, element_id, width, height)
923+
BackendHelpers::PlaywrightManager.new_page do |page|
924+
page.set_viewport_size(width: width, height: height)
925+
page.goto("file://#{input}")
926+
element = page.query_selector("\##{element_id}")
931927

932-
@playwright_fiber = Fiber.new do
933-
playwright_cli_executable_path = ENV.fetch("PLAYWRIGHT_CLI_EXECUTABLE_PATH", "npx playwright")
934-
Playwright.create(playwright_cli_executable_path: playwright_cli_executable_path) do |playwright|
935-
playwright.chromium.launch(headless: true) do |browser|
936-
request = Fiber.yield
937-
loop do
938-
result = nil
939-
case request.shift
940-
when :finish
941-
break
942-
when :render
943-
input, output, format, element_id, width, height = request
944-
945-
page = browser.new_page
946-
page.set_viewport_size(width: width, height: height)
947-
page.goto("file://#{input}")
948-
element = page.query_selector("\##{element_id}")
949-
950-
kwargs = {type: format}
951-
kwargs[:path] = output unless output.nil?
952-
result = element.screenshot(**kwargs)
953-
end
954-
request = Fiber.yield(result)
955-
end
956-
end
957-
end
958-
end
959-
@playwright_fiber.resume
928+
kwargs = {type: format}
929+
kwargs[:path] = output unless output.nil?
930+
element.screenshot(**kwargs)
960931
end
961932
end
962-
963-
def self.terminate_playwright
964-
return if @playwright_fiber.nil?
965-
966-
@playwright_fiber.resume([:finish])
967-
end
968-
969-
at_exit { terminate_playwright }
970-
971-
def self.render_image(input, output, format, element_id, width, height)
972-
ensure_playwright if @playwright_fiber.nil?
973-
@playwright_fiber.resume([:render, input, output, format.to_s, element_id, width, height])
974-
end
975933
end
976934
end
977935
end

0 commit comments

Comments
 (0)