posit-dev / r-shinylive

https://posit-dev.github.io/r-shinylive/
Other
151 stars 16 forks source link

Prevent export() from clobbering index.html #32

Closed willgearty closed 2 months ago

willgearty commented 10 months ago

I'm looking to have a custom index.html file for my shiny app. Nothing flashy, just a custom <title> and favicon location. The problem is, every time I run shinylive::export() to update app.json, it also clobbers the index.html file. Is there a way to prevent this or to have export() check my app directory for a custom index.html file and use that instead?

AnthonyFucci commented 3 months ago

On a recent project, we added code that runs after shinylive::export so we could support custom and tab favicon.</p> <pre><code class="language-r"> destdir = 'site' title_element_text <- " <title>My Project</title>" link_element_text <- ' <link rel="shortcut icon" href="data:image/png;base64,A_PNG_FILE_BASE64_ENCODED_STRING" type="image/png">' shinylive_file_location <- file.path(destdir, "index.html") shinylive_html_file <- readLines(shinylive_file_location) line_number_of_title_element <- grep("<title>", shinylive_html_file) new_shinylive_html_file <- c( shinylive_html_file[1:(line_number_of_title_element-1)], title_element_text, link_element_text, shinylive_html_file[(line_number_of_title_element+1):(length(shinylive_html_file))] ) writeLines(new_shinylive_html_file, shinylive_file_location)</code></pre> <p>This overwrites the <code>index.html</code> file in destdir. I bet shinylive will eventually support this, and then we can abandon this extra script.</p> </div> </div> <div class="comment"> <div class="user"> <a rel="noreferrer nofollow" target="_blank" href="https://github.com/gadenbuie"><img src="https://avatars.githubusercontent.com/u/5420529?v=4" />gadenbuie</a> commented <strong> 2 months ago</strong> </div> <div class="markdown-body"> <p>The templates themselves live <a href="https://github.com/posit-dev/shinylive/tree/main/export_template">in shinylive in the export_template folder</a>, which both makes sense because it includes shinylive-specific code that might change and also limits our flexibility to change the template because both the R and Python packages rely on the template and are therefore coupled to its format.</p> <p>I see a couple options here that might be worth pursuing:</p> <ol> <li> <p>The most backwards-compatible way forward would be for <code>shinylive::export()</code> to gain an <code>template_dir</code> argument that would be an opportunity to provide a path to a directory containing a modified export template. </p> <p>Users would copy the <code>template_dir</code> from the local shinylive assets (we'd provide instructions in the docs, but would rely on un-exported functions). Then, they'd modify the templates as needed.</p> </li> <li> <p>Additionally, there is clearly a general need to be able to customize typical parts of the template, like the <code>title</code>. We could also add <code>template_params</code>, which would take a list of parameters to be interpolated into the template. We'd use a list instead of individual arguments -- e.g. <code>template_params = list(title = "My Dashboard")</code> instead of <code>title = "My Dashboard"</code> -- so that the template can evolve without requiring API changes in the shinylive packages.</p> <p>If we switch to using glue for the template interpolation, this would also allow custom templates to have their own placeholder and interpolation strings.</p> </li> </ol> </div> </div> <div class="page-bar-simple"> </div> <div class="footer"> <ul class="body"> <li>© <script> document.write(new Date().getFullYear()) </script> Githubissues.</li> <li>Githubissues is a development platform for aggregating issues.</li> </ul> </div> <script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.min.js"></script> <script src="/githubissues/assets/js.js"></script> <script src="/githubissues/assets/markdown.js"></script> <script src="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.4.0/build/highlight.min.js"></script> <script src="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.4.0/build/languages/go.min.js"></script> <script> hljs.highlightAll(); </script> </body> </html>