35

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.

5
  • The question isn't about d3. d3 and your inline <script> is run client side and doesn't need a server. The real question is "How do I setup a local server using Python?" Commented Jan 16, 2015 at 5: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
    Commented Jan 16, 2015 at 6: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
    Commented Jan 16, 2015 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
    Commented Jan 16, 2015 at 14:51

5 Answers 5

67

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).

1
  • python -m http.server [port] to pick a custom port. Great to verify a Firewall is working properly when testing with curl -4 http://[ip]:[port] from the outside world, but run the server in an empty directory to not expose yourself, please. Commented Oct 29, 2024 at 23:14
10

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.

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.

1

Your local environment has Python3, then you can run the following the command.

  • Python3
python3 -m http.server
  • Python2 or less
python -m http.server

And then you need to visit the port 8000 in the browser like http://localhost:8000 or http://0.0.0.0:8000.

-2

stvsmth already answered, just to add if proposed solution "python -m http.server" raises the error Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings despite installed Python on your computer, you should take into account that "python" and "py" could execute different commands See Why do python and py commands run different python 3 versions? for more information. So, if in your local computer doesn't work (with above error):

python -m http.server

Try with:

py -m http.server

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.