Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Inserting-embedding some html data at the end of a .py file

Reply
Thread Tools

Re: Inserting-embedding some html data at the end of a .py file

 
 
33
Guest
Posts: n/a
 
      03-05-2013
But i did, I just tried this:

# open html template
if htmlpage.endswith('.html'):
f = open( "/home/nikos/public_html/" + htmlpage )

htmldata = f.read()
counter = ''' <center><a href="(E-Mail Removed)"> <img src="/data/images/mail.png"> </a>
<center><table border=2 cellpadding=2 bgcolor=black>
<td><font color=lime> </td>
<td><a href="http://superhost.gr/?show=stats"><font color=cyan> %d </td>
''' % data[0]
else:
f = open( "/home/nikos/public_html/cgi-bin/" + htmlpage )

htmldata = f.read()
counter = '''
print '''<center><a href="(E-Mail Removed)"> <img src="/data/images/mail.png"> </a>
<center><table border=2 cellpadding=2 bgcolor=black>
<td><font color=lime> </td>
<td><a href="http://superhost.gr/?show=stats"><font color=cyan> %d </td>
'''
''' % data[0]

template = htmldata + counter
print ( template )
=============

But still doens't embed correctly the additional html data at the end of the .py files.....

Do you have an idea?
 
Reply With Quote
 
 
 
 
33
Guest
Posts: n/a
 
      03-05-2013
What extra triple quote?
There are 2 sets of triple quotes the counter's and the print's !!

in case htmlpage variable is a .py file i must append a print '''html''' in order for .py code to absorve that code properly no?
 
Reply With Quote
 
 
 
 
33
Guest
Posts: n/a
 
      03-05-2013
What extra triple quote?
There are 2 sets of triple quotes the counter's and the print's !!

in case htmlpage variable is a .py file i must append a print '''html''' in order for .py code to absorve that code properly no?
 
Reply With Quote
 
Mark Lawrence
Guest
Posts: n/a
 
      03-05-2013
On 05/03/2013 17:39, 33 wrote:
> But i did, I just tried this:
>
> # open html template
> if htmlpage.endswith('.html'):
> f = open( "/home/nikos/public_html/" + htmlpage )
>
> htmldata = f.read()
> counter = ''' <center><a href="(E-Mail Removed)"> <img src="/data/images/mail.png"> </a>
> <center><table border=2 cellpadding=2 bgcolor=black>
> <td><font color=lime> </td>
> <td><a href="http://superhost.gr/?show=stats"><font color=cyan> %d </td>
> ''' % data[0]
> else:
> f = open( "/home/nikos/public_html/cgi-bin/" + htmlpage )
>
> htmldata = f.read()
> counter = '''
> print '''<center><a href="(E-Mail Removed)"> <img src="/data/images/mail.png"> </a>
> <center><table border=2 cellpadding=2 bgcolor=black>
> <td><font color=lime> </td>
> <td><a href="http://superhost.gr/?show=stats"><font color=cyan> %d </td>
> '''
> ''' % data[0]
>
> template = htmldata + counter
> print ( template )
> =============
>
> But still doens't embed correctly the additional html data at the end of the .py files.....
>
> Do you have an idea?
>


You're doing something wrong.

--
Cheers.

Mark Lawrence

 
Reply With Quote
 
Dave Angel
Guest
Posts: n/a
 
      03-05-2013
On 03/05/2013 12:49 PM, Νίκος Γκρ33κ wrote:
> What extra triple quote?
> There are 2 sets of triple quotes the counter's and the print's !!


There are 3 pairs of triple-quotes. But one pair is nested inside the
other, so the interpreter will not handle it the way you apparently
want. If you have to use quotes inside quotes, you must either use a
different kind, or escape them. Perhaps you wanted something like:

counter = """
print '''<center> etc....
<td>font ...
'''
""" % data[0]

But this fragment is unreadable to me, so the syntax error is probably
the least of your worries.

>
> in case htmlpage variable is a .py file i must append a print '''html''' in order for .py code to absorve that code properly no?
>


No clue what that sentence fragment means.

'absorve' isn't an English word, and neither 'absorb' nor 'observe' make
any sense.

Perhaps if you posted a complete program, and identified it by name, and
specified the complete environment it runs in, and used English to
describe what should be appended to what, we'd be able to help. As it
is, you've got mixed clues from which that it's both Python 2 and 3,
running Linux or equivalent, and probably runs on a server.

Using symbol names that make sense would also help. 'htmlpage' is
apparently a str representing a filename on disk. 'counter' is not an
integer, but a long string of html. 'data' is apparently a tuple or
list, and the zeroth element is an int. ??

--
--
DaveA
 
Reply With Quote
 
33
Guest
Posts: n/a
 
      03-05-2013
Let's focus on just the following snipper please:

f = open( some_python_file )

htmldata = f.read()
counter = ''' print( "
<center><a href="(E-Mail Removed)"> <img src="/data/images/mail.png"> </a>
<center><table border=2 cellpadding=2 bgcolor=black>
<td><font color=lime> </td>
<td><a href="http://superhost.gr/?show=stats"><font color=cyan> %d </td>
" )
''' % data[0]

#render template
template = htmldata + counter
print ( template )
=============================

What this snippet tries to accomplish is append the following string

=========================================
counter = ''' <center><a href="(E-Mail Removed)"> <img src="/data/images/mail.png"> </a>
<center><table border=2 cellpadding=2 bgcolor=black>
<td><font color=lime> </td>
<td><a href="http://superhost.gr/?show=stats"><font color=cyan> %d </td>
''' % data[0
=========================================

at the end of the the python script file that it currently opened.
I'am using the print statemnt inside the triple quoted string so to append that html data by inserting a print statemnt but although i have changes the type of quoting it still fails.

If i try to append that html data to an .html file they are inserted beautifully but inside a .py file they dont.

I just need to append that string after the end of a .py file. that's all iwant to do.
 
Reply With Quote
 
33
Guest
Posts: n/a
 
      03-05-2013
Let's focus on just the following snipper please:

f = open( some_python_file )

htmldata = f.read()
counter = ''' print( "
<center><a href="(E-Mail Removed)"> <img src="/data/images/mail.png"> </a>
<center><table border=2 cellpadding=2 bgcolor=black>
<td><font color=lime> </td>
<td><a href="http://superhost.gr/?show=stats"><font color=cyan> %d </td>
" )
''' % data[0]

#render template
template = htmldata + counter
print ( template )
=============================

What this snippet tries to accomplish is append the following string

=========================================
counter = ''' <center><a href="(E-Mail Removed)"> <img src="/data/images/mail.png"> </a>
<center><table border=2 cellpadding=2 bgcolor=black>
<td><font color=lime> </td>
<td><a href="http://superhost.gr/?show=stats"><font color=cyan> %d </td>
''' % data[0
=========================================

at the end of the the python script file that it currently opened.
I'am using the print statemnt inside the triple quoted string so to append that html data by inserting a print statemnt but although i have changes the type of quoting it still fails.

If i try to append that html data to an .html file they are inserted beautifully but inside a .py file they dont.

I just need to append that string after the end of a .py file. that's all iwant to do.
 
Reply With Quote
 
Joel Goldstick
Guest
Posts: n/a
 
      03-05-2013
On Tue, Mar 5, 2013 at 1:53 PM, Νίκος Γκρ33κ <(E-Mail Removed)> wrote:

> Let's focus on just the following snipper please:
>
> f = open( some_python_file )
>
> htmldata = f.read()
> counter = ''' print( "
> <center><a href="mailto:support@superhost..gr">
> <img src="/data/images/mail.png"> </a>
> <center><table border=2 cellpadding=2
> bgcolor=black>
> <td><font color=lime>Αριθμός
> Επισκεπτών</td>
> <td><a href="
> http://superhost.gr/?show=stats"><font color=cyan> %d </td>
> " )
> ''' % data[0]
>
> #render template
> template = htmldata + counter
> print ( template )
> =============================
>
> What this snippet tries to accomplish is append the following string
>
> =========================================
> counter = ''' <center><a href="(E-Mail Removed)">
> <img src="/data/images/mail.png"> </a>
> <center><table border=2 cellpadding=2
> bgcolor=black>
> <td><font color=lime>Αριθμός
> Επισκεπτών</td>
> <td><a href="
> http://superhost.gr/?show=stats"><font color=cyan> %d </td>
> ''' % data[0
> =========================================
>
> at the end of the the python script file that it currently opened.
> I'am using the print statemnt inside the triple quoted string so to append
> that html data by inserting a print statemnt but although i have changes
> the type of quoting it still fails.
>
> If i try to append that html data to an .html file they are inserted
> beautifully but inside a .py file they dont.
>
> I just need to append that string after the end of a .py file. that's all
> i want to do.
> --
> http://mail.python.org/mailman/listinfo/python-list
>


print displays to the console. To write to a file open file in 'append'
mode and write:

with open("test.py", "a") as myfile:
myfile.write("appended text")



--
Joel Goldstick
http://joelgoldstick.com

 
Reply With Quote
 
Dave Angel
Guest
Posts: n/a
 
      03-05-2013
On 03/05/2013 01:53 PM, 33 wrote:
> Let's focus on just the following snipper please:


Once again, I repeat. Make a fragment that contains enough information
to actually run. Explain in what environment it's running, and what you
hoped would happen. For example, why on earth would you assume that
printing to the console below would write to a file instead? Is some
part of this running as a cgi inside a web server? What is your
environment?
>
> f = open( some_python_file )
>
> htmldata = f.read()
> counter = ''' print( "


Syntax error here. Since the inner string is more than one line, you
will need to use """. See my example last message. This only matters
if you succeed in writing this to a python file.

> <center><a href="(E-Mail Removed)"> <img src="/data/images/mail.png"> </a>
> <center><table border=2 cellpadding=2 bgcolor=black>
> <td><font color=lime> </td>
> <td><a href="http://superhost.gr/?show=stats"><font color=cyan> %d </td>
> " )
> ''' % data[0]
>
> #render template
> template = htmldata + counter
> print ( template )


Why should print write to some python file you have open?? Besides you
have the file open for readonly access. So you might use:

f.close()
f = open( some_python_file, "w" )
f.write(template)
f.close()


> =============================
>
> What this snippet tries to accomplish is append the following string
>
> =========================================
> counter = ''' <center><a href="(E-Mail Removed)"> <img src="/data/images/mail.png"> </a>
> <center><table border=2 cellpadding=2 bgcolor=black>
> <td><font color=lime> </td>
> <td><a href="http://superhost.gr/?show=stats"><font color=cyan> %d </td>
> ''' % data[0
> =========================================
>
> at the end of the the python script file that it currently opened.


But that's not at all what's inside the counter variable.

> I'am using the print statemnt inside the triple quoted string so to append that html data by inserting a print statemnt but although i have changes the type of quoting it still fails.


Don't ever say "it fails". If you can't be bothered to explain in what
way it fails, forget it. Perhaps you're trying to say it prints a
string to the console instead of writing a different string to the
/fullpath/to/some_python_file.py

Or perhaps you're saying it crashed the Windows machine, and killed
power for miles around. Or perhaps you're saying it got an exception
but we won't bother telling you which.


>
> If i try to append that html data to an .html file they are inserted beautifully


Untrue, unless your environment is specified, it certainly did not.
perhaps you were running the program with output redirected to a file,
in which case it lost the earlier version. Or perhaps you were running
it on a web server, and you *think* some file changed because some
obscure thing happened on your browser. What's your environment?

> but inside a .py file they dont.
>
> I just need to append that string after the end of a .py file. that's all i want to do.
>


Easy to do in emacs.


--
DaveA
 
Reply With Quote
 
Roland Koebler
Guest
Posts: n/a
 
      03-06-2013
Hi,

On Tue, Mar 05, 2013 at 09:39:19AM -0800, Νίκος Γκρ33κ wrote:
> But i did, I just tried this:
>
> # open html template
> if htmlpage.endswith('.html'):
> f = open( "/home/nikos/public_html/" + htmlpage )
>
> htmldata = f.read()
> counter = ''' <center><a href="(E-Mail Removed)"> <img src="/data/images/mail.png"> </a>
> <center><table border=2 cellpadding=2 bgcolor=black>
> <td><font color=lime>Αριθμός Επισκεπτών</td>
> <td><a href="http://superhost.gr/?show=stats"><font color=cyan> %d </td>
> ''' % data[0]
> else:
> f = open( "/home/nikos/public_html/cgi-bin/" + htmlpage )
>
> htmldata = f.read()
> counter = '''
> print '''<center><a href="(E-Mail Removed)"> <img src="/data/images/mail.png"> </a>
> <center><table border=2 cellpadding=2 bgcolor=black>
> <td><font color=lime>Αριθμός Επισκεπτών</td>
> <td><a href="http://superhost.gr/?show=stats"><font color=cyan> %d </td>
> '''
> ''' % data[0]
>
> template = htmldata + counter
> print ( template )
> =============
>
> But still doens't embed correctly the additional html data at the end of the .py files.....
>
> Do you have an idea?

as someone said: You're doing it the wrong way.

I would recommend to use a template-engine; then you can put the
complete html-design (and some design-control-structures) into
the template (and *not* into the cgi) and fill data into the
template with a python-script.


Roland
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Inserting-embedding some html data at the end of a .py file 33 Python 34 03-07-2013 12:17 PM
Re: Inserting-embedding some html data at the end of a .py file 33 Python 4 03-05-2013 10:49 PM
Re: Inserting-embedding some html data at the end of a .py file 33 Python 2 03-05-2013 05:25 PM
Is a blank at the end of a C file a better style than a newline atthe end of the file or is it better to place none? Jimmy C Programming 13 09-21-2011 12:17 AM
Is a blank at the end of a C file a better style than a newline atthe end of the file or is it better to place none? Jimmy C Programming 3 09-09-2011 10:36 PM



Advertisments