-3

I was wondering if i can use the static_file() from withing a template as follows:

{{ static_file( filename, root=filepath, download=True ) }}

Can it be written like that?

And if can, then how i will be able to substitite the variables within the statement, in my case, filename and filepath with their values taken from the wsgi python file?

And in general, are we able to use Bottle's framework statement and functions from within a template system or we can use then from the wsgi python app?!

  • 1
    There are different answers for Bottle and Flask because they use different template engines. Have you tried looking in [Bottle templating docs](https://bottlepy.org/docs/0.12/stpl.html) and [Flask's Jinja templating engine docs](http://jinja.pocoo.org/docs/2.10/templates/)? – Fine Sep 26 '18 at 08:49

2 Answers2

2

No, static_file is for returning a static file; templates are for returning dynamic content. They do not interoperate.

You use static_file from within your code (route handler), not from within a template.

E.g. (from the documentation),

@route('/static/<filepath:path>')
def server_static(filepath):
    return static_file(filepath, root='/path/to/your/static/files')
ron rothman
  • 14,574
  • 6
  • 34
  • 35
  • Thats how i was intended to use it, it works great this way, yes, but problem is that i wanto print some html data first stored in the variable `pdata` and `return static_file(filepath, root='/path/to/your/static/files')` just error out. So, to avoid this issue i had to use a template as the middle man. If i can do it without the use for a template please do tell me how. – Νικόλαος Βέργος Sep 26 '18 at 16:54
  • What you're imagining is not possible. Hence my answer to your question, "Can it be written like that." – ron rothman Sep 26 '18 at 18:43
  • You mentioned something regarding wrapping pdata within static_file the other day. Can you clarify that? I really don't want to use a template for this. – Νικόλαος Βέργος Sep 26 '18 at 19:03
  • ? It's not possible. I'm afraid you are misunderstanding HTTP. – ron rothman Sep 26 '18 at 19:21
  • So, template usage it is. i return a template and then withing the template after printing the html data that i want in the last line i have placed this: `` That downloads the file okey, but when ti comes to .pdf files it opnes them in browser instead of downloadign them. If you come up with some better solution please inform me! – Νικόλαος Βέργος Sep 26 '18 at 19:28
  • I can help you with the download issue, but please open a new question since it is distinct from this one and too big to put in a comment. – ron rothman Sep 27 '18 at 00:06
  • Thank you, i have just opened a new question, here https://stackoverflow.com/questions/52531054/return-some-html-before-serving-a-file – Νικόλαος Βέργος Sep 27 '18 at 06:49
0

Static file is a routing option, it doesn't actually serve files. You can just return an open files binary data however using a function and use javascript to prompt the download.

eatmeimadanish
  • 2,930
  • 1
  • 9
  • 14
  • `static_file( filename, root=filepath, download=True` is used to serve a file, os this function *is* used for that reason. – Νικόλαος Βέργος Oct 09 '18 at 17:15
  • 1
    static_file is a helper function to serve files. It simply routes the request to the static file. You can't use it in a template because it requires the function return call as the HTML body request. Not simply a binary representation of the data. Hence my point that is actually a routing request to the file and not an actual representation of the file. – eatmeimadanish Oct 09 '18 at 17:20
  • Yes, we can agree on that, that its a routing request pointing to the file. – Νικόλαος Βέργος Oct 09 '18 at 17:33
  • It's also why it won't work in your template. It must be the HTML return call. Use a binary form of the file and then have javascript injected to start the download process. – eatmeimadanish Oct 09 '18 at 17:37