vsajip / prettytable

Automatically exported from code.google.com/p/prettytable
Other
0 stars 0 forks source link

The html feature will not work with flask or I am crazy #45

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. install the module
2. try to use it with flask
3. get error

What is the expected output? What do you see instead?

+------+------------+---------+ | Name | Phone | Yardage | 
+------+------------+---------+ | John | 7193382296 | 27 | | John | 7193382296 
| 27 | +------+------------+---------+
What version of the product are you using? On what operating system?

Latest mac OSX
Please provide any additional information below.

Original issue reported on code.google.com by pilotkid2011 on 14 Dec 2013 at 11:24

GoogleCodeExporter commented 9 years ago
There is nowhere near enough information in this report for me to tell you 
anything useful.  *How* are you trying to use it with Flask?  *What* error are 
you getting?  Unless you show me some code I can't help you at all.

Original comment by luke@maurits.id.au on 14 Dec 2013 at 11:42

GoogleCodeExporter commented 9 years ago
The module is just returning a string without any form of formatting 

Original comment by pilotkid2011 on 14 Dec 2013 at 11:44

GoogleCodeExporter commented 9 years ago
How are you using it?  Show me code.

Original comment by luke@maurits.id.au on 14 Dec 2013 at 11:53

GoogleCodeExporter commented 9 years ago
def hello_admins(username):
    from prettytable import *

    x = PrettyTable(["Name","Phone","Yardage"])
    x.add_row(['John',"7193382296","27"])
    x.add_row(['Phil',"7195926682","16"])
        x.get_string(attributes = {"class": "foo"})
    return render_template("allshooters.html",tbl=x )

Original comment by pilotkid2011 on 14 Dec 2013 at 11:56

GoogleCodeExporter commented 9 years ago
Try replacing "get_string" with "get_html_string" and see if that works.

Original comment by luke@maurits.id.au on 14 Dec 2013 at 11:58

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
I get the same string result... :/

My html code is 

{% block contents %}
<p>a</p>
{% for message in get_flashed_messages() %}
        <div class = flash>

          {{message}}

        </div>
    {% endfor %}
{{tbl}}

{% endblock %}

Original comment by pilotkid2011 on 15 Dec 2013 at 12:02

GoogleCodeExporter commented 9 years ago
Oh!  That's because you're just passing the PrettyTable object x to the Flask 
rendering system.  You need to actually pass the string containing the HTML 
code that PrettyTable generates.  Get rid of the line:

x.get_string(attributes = {"class": "foo"})

and change the final line to:

return render_template("allshooters.html",tbl=x.get_html_string(attributes = 
{"class": "foo"}))

(or, alternatively, store the output of get_html_string in an intermediary 
variable and pass that to render_template).

Original comment by luke@maurits.id.au on 15 Dec 2013 at 12:03

GoogleCodeExporter commented 9 years ago
Yay it somewhat worked now I am getting the result as actual html" <table 
class="foo"> <tr> <th>Name</th> <th>Phone</th> <th>Yardage</th> </tr> <tr> 
<td>John</td> <td>7193382296</td> <td>27</td> </tr> <tr> <td>Phil</td> 
<td>7195926682</td> <td>16</td> </tr> </table>"  but i can figure it out.. 
unless you have any quick reconmendations

Original comment by pilotkid2011 on 15 Dec 2013 at 12:06

GoogleCodeExporter commented 9 years ago
This is probably something to do with Flask, automatically escaping HTML input 
to templates in order to avoid CSS attacks and similar things.  I vaguely 
recall having to deal with something like that in the past, but I don't know 
how to fix it off the top of my head.  You should be able to find something in 
the Flask docs easily enough.

Original comment by luke@maurits.id.au on 15 Dec 2013 at 12:08

GoogleCodeExporter commented 9 years ago
Ok also may I give you a small piece of constructive criticism with your 
tutorial?

Original comment by pilotkid2011 on 15 Dec 2013 at 12:15

GoogleCodeExporter commented 9 years ago
Yes, of course!  Please do.

Original comment by luke@maurits.id.au on 15 Dec 2013 at 1:32

GoogleCodeExporter commented 9 years ago
There were times when I was reading the tutorial where I was confused on what 
you meant I also thought there could be more code but other than that I love 
this module I've used it on 3 other projects (I am making my first site now) 
but it is a great module it just needs a more coded and better worded tutorial 
just to help others 

Original comment by pilotkid2011 on 15 Dec 2013 at 4:17

GoogleCodeExporter commented 9 years ago
Also I am sorry about this but I cannot figure out the getting the table to 
work without getting rid of the style sheet

Original comment by pilotkid2011 on 15 Dec 2013 at 4:27

GoogleCodeExporter commented 9 years ago
Try replacing "{{tbl}}" in your template with "{{tbl|safe}}" and see if that 
fixes it.

Original comment by luke@maurits.id.au on 15 Dec 2013 at 4:47

GoogleCodeExporter commented 9 years ago
I LOVE YOU!!!!!!

Original comment by pilotkid2011 on 15 Dec 2013 at 4:49

GoogleCodeExporter commented 9 years ago
Glad it worked.  You should make sure you understand why Flask automatically 
escapes HTML by default, and understand when it is and isn't safe to use the 
"safe" filter.  Maybe read https://en.wikipedia.org/wiki/Cross-site_scripting.

Thanks for your feedback on the tutorial.  Next time I update it I'll try 
adding some more example code.

Original comment by luke@maurits.id.au on 15 Dec 2013 at 5:38

GoogleCodeExporter commented 9 years ago
Thank you I am am kinda learning the whole flask this but my site is pretty 
cool 

Original comment by pilotkid2011 on 15 Dec 2013 at 5:39

GoogleCodeExporter commented 9 years ago

Original comment by luke@maurits.id.au on 15 Dec 2013 at 5:56