Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add template.json files for all templates #23

Merged
merged 3 commits into from
Sep 5, 2024
Merged

Conversation

gadenbuie
Copy link
Contributor

@gadenbuie gadenbuie commented Aug 22, 2024

For posit-dev/py-shiny#1631 but can be merged at any time. I'm going to wait to merge this when we also update py-shiny-site or at least closer to the next py-shiny release. The updated shiny create workflow removes the _template.json files, but they will not be removed by older versions of shiny.

Create template.json
library(fs)
library(rvest)

apps <- dir_ls(".", recurse = TRUE, glob = "**/app-*.py")
apps <- apps[!grepl("venv", apps)]

for (app in apps) {
  tmpl_json <- path(path_dir(app), "_template.json")
  if (file_exists(tmpl_json)) {
    next
  }

  slug <- path_file(path_dir(app))
  cli::cli_progress_step(slug)
  
  title <- ""
  description <- ""
  tryCatch({
    url <- glue::glue("https://shiny.posit.co/py/templates/{slug}/")
    page <- read_html(url)
  
    title <- page %>% html_node("h1") %>% html_text()
    description <- page %>% html_node(".panel-tabset + p") %>% html_text()
    description <- paste0(strsplit(description, "[.] [A-Z]")[[1]][1], ".")
  }, error = function(err) {
    cli::cli_inform("Failed to scrape template info for {slug}.")
  })

  template <- list(
    name = slug,
    title = title,
    description = description
  )

  jsonlite::write_json(template, tmpl_json, pretty = TRUE, auto_unbox = TRUE)
}

cli::cli_progress_done()

@gadenbuie gadenbuie marked this pull request as ready for review August 27, 2024 15:56
@gadenbuie gadenbuie requested a review from cpsievert August 27, 2024 16:01
@gadenbuie gadenbuie merged commit 8bcea25 into main Sep 5, 2024
@gadenbuie gadenbuie deleted the template-json branch September 5, 2024 20:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants