Output Multiline Strings with Python Flask or Other Structure

I am trying to take Redhat kickstart files and modify them in python before using them in server setup. My application uses python to curl the kickstart source file from my Redhat Satellite server, then I do the replacement of the string with specific values ​​in the kickstart file. When I twist the file in python, it returns as a multi-line string, which is what I need for redhat kickstart to correctly interpret the file. But when I return a string variable through one of these frameworks (web2py, bottle, flask), something happens and it does not return it as a multi-line string, I need to save the exact format of the source file, except for the areas I change. I don’t want to put kickstart files into the templates, because I manage them through the satellite, if I twist the file from the satellite, then it picks up any changes without thinking about entering the template for all the time. Then in the template or something, I either return the line without the template, or to the template file, I transfer only 1 variable to the template as the entire kickstart file.

@route('/kickstart/<name>') def kickstart(name): ks = vula.kickstarter.kickstart.Kickstarter() ks_file = ks.getKickstartFile() return pystache.render('{{kickstart}}', {'kickstart': ks_file}) 

Here is my vula method. It returns the file exactly as I need it. But again, something happens between this and returns this value through the framework.

 def getKickstartFile(self): response = urllib2.urlopen('https://my-satellite-server/core-kickstarter') ks_file = response.read() return ks_file 

I started using Bottle as a framework, but I found an expression that says they are not able to return multi-line strings, so scratch this. I moved to Flux, but now Flux is doing the same. I'm still learning python and maybe I am doing something wrong, but I need help to get this to work correctly. I would like to output a multi-line string. I understand that you are using either

 """ or ''' 

for multi-line lines, but even after you do this and send it through the framework, it will print on the screen as one continuous line. What am I doing wrong? As a last resort, I will be forced to put kickstart files into templates if I cannot output multi-line strings.

+4
source share
2 answers

Both bottles and flasks can handle multi-line strings just fine. Your problem is that by default your data is interpreted as text/html , and in HTML any combination of spaces is collapsed into one space when displayed. In order for your data to be returned exactly as you sent it, you want to set the Content-Type header to text/plain .

In the flask:

 # If you want *all* your responses to be text/plain # then this is what you want @app.after_request def treat_as_plain_text(response): response.headers["content-type"] = "text/plain" return response # If you want only *this* route to respond # with Content-Type=text/plain @app.route("/plain-text") def a_plain_text_route(): response = make_response(getKickstartFile()) response.headers["content-type"] = "text/plain" return response 

In a bottle:

 @route("/plain-text") def plain_text(): response.content_type = "text/plain" return """This multi-line string will show up just fine""" 
+10
source

Sean's answer is the right way, but if you just want to test something, you can use the xmp tag:

 @app.route("/"): def hello(): return """<xmp> Hello, Multiline! </xmp>""" 

decorator version

You can also create your own decorator for this:

 from functools import wraps def multiline(fn): @wraps(fn) def _fn(*args, **kwargs): return "<xmp>" + fn(*args, **kwargs) + "</xmp>" return _fn 

Then you can do:

  @app.route("/"): @multiline def hello(): return """Hello, Multiline!""" 
+1
source

Source: https://habr.com/ru/post/1488104/


All Articles