from http import server
from socketserver import TCPServer
import os


class Server:
    def __init__(self, own_config, config, builder):
        self.directory = config.folders['output']
        self.port = own_config['port']
        self.builder = builder

    def serve(self):
        os.chdir(self.directory)

        request_handler = server.SimpleHTTPRequestHandler

        httpd = TCPServer(('', self.port), request_handler)
        try:
            httpd.serve_forever()
        except KeyboardInterrupt:
            httpd.shutdown()


def register(plugin_config, config, commander, builder, content_renderer):
    server = Server(plugin_config, config, builder)
    commander.add('preview', server.serve, 'Serve the rendered site')

from http import server
from socketserver import TCPServer
import os


class Server:
    def __init__(self, own_config, config, builder):
        self.directory = config.folders['output']
        self.port = own_config.get('port', 5000)
        self.builder = builder

    def serve(self):
        os.chdir(self.directory)

        request_handler = server.SimpleHTTPRequestHandler

        httpd = TCPServer(('', self.port), request_handler)
        try:
            httpd.serve_forever()
        except KeyboardInterrupt:
            httpd.shutdown()


def register(plugin_config, config, commander, builder, content_renderer):
    server = Server(plugin_config, config, builder)
    commander.add('preview', server.serve, 'Serve the rendered site')

Set up a default port of 5000 so it won't fail if you forget to specify one in config.yaml
Set up a default port of 5000 so it won't fail if you forget to specify one in config.yaml

  from http import server
  from socketserver import TCPServer
  import os
  
  
  class Server:
      def __init__(self, own_config, config, builder):
          self.directory = config.folders['output']
-         self.port = own_config['port']
+         self.port = own_config.get('port', 5000)
          self.builder = builder
  
      def serve(self):
          os.chdir(self.directory)
  
          request_handler = server.SimpleHTTPRequestHandler
  
          httpd = TCPServer(('', self.port), request_handler)
          try:
              httpd.serve_forever()
          except KeyboardInterrupt:
              httpd.shutdown()
  
  
  def register(plugin_config, config, commander, builder, content_renderer):
      server = Server(plugin_config, config, builder)
      commander.add('preview', server.serve, 'Serve the rendered site')
  
Set up a default port of 5000 so it won't fail if you forget to specify one in config.yaml
## Code Before:
from http import server
from socketserver import TCPServer
import os


class Server:
    def __init__(self, own_config, config, builder):
        self.directory = config.folders['output']
        self.port = own_config['port']
        self.builder = builder

    def serve(self):
        os.chdir(self.directory)

        request_handler = server.SimpleHTTPRequestHandler

        httpd = TCPServer(('', self.port), request_handler)
        try:
            httpd.serve_forever()
        except KeyboardInterrupt:
            httpd.shutdown()


def register(plugin_config, config, commander, builder, content_renderer):
    server = Server(plugin_config, config, builder)
    commander.add('preview', server.serve, 'Serve the rendered site')

## Instruction:
Set up a default port of 5000 so it won't fail if you forget to specify one in config.yaml
## Code After:
from http import server
from socketserver import TCPServer
import os


class Server:
    def __init__(self, own_config, config, builder):
        self.directory = config.folders['output']
        self.port = own_config.get('port', 5000)
        self.builder = builder

    def serve(self):
        os.chdir(self.directory)

        request_handler = server.SimpleHTTPRequestHandler

        httpd = TCPServer(('', self.port), request_handler)
        try:
            httpd.serve_forever()
        except KeyboardInterrupt:
            httpd.shutdown()


def register(plugin_config, config, commander, builder, content_renderer):
    server = Server(plugin_config, config, builder)
    commander.add('preview', server.serve, 'Serve the rendered site')
