so after I store an HTML input in a python variable using flask. How can I use this variable in a different path file i.e.: [I have commented out the problems next to the corresponding LOC's]
/example.html
<input type=text name=inputStr>
<button type=submit>Submit</button>
/main.py
#app.route("/example", methods=['POST'], ['GET'])
def example():
if request.method=='POST':
string=request.form["inputStr"] # suppose this is the variable i'd like to use in a diff file
return render_template("example.html")
/examplefile.py
from main import string # this does not seem to work, as you can see i'd like to import the string here
examplestring = string
print(examplestring)
Try:
/main.py
#app.route("/example", methods=['POST'], ['GET'])
def example():
if request.method=='POST':
global string=request.form["inputStr"] # suppose this is the variable i'd like to use in a diff file
return render_template("example.html")
/examplefile.py
from main import example
examplestring = example.string
If it doesn't work, this article should help you:
How to share global variables between files in Python
Related
I try to define custom filter in jinja2 using bottle (not flask). Suppose I want to define a pandoc custom filter. Regarding the jinja2 documentation, I have to define it that way:
from bottle import jinja2_view as view, jinja2_template as template
from jinja2 import environment, filters
def pandoc_convert(s):
return pypandoc.convert_text(s, format='md', to='html5')
#get("/foo")
def foo():
return template('foo.html')
environment = jinja2.Environment()
# Putting my custom filter.
environment.filters['pandoc'] = pandoc_convert
run(host='localhost', port=8080, debug=True)
In template foo.html I have
{{ "This is *some* markdown" | pandoc }}
When I run bottle code, it gets me:
jinja2.exceptions.TemplateAssertionError: No filter named 'pandoc'
but if you print environment.filters, then 'pandoc': <function pandoc_convert at 0x7f827e233040>} appears in that print.
(Updated)
This answer provided generic instructions for working with Jinja, but
it looks as if bottle has its own special way of doing things. You
need to ignore the Jinja documentation about environments and filters,
and instead delve into the bottle source; specifically, the
Jinja2Template, which looks like this:
class Jinja2Template(BaseTemplate):
def prepare(self, filters=None, tests=None, globals={}, **kwargs):
from jinja2 import Environment, FunctionLoader
self.env = Environment(loader=FunctionLoader(self.loader), **kwargs)
if filters: self.env.filters.update(filters)
...
Note that the prepare method accepts a filters keyword argument for setting filters on the environment it creates.
The jinja2_template function, which is defined like this:
jinja2_template = functools.partial(template, template_adapter=Jinja2Template)
And lastly, the template function, which includes:
if tplid not in TEMPLATES or DEBUG:
settings = kwargs.pop('template_settings', {})
if isinstance(tpl, adapter):
TEMPLATES[tplid] = tpl
if settings: TEMPLATES[tplid].prepare(**settings)
elif "\n" in tpl or "{" in tpl or "%" in tpl or '$' in tpl:
TEMPLATES[tplid] = adapter(source=tpl, lookup=lookup, **settings)
else:
TEMPLATES[tplid] = adapter(name=tpl, lookup=lookup, **settings)
So, when you call jinja2_template("foo.html") (which is what you're
doing), this becomes:
template("foo.html", template_adapter=Jinja2Template)
And within the template function, template will call the prepare
method of Jinja2Template with keyword arguments from settings,
which comes from the template_settings argument to template. So,
we can use a custom filter like this:
import bottle
def hellofilter(value):
return value.replace("hello", "goodbye")
#bottle.get("/foo")
def foo():
settings = {
"filters": {
"hello": hellofilter,
}
}
return bottle.jinja2_template("foo.html", template_settings=settings)
if __name__ == "__main__":
bottle.run(host="127.0.0.1", port=7070, debug=True)
If my template foo.html looks like this:
This is a test. {{ "hello world"|hello }}
Asking for 127.0.0.1:7070/foo will return:
This is a test. goodbye world
If we didn't want to pass the template_settings argument every time
we call jinja2_template, we could use functools.partial ourselves
to create a wrapper for the jinja2_template function:
import bottle
import functools
def hellofilter(value):
return value.replace("hello", "goodbye")
template_settings = {
"filters": {
"hello": hellofilter,
},
}
template = functools.partial(
bottle.jinja2_template, template_settings=template_settings
)
#bottle.get("/foo")
def foo():
return template("foo.html")
if __name__ == "__main__":
bottle.run(host="127.0.0.1", port=7070, debug=True)
I currently have my flask running static where I run the ETL job independently and then use the result dataset in my Flask to display chartjs line charts.
However, I'd like to integrate the ETL piece in my web framework where my users can login and submit the input parameter(locations of the input files and an added version id) using HTML form, which will then be used by my ETL job to run and use the resultant data directly to display the charts on same page.
Current Setup:
My custom ETL module has submodules that act together to form a simple pipeline process:
globals.py - has my globals such as location of the s3 and the etc. ideally i'd like my user's form inputs to be stored here so that they can be used directly in all my submodules wherever necessary.
s3_bkt = 'abc' #change bucket here
s3_loc = 's3://'+s3_bkt+'/'
ip_loc = 'alv-input/'
#Ideally ,I'd like my users form inputs to be sitting here
# ip1 = 'alv_ip.csv'
# ip2 = 'Input_product_ICL_60K_Seg15.xlsx'
#version = 'v1'
op_loc = 'alv-output/'
---main-module.py - main function
import module3 as m3
import globals as g
def main(ip1,ip2,version):
data3,ip1,ip2,version = m3.module3(ip1,ip2,version)
----perform some actions on the data and return---
return res_data
---module3.py
import module2 as m2
def mod3(ip1,ip2,version):
data2,ip1,ip2,version = m2.mod2(ip1,ip2,version)
----perform some actions on the data and return---
return data3
---module2.py
import module1 as m1
import globals as g
def mod2(ip1,ip2,version):
data1,ip1,ip2,version = m1.mod1(ip1,ip2,version)
data_cnsts = pd.read_csv(ip2) #this is where i'll be using the user's input for ip2
----perform some actions on the datasets and write them to location with version_id to return---
data1.to_csv(g.op_loc+'data2-'+ version + '.csv', index=False)
return data2
---module1.py
import globals as g
def mod1(ip1,ip2,version):
#this is the location where the form input for the data location should be actually used
data = pd.read_csv(g.s3_loc+g.ip_loc+ip1)
----perform some actions on the data and return---
return data1
Flask setup:
import main-module as mm
app = Flask(__name__)
#this is where the user first hits and submits the form
#app.route('/form')
def form():
return render_template('form.html')
#app.route('/result/',methods=['GET', 'POST'])
def upload():
msg=''
if request.method == 'GET':
return f"The URL /data is accessed directly. Try going to '/upload' to submit form"
if request.method == 'POST':
ip1 = request.form['ip_file']
ip2 = request.form['ip_sas_file']
version = request.form['version']
data = mm.main(ip1,ip2,version)
grpby_vars = ['a','b','c']
grouped_data = data.groupby(['mob'])[grpby_vars].mean().reset_index()
#labels for the chart
a = [val for val in grouped_data['a']]
#values for the chart
b = [round(val*100,3) for val in grouped_data['b']]
c = [round(val*100,3) for val in grouped_data['c']]
d = [val for val in grouped_data['d']]
return render_template('results.html', title='Predictions',a=a,b=b,c=c,d=d)
The Flask setup works perfectly fine without using any form inputs from the user(if the ETL job and Flask is de-coupled i.e. when I run the ETL and supply the result data location to Flask directly.
Problem:
The problem after integration is that I'm not very sure how to pass these inputs from users to all my sub-module. Below is the error I get when I use the current setup.
data3,ip1,ip2,version = m3.module3(ip1,ip2,version)
TypeError: module3() missing 3 required positional arguments: 'ip1', 'ip2', and 'version'
So, definitely this is due to the issue with my param passing across my sub-modules.
So my question is how do I use the data from the form as a global variable across my sub-modules. Ideally I'd like them to be stored in my globals so that I'd not have to be passing them as a param through all modules.
Is there a standard way to achieve that? Might sound very trivial but I'm struggling hard to get to my end-state.
Thanks for reading through :)
I realized my dumb mistake, I should've have just included
data,ip1,ip2,version = mm.main(ip1,ip2,version)
I could also instead use my globals file by initiating the inputs with empty string and then import my globals in the flask file and update the values. This way I can avoid the route of passing the params through my sub-modules.
I want to add set of css files to head. For instance,
python file:
modules.py
class SomeModule(tornado.web.UIModule):
def css_files(self):
return [self.handler.static_url('css/modules/some-module.css'),]
def render(self, some_data=None):
result = ''
if some_data is not None:
"""to do something with data"""
return result
server.py
app = Application(
...
ui_module=modules
...
)
template file:
...
{% module SomeModule(some_data=put_data_here) %}
As the result, I see only data that were returned from render. But css files weren't set between head tags.
What's the result of the template file depends on how you generate the template file. As far as I can see, you didn't include the code for that in your question.
If you want to use css_files() or similar, you need to use self.render_string() in order to give Tornado a chance to insert the CSS tags in the proper places.
For an example of how to use tornado.web.UIModule, see this Slideshare presentation
I'm using Jinja on my site and I like it.
I've come across a simple need. How to display today's date? Is there a way to inline some Python code in a Jinja template?
import datetime
now = datetime.datetime.utcnow()
print now.strftime("%Y-%m-%d %H:%M")
This article says no, but suggests using a macro or a filter?
Really? Must we resort to all that? OK, what would that look like in this case?
No, there is no way to inline Python into Jinja. However, you can add to the constructs that Jinja knows by extending the Environment of the template engine or the global namespace available to all templates. Alternately, you can add a filter that let's you format datetime objects.
Flask stores the Jinja2 Environment on app.jinja_env. You can inject new context into the environment by either adding to this dictionary directly, or by using the #app.context_processor decorator.
Whatever path you choose, this should be done while you are setting up the application, before you have served any requests. (See the snippets section of the website for some good examples of how to set up filters - the docs contain a good example of adding to the global variables).
The current answers are correct for pretty much every situation. However there are some very rare cases where you would want to have python code inside the template. In my case I want to use it to preprocess some latex files and I would prefer to keep the python code generating table values, plots, etc, inside the latex file it self.
So I made a Jinja2 extension that adds a new "py" block allowing python code to be written inside the template. Please keep in mind that I had to do some questionable work-arounds to get this to work, so I'm not 100% sure in which situations it fails or behaves unexpectedly.
This is an example template.
Foo was given to the template
foo: {{ foo }}
Bar was not, so it is missing
bar is missing: {{ bar == missing }}
{% py %}
# Normal python code in here
# Excess indentation will be removed.
# All template variables are accessible and can be modified.
import numpy as np
a = np.array([1, 2])
m = np.array([[3, 4], [5, 6]])
bar = m # a * foo
# It's also possible to template the python code.
{% if change_foo %}
foo = 'new foo value'
{% endif %}
print("Stdio is redirected to the output.")
{% endpy %}
Foo will have the new value if you set change_foo to True
foo: {{ foo }}
Bar will now have a value.
bar: {{ bar }}
{% py %}
# The locals from previous blocks are accessible.
m = m**2
{% endpy %}
m:
{{ m }}
The output if we set the template parameters to foo=10, change_foo=True is:
Foo was given to the template
foo: 10
Bar was not, so it is missing
bar is missing: True
Stdio is redirected to the output.
Foo will have the new value if you set change_foo to True
foo: new foo value
Bar will now have a value.
bar: [110 170]
m:
[[ 9 16]
[25 36]]
The extension with a main function to run the example.
from jinja2 import Environment, PackageLoader, nodes
from jinja2.ext import Extension
from textwrap import dedent
from io import StringIO
import sys
import re
import ctypes
def main():
env = Environment(
loader=PackageLoader('python_spike', 'templates'),
extensions=[PythonExtension]
)
template = env.get_template('emb_py2.txt')
print(template.render(foo=10, change_foo=True))
var_name_regex = re.compile(r"l_(\d+)_(.+)")
class PythonExtension(Extension):
# a set of names that trigger the extension.
tags = {'py'}
def __init__(self, environment: Environment):
super().__init__(environment)
def parse(self, parser):
lineno = next(parser.stream).lineno
body = parser.parse_statements(['name:endpy'], drop_needle=True)
return nodes.CallBlock(self.call_method('_exec_python',
[nodes.ContextReference(), nodes.Const(lineno), nodes.Const(parser.filename)]),
[], [], body).set_lineno(lineno)
def _exec_python(self, ctx, lineno, filename, caller):
# Remove access indentation
code = dedent(caller())
# Compile the code.
compiled_code = compile("\n"*(lineno-1) + code, filename, "exec")
# Create string io to capture stdio and replace it.
sout = StringIO()
stdout = sys.stdout
sys.stdout = sout
try:
# Execute the code with the context parents as global and context vars and locals.
exec(compiled_code, ctx.parent, ctx.vars)
except Exception:
raise
finally:
# Restore stdout whether the code crashed or not.
sys.stdout = stdout
# Get a set of all names in the code.
code_names = set(compiled_code.co_names)
# The the frame in the jinja generated python code.
caller_frame = sys._getframe(2)
# Loop through all the locals.
for local_var_name in caller_frame.f_locals:
# Look for variables matching the template variable regex.
match = re.match(var_name_regex, local_var_name)
if match:
# Get the variable name.
var_name = match.group(2)
# If the variable's name appears in the code and is in the locals.
if (var_name in code_names) and (var_name in ctx.vars):
# Copy the value to the frame's locals.
caller_frame.f_locals[local_var_name] = ctx.vars[var_name]
# Do some ctypes vodo to make sure the frame locals are actually updated.
ctx.exported_vars.add(var_name)
ctypes.pythonapi.PyFrame_LocalsToFast(
ctypes.py_object(caller_frame),
ctypes.c_int(1))
# Return the captured text.
return sout.getvalue()
if __name__ == "__main__":
main()
You can add to global variables which can be accessed from Jinja templates. You can put your own function definitions in there, which do whatever you need.
I build simple webpage using CherryPy and Jinja2
The webserver python file:
import cherrypy
from jinja2 import Environment, FileSystemLoader
from soltyslib import listFiles
env = Environment(loader=FileSystemLoader('templates'))
class HelloWorld(object):
#cherrypy.expose
def index(self):
template = env.get_template('index.html')
result = template.render(name='Pawel',files=listFiles('templates'))
return result
cherrypy.quickstart(HelloWorld())
Template file:
Hello {{name}}!
<ul>
{% for file in files %}
<li>{{file}}</li>
{% endfor %}
</ul>
Ok, and then I run webserver, I go to 127.0.0.1:8080 and see result that is expected:
Hello Pawel!
templates\index.html
templates\list.html
But then I hit refresh in browser and this is a result:
Hello Pawel!
templates\index.html
templates\list.html
templates\index.html
templates\list.html
Why? for loop is evaluated again? How to prevent from doing so?
And in case somebody wondering how listFiles function looks:
import os,sys
from collections import deque
def listFiles(cdir, fileslist=[]):
basedir = cdir
queuedir = deque()
queuedir.append(basedir)
while len(queuedir) > 0:
currentbase = queuedir.popleft()
for f in os.listdir(currentbase):
f = os.path.join(currentbase,f)
if os.path.isdir(f):
queuedir.append(f)
else:
fileslist.append(f)
return fileslist
Your problem is
def listFiles(cdir, fileslist=[]):
You're reusing the same list on every call, because default arguments are evaluated only when the function is defined, not every time it's called. See "Least Astonishment" and the Mutable Default Argument for a long discussion if this behavior.
Do
def listFiles(cdir, fileslist=None):
if fileslist is None:
fileslist = []
Your problem is in fileslist=[] in the function declaration. Default values are only ever evaluated once, which means that the list is created the first call, but never rebuilt or cleared.
It's the default kwarg for fileslist in listFiles. That list is created once at module load time and keeps accruing items as you append.