A simple http server for showing files in browser

Usually my workflow involves generating batches at plots and then looking at them with my browser (I hate jupyter). A useful tool for this is the python module SimpleHTTPServer (which is only available in python 2). By default, this module creates an HTTP server on port 8000 which shows you links to the files from which the server was launched. From there you can view html, plain text and image files.

Anyway, I’m extremely ignorant about these sorts of things, so I was wondering if someone could get me started on what the simplest way to implement something like this in Julia would be. Is there an existing package that would be helpful for setting something like this up?

1 Like

I am also very limited in knowledge on these things but have been working on something along those lines (maybe a little different) and have been playing around with PagesJL but found a limitation that is holding me back right now. If you just want a to serve a page/pages simply from julia you could do something like this:

using HttpServer
import HttpServer.mimetypes

folders = Dict("/CodeBook" => 1) # "/MyCoolApp"

http = HttpHandler() do req::Request, res::Response

        if haskey(folders, req.resource) # the path is exactly = to a folder so we send the default page.
            Response(readstring(dirname(@__FILE__)*"/index.html"))

        else # the path is somewhere in the/a folder.
            key = match(r"(?:\.(\w+$))", req.resource)[1]  ## uri = URI(req.resource).path
            mime = mimetypes[ key ]
            Response(200, Dict{AbstractString,AbstractString}([("Content-Type",mime)]), readstring(dirname(@__FILE__)*req.resource))
         end
end

server = Server(http)
println("Server listening on 8000...")
run(server,8000)

EDIT: Obviously you need to make the page you want to serve and any accompanying scripts or css files. Note that I put the whole mime-types thing in there because it was causing me trouble on my computer. I would have thought that it would have somehow been correctly surmised without any help but like I said, I am not an expert on this.

re-edit: Sorry for the edit/reediting of the code. I now have my morning coffee in my hand and hope to wake up anytime now :sleeping:

1 Like

Just in case you are still interested in using PagesJL and enjoying the benefits that come with it, I’ve issued a pull request for a proposed fix/patch to allow PagesJL to work with endpoints as “folders”. I don’t know if this is a good solution but it does work fine for me. If the pull request is accepted then you could use Pages as indicated and also access the contents of a folder in the following way:

Endpoint("/MyCoolSite") do request::Request
    Pages.pages["/MyCoolSite"].folder = dirname(@__FILE__)
end

Now any file requested by /MyCoolSite/index.html will be fetched and sent along. For example:

<script src="/MyCoolSite/libs/js/pages.js"></script>

*The only caveat is that you still have to include the folder name as part of the path to any requested files. The problem with this is not that it is difficult but that it is not the standard way of specifying paths and could end up frustrating someone who is unaware of it.

I am open to suggestion on how to fix it. Currently my thought is to alter the path just before the browser sends off the request …not sure though.

Thanks, I’ll start messing around with PagesJL when I have the time and post back here after doing some experimenting.

BTW, the HTTP server is still available in python3, it’s just called http.server now (i.e. python3 -m http.server)

2 Likes