19

I am trying to do some basic D3 programming. All the books I am reading talk about setting up a local http server and that is where I am finding myself stuck. I typed the following

python -m http.server 

to host the local server. Now, my problem is how to open my html file in this local server? I don't even know how to find the file in the command prompt. Any help will be appreciated. The following is my html file code on aptana. I also have put the d3.js file in the aptana.

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>
            D3 Page Template
        </title>
        <script type="text/javascript" src="d3.js"></script>
    </head>
    <script type="text/javascript">
        //D3 codes will go here
    </script>
</html>

When I am running aptana, the html file is opening in a regular firefox page. I want it to open in the locally hosted http server page. Any hints.

stvsmth
  • 3,578
  • 2
  • 28
  • 30
sharky
  • 209
  • 1
  • 2
  • 5
  • The question isn't about d3. d3 and your inline – Taysky Jan 16 '15 at 05:52
  • But all the books that I am reading are talking about a locally hosted http server. Are you saying that this sever is not needed to d3 programming? – sharky Jan 16 '15 at 06:11
  • I agree w/ Taysky, this question needs to be re-titled and re-tagged. Realize that most programming tasks will be composed of many smaller tasks, some quite un-related to your core task. Learning to ask questions appropriate to the task is an essential skill. – stvsmth Jan 16 '15 at 14:49
  • 2
    Surprisingly, Googling Taysky's suggested title or starting a SO question with it doesn't present anything that useful. Not to mention that differences in Python2 and Python3 could be confusing to a beginner. – stvsmth Jan 16 '15 at 14:51
  • http://unix.stackexchange.com/questions/32182/simple-command-line-http-server – Ciro Santilli OurBigBook.com Aug 04 '16 at 10:59

3 Answers3

50

The answer is provided when you start the server. In the same directory where you have your HTML file, start the server:

$ python -m http.server
Serving HTTP on 0.0.0.0 port 8000 ...

(Or, the Python2 incantation)

$ python -m SimpleHTTPServer
Serving HTTP on 0.0.0.0 port 8000 ...

In this message, Python tells you the IP address (0.0.0.0) and the port number (8000).

So, if the file is named d3_template.html, you can get to this page via http://0.0.0.0:8000/d3_template.html

On most machines you should also be able to use

http://localhost:8000/d3_template.html or http://127.0.0.1:8000/d3_template.html

If you get an error like this:

socket.error: [Errno 48] Address already in use

You want to use a different port:

$ python -m http.server 8888

And to load the file:

http://0.0.0.0:8888/d3_template.html

To understand why all of these work, you'd want to learn a fair bit about networking (ports, DNS, loopback interface, how multiple network cards behave on the same machine and, if things aren't working as expected, firewalls, restricted ports and who knows what else).

stvsmth
  • 3,578
  • 2
  • 28
  • 30
3

Try this:

from http.server import HTTPServer, BaseHTTPRequestHandler

class Serv(BaseHTTPRequestHandler):

    def do_GET(self):
       if self.path == '/':
           self.path = '/test.html'
       try:
           file_to_open = open(self.path[1:]).read()
           self.send_response(200)
       except:
           file_to_open = "File not found"
           self.send_response(404)
       self.end_headers()
       self.wfile.write(bytes(file_to_open, 'utf-8'))

httpd = HTTPServer(('localhost',8080),Serv)
httpd.serve_forever()

Where test.html is the HTML file you wrote.

John Mee
  • 50,179
  • 34
  • 152
  • 186
1

I've created a small portable python 3 script (should work on MacOS/Linux) to locally render html file that use d3 or more generally websites. I thought this could be useful for others.

Essentially it creates a local server using a subprocess, opens your browser for rendering and then shuts down the server properly for fast reuse. You can find the Python 3 script here (with some detail on how to use it): https://github.com/alexandreday/local_server. An example use is:

$ python custom_server.py index.html

This will render your index.html file which uses d3.js or a website more generally.

VanillaSpinIce
  • 263
  • 2
  • 10