0

Simply return some (markdown) string in flask:

@app.route("/raw/", methods=['GET'])
def sometext():
    return "This is an **example**"

## Main procedure
if __name__ == "__main__":
    app.run(debug=True, port=8000) 

If you call pandoc directly (pandoc http://localhost:8000/raw) or with subprocess, you get no problem:

import subprocess, os

url = "http://localhost:8000/raw"
pbody = subprocess.run(["pandoc", url], check=True, stdout=subprocess.PIPE)
print(pbody.stdout.decode())

But if you call pandoc within flask method:

@app.route("/get", methods=['GET'])
def index():
    url = "{}".format(url_for('sometext', _external=True))
    pbody = subprocess.run(["pandoc", url], check=True, stdout=subprocess.PIPE, universal_newlines=True)
    print("***Error: ", pbody.stderr)
    return pbody.stdout

Then when you access to http://localhost:8000/get you get Responsetimeout error from pandoc:

pandoc: HttpExceptionRequest Request {
  host                 = "localhost"
  port                 = 8000
  secure               = False
  requestHeaders       = []
  path                 = "/raw/"
  queryString          = ""
  method               = "GET"
  proxy                = Nothing
  rawBody              = False
  redirectCount        = 10
  responseTimeout      = ResponseTimeoutDefault
  requestVersion       = HTTP/1.1
}
 ResponseTimeout

References: url_for in flask API

somenxavier
  • 1,206
  • 3
  • 20
  • 43

1 Answers1

2

As I recall the Flask http server is single threaded so it can't handle the '/raw' request while it's still in the process of handling the '/get' request.

Answer to another SO question suggests using app.run(threaded=True) that may be enough for personal use. For production use you should consider a real web server like nginx or apache.

Even then, assuming pandoc supports it (I've no idea) you may want to consider sending the markdown input to pandoc stdin and avoid the extra HTTP request altogether, something like (untested)

markdown = StringIO("This is an **example**")
pbody = subprocess.run(["pandoc"], check=True, stdin=markdown, stdout=subprocess.PIPE, universal_newlines=True)
Community
  • 1
  • 1
Tommi Komulainen
  • 2,830
  • 1
  • 19
  • 16
  • Or create a real WSGI app. Like [here](http://flask.pocoo.org/docs/0.12/deploying/wsgi-standalone/). – erip Jan 13 '17 at 19:01
  • The `stringIO` does not work (python3). Using [io](https://docs.python.org/3/library/io.html#io.StringIO) library I get `io.UnsupportedOperation: fileno` with `markdown = io.StringIO("This is an **example**")`. Anyone knows how to use `stdin` in subprocess in python3? – somenxavier Jan 14 '17 at 11:03
  • It must work with stringIO because in bash you can call `echo 'some text' | pandoc -` works – somenxavier Jan 14 '17 at 13:33