Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
Emphasis
Source Link
johnsyweb
  • 142.9k
  • 26
  • 197
  • 253

You have the following code at the top and the bottom of index.py:

cgitb.enable() print 'Content-type: text/html\n\n' print "<link rel=\"stylesheet\" type=\"text/css\" href=\"css/msa.css\" >" # [...] ## Calling all the functions of the class template with object (objx) objx=HtmlTemplate() # [...] objx.CloseHtml() 

This will be called each time you import index.

To prevent this happening, put it in a block thus:

if __name__ == '__main__': cgitb.enable() print 'Content-type: text/html\n\n' print "<link rel=\"stylesheet\" type=\"text/css\" href=\"css/msa.css\" >" # [...] ## Calling all the functions of the class template with object (objx) objx=HtmlTemplate() # [...] objx.CloseHtml() 

...or better still in a functionbetter still put this code functions that can be called from elsewhere.

You have the following code at the top and the bottom of index.py:

cgitb.enable() print 'Content-type: text/html\n\n' print "<link rel=\"stylesheet\" type=\"text/css\" href=\"css/msa.css\" >" # [...] ## Calling all the functions of the class template with object (objx) objx=HtmlTemplate() # [...] objx.CloseHtml() 

This will be called each time you import index.

To prevent this happening, put it in a block thus:

if __name__ == '__main__': cgitb.enable() print 'Content-type: text/html\n\n' print "<link rel=\"stylesheet\" type=\"text/css\" href=\"css/msa.css\" >" # [...] ## Calling all the functions of the class template with object (objx) objx=HtmlTemplate() # [...] objx.CloseHtml() 

...or better still in a function that can be called from elsewhere.

You have the following code at the top and the bottom of index.py:

cgitb.enable() print 'Content-type: text/html\n\n' print "<link rel=\"stylesheet\" type=\"text/css\" href=\"css/msa.css\" >" # [...] ## Calling all the functions of the class template with object (objx) objx=HtmlTemplate() # [...] objx.CloseHtml() 

This will be called each time you import index.

To prevent this happening, put it in a block thus:

if __name__ == '__main__': cgitb.enable() print 'Content-type: text/html\n\n' print "<link rel=\"stylesheet\" type=\"text/css\" href=\"css/msa.css\" >" # [...] ## Calling all the functions of the class template with object (objx) objx=HtmlTemplate() # [...] objx.CloseHtml() 

...or better still put this code functions that can be called from elsewhere.

Added code from the top!
Source Link
johnsyweb
  • 142.9k
  • 26
  • 197
  • 253

PresumingYou have the following code at the top and the bottom of index.py loks a bit more like this:

classcgitb.enable() print Template'Content-type:   text/html\n\n' print "<link rel=\"stylesheet\" type=\"text/css\" defhref=\"css/msa.css\" header(self):>"  # [...] ## Calling all the functions of the printclass 'header' template with object (objx) objx=HtmlTemplate() # [...] def body objx.CloseHtml(self) 

This will be called each time you import index.

To prevent this happening, put it in a block thus:

if __name__ == '__main__': cgitb.enable()  print 'body' 'Content-type: text/html\n\n' defprint form(self):"<link rel=\"stylesheet\" type=\"text/css\" href=\"css/msa.css\" >"    print# 'form' [...]   def footer(self): ## Calling all the printfunctions 'footer' 

With each method taking self as its first parameter (which you mention in response to another question), then display.py can take the following form:

importof index tthe =class index.Templatetemplate with object (objx) t.header objx=HtmlTemplate() t # [.body() t.form().]  t objx.footerCloseHtml() 

Unlike 'from index import Template', this does not pollute the display namespace with Template, meaning that you won't clase if you import another module defining...or better still in a symbolfunction that can be called Templatefrom elsewhere.

Running python display.py should then output this:

header body form footer 

Presuming index.py loks a bit more like this:

class Template:   def header(self): print 'header'  def body(self): print 'body'  def form(self): print 'form'    def footer(self): print 'footer' 

With each method taking self as its first parameter (which you mention in response to another question), then display.py can take the following form:

import index t = index.Template() t.header() t.body() t.form() t.footer() 

Unlike 'from index import Template', this does not pollute the display namespace with Template, meaning that you won't clase if you import another module defining a symbol called Template.

Running python display.py should then output this:

header body form footer 

You have the following code at the top and the bottom of index.py:

cgitb.enable() print 'Content-type: text/html\n\n' print "<link rel=\"stylesheet\" type=\"text/css\" href=\"css/msa.css\" >"  # [...] ## Calling all the functions of the class template with object (objx) objx=HtmlTemplate() # [...]  objx.CloseHtml() 

This will be called each time you import index.

To prevent this happening, put it in a block thus:

if __name__ == '__main__': cgitb.enable()  print 'Content-type: text/html\n\n' print "<link rel=\"stylesheet\" type=\"text/css\" href=\"css/msa.css\" >"    # [...] ## Calling all the functions of the class template with object (objx)  objx=HtmlTemplate()  # [...]   objx.CloseHtml() 

...or better still in a function that can be called from elsewhere.

Source Link
johnsyweb
  • 142.9k
  • 26
  • 197
  • 253

Presuming index.py loks a bit more like this:

class Template: def header(self): print 'header' def body(self): print 'body' def form(self): print 'form' def footer(self): print 'footer' 

With each method taking self as its first parameter (which you mention in response to another question), then display.py can take the following form:

import index t = index.Template() t.header() t.body() t.form() t.footer() 

Unlike 'from index import Template', this does not pollute the display namespace with Template, meaning that you won't clase if you import another module defining a symbol called Template.

Running python display.py should then output this:

header body form footer