jrosell commited on
Commit
9ccff4d
·
1 Parent(s): f11cb19
Files changed (5) hide show
  1. Dockerfile +18 -1
  2. README.md +18 -33
  3. app.R +63 -24
  4. deploy_docker.sh → docker_rm.sh +2 -4
  5. docker_run.sh +2 -0
Dockerfile CHANGED
@@ -13,6 +13,23 @@ RUN set -eux; \
13
  git \
14
  sudo \
15
  sqlite3 \
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  ca-certificates \
17
  gcc \
18
  libc6-dev \
@@ -37,7 +54,7 @@ RUN set -eux; \
37
  rm -rf /var/lib/apt/lists/*;
38
 
39
  # Install R packages
40
- RUN install2.r --error -s --deps TRUE htmltools tibble dplyr purrr rlang glue this.path DBI pool RSQLite remotes promises assertthat log here zeallot dbplyr stringr tidyverse
41
  RUN Rscript -e "install.packages('b64', repos = c('https://extendr.r-universe.dev', getOption('repos')))"
42
  RUN Rscript -e "install.packages('uwu', repos = c('https://josiahparry.r-universe.dev', getOption('repos')))"
43
  RUN --mount=type=secret,id=GITHUB_PAT GITHUB_PAT=$(cat /run/secrets/GITHUB_PAT) \
 
13
  git \
14
  sudo \
15
  sqlite3 \
16
+ make \
17
+ libcurl4-openssl-dev \
18
+ libssl-dev \
19
+ libxml2-dev \
20
+ zlib1g-dev \
21
+ libfontconfig1-dev \
22
+ libfreetype6-dev \
23
+ libpng-dev \
24
+ libtiff5-dev \
25
+ libjpeg-dev \
26
+ libharfbuzz-dev \
27
+ libfribidi-dev \
28
+ libpq-dev \
29
+ libgit2-dev \
30
+ gdal-bin libgdal-dev \
31
+ libudunits2-dev \
32
+ pandoc \
33
  ca-certificates \
34
  gcc \
35
  libc6-dev \
 
54
  rm -rf /var/lib/apt/lists/*;
55
 
56
  # Install R packages
57
+ RUN install2.r --error -s --deps TRUE htmltools tibble dplyr purrr rlang glue this.path DBI pool RSQLite remotes promises assertthat log here zeallot dbplyr stringr tidyverse rmarkdown plotly
58
  RUN Rscript -e "install.packages('b64', repos = c('https://extendr.r-universe.dev', getOption('repos')))"
59
  RUN Rscript -e "install.packages('uwu', repos = c('https://josiahparry.r-universe.dev', getOption('repos')))"
60
  RUN --mount=type=secret,id=GITHUB_PAT GITHUB_PAT=$(cat /run/secrets/GITHUB_PAT) \
README.md CHANGED
@@ -16,27 +16,28 @@ experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](h
16
 
17
  **THIS IS A WORK IN PROGRESS, DO NOT USE**
18
 
19
-
20
- ## Deployment options
21
-
22
-
23
- ### Hugging Face Spaces
24
-
25
- When you pushing a repository to a Hugging Face Spaces with the option to deploy Dockerfile and include a Dockerfile, Hugging Face will start building and running your app in the Space.
26
-
27
- Try the demo here:
28
 
29
  * Direct URL to the deployed app: https://jrosell-ambhtmx-plotly.hf.space/
30
  * Space URL: https://huggingface.co/spaces/jrosell/ambhtmx.plotly
31
 
 
32
 
33
- If you have a repo with git enabled with a github hf remote configured, you can run:
 
 
 
34
 
 
35
  ```
36
- bash deploy_hf.sh
37
  ```
38
 
39
- For example, you can adapt this for your gh and hf repos:
 
 
 
 
40
 
41
  ```
42
  git init
@@ -46,36 +47,20 @@ git commit -m "My changes"
46
  git remote add origin [email protected]:jrosell/ambhtmx.plotly.git
47
  git remote add gh [email protected]:jrosell/ambhtmx.plotly.git
48
  git remote add hf [email protected]:spaces/jrosell/ambhtmx.plotly
49
- git push --set-upstream gh main
50
- git push --set-upstream hf main
51
  ```
52
 
53
-
54
- ### Runing the example in Docker
55
-
56
- All in one command:
57
 
58
  ```
59
- bash deploy_docker.sh
60
- ```
61
-
62
- It will run:
63
-
64
- ```
65
- (docker container rm -f ambhtmx-plotly-container || true)\
66
- && (docker rmi $(docker images --format '{{.Repository}}:{{.ID}}'| egrep 'ambhtmx-plotly' | cut -d':' -f2 | uniq) --force || true) \
67
- && docker build -f Dockerfile -t ambhtmx-plotly-image . \
68
- && docker run --env-file=.Renviron -p 7860:7860 --name ambhtmx-plotly-container --rm ambhtmx-plotly-image
69
  ```
70
 
71
-
72
  ## Troubleshooting
73
 
74
- If you want to see the logs:
75
 
76
- ```
77
- docker build -f Dockerfile --no-cache --progress=plain -t ambhtmx-plotly-image . 2>&1 | tee build.log
78
- ```
79
 
80
  Check the [known issues](https://github.com/jrosell/ambhtmx/issues), and if you have another issue? Please, [let me know](https://github.com/jrosell/ambhtmx/issues).
81
 
 
16
 
17
  **THIS IS A WORK IN PROGRESS, DO NOT USE**
18
 
19
+ See the demo here:
 
 
 
 
 
 
 
 
20
 
21
  * Direct URL to the deployed app: https://jrosell-ambhtmx-plotly.hf.space/
22
  * Space URL: https://huggingface.co/spaces/jrosell/ambhtmx.plotly
23
 
24
+ Or test it locally:
25
 
26
+ ```
27
+ git clone [email protected]:jrosell/ambhtmx.plotly.git
28
+ bash docker_run.sh
29
+ ```
30
 
31
+ If you want, you can remove docker containers and images:
32
  ```
33
+ bash docker_rm.sh
34
  ```
35
 
36
+ ## How to use Github and Hugging Face Spaces for my project
37
+
38
+ When you pushing a repository to a Hugging Face Spaces with the option to deploy Dockerfile and include a Dockerfile, Hugging Face will start building and running your app in the Space.
39
+
40
+ Customize this example, to enable Hugging Face Spaces as additional remote:
41
 
42
  ```
43
  git init
 
47
  git remote add origin [email protected]:jrosell/ambhtmx.plotly.git
48
  git remote add gh [email protected]:jrosell/ambhtmx.plotly.git
49
  git remote add hf [email protected]:spaces/jrosell/ambhtmx.plotly
50
+ git remote -v
 
51
  ```
52
 
53
+ Then, to depoly to Hugging Face Spaces run:
 
 
 
54
 
55
  ```
56
+ bash deploy_hf.sh
 
 
 
 
 
 
 
 
 
57
  ```
58
 
 
59
  ## Troubleshooting
60
 
61
+ Kown issues:
62
 
63
+ * Only updates when refreshing the whole page. WIP should rerender on innerHTML replace.
 
 
64
 
65
  Check the [known issues](https://github.com/jrosell/ambhtmx/issues), and if you have another issue? Please, [let me know](https://github.com/jrosell/ambhtmx/issues).
66
 
app.R CHANGED
@@ -1,31 +1,70 @@
1
  # remotes::install_github("jrosell/ambhtmx", force = TRUE)
2
  library(ambhtmx)
3
- # devtools::load_all()
 
4
 
5
- card_3d_demo <- \() {
6
- card <- "card"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  tryCatch({
8
- # Original python code credit: https://fastht.ml/
9
- # Design credit: https://codepen.io/markmiro/pen/wbqMPa
10
- bgurl <- "https://ucarecdn.com/35a0e8a7-fcc5-48af-8a3f-70bb96ff5c48/-/preview/750x1000/"
11
- card_styles <- "font-family: 'Arial Black', 'Arial Bold', Gadget, sans-serif; perspective: 1500px;"
12
- card_3d <- \(text, bgurl, amt, left_align) {
13
- align <- ifelse(left_align, 'left', 'right')
14
- scr <- script_from_js_tpl('card3d.js', amt = amt)
15
- sty <- style_from_css_tpl(
16
- 'card3d.css', bgurl = glue('url({bgurl})'), align = align
17
- )
18
- div(text, div(), scr, sty, align = align)
19
- }
20
- card <- card_3d("Mouseover me", bgurl, amt = 1.5, left_align = T)
21
- },
 
 
 
 
 
 
 
 
22
  error = \(e) print(e)
23
  )
24
- div(card, style = card_styles)
25
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
26
 
27
- ambhtmx(host = "0.0.0.0", port = "7860", protocol ="http")$app$
28
- get("/", \(req, res) {
29
- card_3d_demo() |> send_page(res)
30
- })$
31
- start()
 
1
  # remotes::install_github("jrosell/ambhtmx", force = TRUE)
2
  library(ambhtmx)
3
+ library(ggplot2)
4
+ library(plotly)
5
 
6
+ page_title <- "WIP: ambhtmx slider example"
7
+
8
+ head_tags <- htmltools::tagList()
9
+
10
+ #' Generate a plot from rexp_data
11
+ generate_htmlwidget <- \(){
12
+ print("generate_htmlwidget")
13
+ rexp_df <- tibble(x = 1:length(rexp_data), y = rexp_data)
14
+ p_plot <- ggplot(rexp_df, aes(x, y)) + geom_line()
15
+ plotly::ggplotly(p_plot, width = 200, height = 100)
16
+ }
17
+
18
+ counter <- 1
19
+ rexp_data <- c(rexp(1), rexp(1))
20
+
21
+ #' Starting the app
22
+ c(app, context, operations) %<-% ambhtmx(host = "0.0.0.0", port = "7860")
23
+
24
+
25
+ #' Main page of the app
26
+ app$get("/", \(req, res){
27
+ html <- ""
28
  tryCatch({
29
+ rexp_widget <- generate_htmlwidget()
30
+ rexp_tags <- amb_htmlwidget(rexp_widget, id = "rexp_widget")
31
+ html <- div(
32
+ style = "margin: 20px",
33
+ h1(page_title),
34
+ div(
35
+ id = "counter",
36
+ p(glue("Counter is set to {counter}")),
37
+ rexp_tags
38
+ ),
39
+ button(
40
+ "+1",
41
+ hx_get="/increment",
42
+ hx_target="#counter",
43
+ # hx_target="#rexp_widget [data-for]",
44
+ # hx_target="#debug",
45
+ hx_swap="outerHTML"
46
+ ),
47
+ div(id = "debug")
48
+ ) |>
49
+ send_page(res)
50
+ },
51
  error = \(e) print(e)
52
  )
53
+ })
54
+
55
+ #' Call to return the plot
56
+ app$get("/increment", \(req, res){
57
+ counter <<- counter + 1
58
+ rexp_data <<- c(rexp_data, rexp(1))
59
+ rexp_widget <- generate_htmlwidget()
60
+ rexp_tags <- amb_htmlwidget(rexp_widget, id = "rexp_widget")
61
+ div(
62
+ id = "counter",
63
+ p(glue("Counter is set to {counter}")),
64
+ rexp_tags
65
+ ) |>
66
+ send_tags(res)
67
+ })
68
 
69
+ #' Start the app with all the previous defined routes
70
+ app$start(open = FALSE)
 
 
 
deploy_docker.sh → docker_rm.sh RENAMED
@@ -1,4 +1,2 @@
1
- (docker container rm -f ambhtmx-plotly-container || true)\
2
- && (docker rmi $(docker images --format '{{.Repository}}:{{.ID}}'| egrep 'ambhtmx-plotly-image' | cut -d':' -f2 | uniq) --force || true) \
3
- && docker build -f Dockerfile -t ambhtmx-plotly-image . \
4
- && docker run --env-file=.Renviron -p 7860:7860 --name ambhtmx-plotly-container --rm ambhtmx-plotly-image
 
1
+ (docker container rm -f ambhtmx-plotly-container || true) \
2
+ && (docker rmi $(docker images --format '{{.Repository}}:{{.ID}}'| egrep 'ambhtmx-plotly-image' | cut -d':' -f2 | uniq) --force || true)
 
 
docker_run.sh ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ (docker build -f Dockerfile -t ambhtmx-plotly-image . || true) \
2
+ && docker run --env-file=.Renviron -p 7860:7860 --name ambhtmx-plotly-container --rm ambhtmx-plotly-image