nx10 / httpgd

Asynchronous http server graphics device for R.
https://nx10.github.io/httpgd
GNU General Public License v2.0
389 stars 19 forks source link

Base URL setting #66

Closed eitsupi closed 3 years ago

eitsupi commented 3 years ago

Thank you for developing a very nice package!

When using R with Docker, httpgd works right away, just install it without any complicated settings.

$ docker run --rm -it -p 8080:8080 rocker/r-ver
> install.packages("httpgd")
> httpgd::hgd(host = "0.0.0.0", port = 8080)
httpgd server running at:
  http://a02b6b09d6a0:8080/live?token=BTbNxZtk
> plot(mtcars)

However, the hostname of the displayed URL is the name of the container, not the URL you actually want to access (eg http://localhost:8080/live?token=BTbNxZtk).

If we can set the URL to be displayed, we can open it with a browser just by clicking the URL.

nx10 commented 3 years ago

Thanks for opening this issue!

I have added an optional parameter to hgd_url that lets you override the hostname (1dd961d5ea9df7604de3c412de0ace972045e2da). If httpgd is started with hgd(host="0.0.0.0") it will now also print both addresses (localhost+hostname):

> hgd()
httpgd server running at:
  http://127.0.0.1:57071/live?token=ssWQcTZg

> hgd(host="0.0.0.0")
httpgd server running at:
  http://127.0.0.1:57079/live?token=JIwUQfXM
  http://DESKTOP-ABC:57079/live?token=JIwUQfXM

You can now also print your own URL with something like:

> cat(hgd_url(host="1.2.3.4"))
http://1.2.3.4:57417/live?token=722k8LIj

Edit: I now (fd72a3cd03ed4f19a9e903a5e1dcda534aea2bef) also added a replacement parameter for the port in case port forwarding is used.

eitsupi commented 3 years ago

Thank you for your immediate update ! And thank you for noticing that I have only given examples with matching ports.

That works for me.

$ docker run --rm -it -p 8000:8080 rocker/r-ver bash
> httpgd::hgd(host = "0.0.0.0", port = 8080)
httpgd server running at:
  http://127.0.0.1:8080/live?token=6DvPu1Ab
  http://1832ca70ca2b:8080/live?token=6DvPu1Ab
> httpgd::hgd_url(host = "localhost", port = 8000)
[1] "http://localhost:8000/live?token=6DvPu1Ab"

I'm thrilled to be able to easily view the plots in Docker without using X11, which is unfamiliar to me.

nx10 commented 3 years ago

Glad you like it! It's always great to hear from people using httpgd in new ways.

wds15 commented 3 years ago

Just to add to this... I also would like to see being able to use the standard "options" mechanism of R to set the host and port used to bind to (as opposed to having this fixed). I could work around it by putting something similar like this into my .Rprofile:

## by default do not save anything on R exit
formals(quit)$save <- formals(q)$save <- "no"

as R is really quirky in what it allows. Since I anyway wanted to change the displayed hostname and port I ended up writing a function doing the manipulations:

## start a http web-server
.start_webplots <- function(host="0.0.0.0", port=8787) {
    httpgd::hgd(host="127.0.0.1", port=8787)
    docker_url <- httpgd::hgd_url()
    host_url <- gsub("^(http:\\/\\/)(.*)(:)(8787)(\\/.*)",
                     paste0("\\1localhost:MAPPED-DOCKER-PORT-", port, "\\5"), docker_url)
    cat(paste0("To access rendered plots, please point your browser to:\n", host_url, "\n"))
}

## start hgd plotting device
.start_webplots()

Maybe this is useful for others. I am putting this into my rocker image which I run via Docker.

It's a really cool library from what I can see so far.

BTW, the patches discussed above are not yet on CRAN as I was not able to find the mentioned options, right?

Thanks for this library!

nx10 commented 3 years ago

Thanks for the reminder, adding options has been on my to-do list for a long time (https://github.com/nx10/httpgd/issues/12).

You are right, the changes are not yet up on CRAN. I try to keep the CRAN releases as stable as possible and want to make sure everything is tested and properly documented beforehand. The next release will be quite big (modular rendering, rewritten client), but I hope it will be ready soon. I will make sure it includes the "options" mechanism.

If you want to help: Testing and bug reporting on the GitHub version is always appreciated.

nx10 commented 3 years ago

I just added the options functionality. Every startup parameter of hgd(...) can be set via httpgd.{paramname}. I am not so happy with how verbose this makes the code look (and that it has to do 15 lookups). If anyone has a better idea how to do this feel free to comment.

https://github.com/nx10/httpgd/blob/e4e6cc67e21a5e296e8b626ea29c61f4921768c2/R/httpgd.R#L76-L90