I was extending a ckan template as per:
http://docs.ckan.org/en/latest/theming/templates.html
So when I add {% ckan_extends %} in an empty index.html file in my extension to use the original index.html I get the following exception and traceback(My debug is set to true):
WebError Traceback:
⇝ ValueError: invalid literal for int() with base 10: ''
View as: Interactive (full) | Text (full) | XML (full)
Module ckan.controllers.home:88 in index view
>> return base.render('home/index.html', cache_force=True)
Module ckan.lib.base:174 in render view
>> return cached_template(template_name, render_template)
Module pylons.templating:249 in cached_template view
>> return render_func()
Module ckan.lib.base:128 in render_template view
>> return render_jinja2(template_name, globs)
Module ckan.lib.base:85 in render_jinja2 view
>> return template.render(**extra_vars)
Module jinja2.environment:989 in render view
>> return self.environment.handle_exception(exc_info, True)
Module jinja2.environment:748 in handle_exception view
>> traceback = _make_traceback(exc_info, source_hint)
Module jinja2.debug:140 in make_traceback view
>> return translate_exception(exc_info, initial_skip)
Module jinja2.debug:182 in translate_exception view
>> lineno = template.get_corresponding_lineno(tb.tb_lineno)
Module jinja2.environment:1056 in get_corresponding_lineno view
>> for template_line, code_line in reversed(self.debug_info):
Module jinja2.environment:1072 in debug_info view
>> self._debug_info.split('&')]
ValueError: invalid literal for int() with base 10: ''
Help needed.
Related
I would like to import variables defined in a json file(my_info.json) as attibutes for bazel rules.
I tried this (https://docs.bazel.build/versions/5.3.1/skylark/tutorial-sharing-variables.html) and works but do not want to use a .bzl file and import variables directly to attributes to BUILD.bazel.
I want to use those variables imported from my_info.json as attributes for other BUILD.bazel files.
projects/python_web/BUILD.bazel
load("//projects/tools/parser:config.bzl", "MY_REPO","MY_IMAGE")
container_push(
name = "publish",
format = "Docker",
registry = "registry.hub.docker.com",
repository = MY_REPO,
image = MY_IMAGE,
tag = "1",
)
Asking the similar in Bazel slack I was informed the is not possible to import variables directly to Bazel and it is needed to parse the json variables and write them into a .bzl file.
I tried also this code but nothing is written in config.bzl file.
my_info.json
{
"MYREPO" : "registry.hub.docker.com",
"MYIMAGE" : "michael/monorepo-python-web"
}
WORKSPACE.bazel
load("//projects/tools/parser:jsonparser.bzl", "load_my_json")
load_my_json(
name = "myjson"
)
projects/tools/parser/jsonparser.bzl
def _load_my_json_impl(repository_ctx):
json_data = json.decode(repository_ctx.read(repository_ctx.path(Label(":my_info.json"))))
config_lines = ["%s = %s" % (key, repr(val)) for key, val in json_data.items()]
repository_ctx.file("config.bzl", "\n".join(config_lines))
load_my_json = repository_rule(
implementation = _load_my_json_impl,
attrs = {},
)
projects/tools/parser/BUILD.bazel
load("#aspect_bazel_lib//lib:yq.bzl", "yq")
load(":config.bzl", "MYREPO", "MY_IMAGE")
yq(
name = "convert",
srcs = ["my_info2.json"],
args = ["-P"],
outs = ["bar.yaml"],
)
Executing:
% bazel build projects/tools/parser:convert
ERROR: Traceback (most recent call last):
File "/Users/michael.taquia/Documents/Personal/Projects/bazel/bazel-projects/multi-language-bazel-monorepo/projects/tools/parser/BUILD.bazel", line 2, column 22, in <toplevel>
load(":config.bzl", "MYREPO", "MY_IMAGE")
Error: file ':config.bzl' does not contain symbol 'MYREPO'
When making troubleshooting I see the execution calls the jsonparser.bzl but never enters to _load_my_json_impl function (based in print statements) and does not write anything to config.bzl.
Notes: Tested on macOS 12.6 (21G115 ) Darwin Kernel Version 21.6.0
There is a better way to do that? A code snippet will be very useful.
I'm using Airflow 2.2.2 with the latest providers installed as appropriate.
I'm trying to use the Azure and MySQL hooks and have created custom operators with templates defined for what variables can be templated.
When I do so, I get an error saying that conn or var cannot be found
e.g. my passed parameter is
{{ conn.<variable_name> }}
or
{{ var.json.value.<variable_name> }}
I believe this should be possible in > v2.0 but not working for me, any ideas why?
EDIT: Below are snippets of code with some sensitive information removed, let me know if anything else is needed?
DAG error -
Broken DAG: [/home/dags/dag.py] Traceback (most recent call last):
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/dags/dag.py", line 52, in <module>
wasb_conn_id = {{ conn.wasb }},
NameError: name 'conn' is not defined
task in dag.py
t1 = WasbLogBlobsToCSVOperator(
task_id='task_xyz',
wasb_conn_id = {{ conn.wasb }},
Custom Operator using an extended version of the Microsoft Azure wasb hook , used by dag.py -
class WasbLogBlobsToCSVOperator(BaseOperator):
template_fields = (
'wasb_conn_id',
)
def __init__(
self,
*,
wasb_conn_id: str = 'wasb',
**kwargs,
) -> None:
super().__init__(**kwargs)
self.wasb_conn_id = wasb_conn_id
self.hook = ExtendedWasbHook(wasb_conn_id=self.wasb_conn_id)
There looks to be a few things going on here that should help.
Jinja templates are string expressions. Try wrapping your wasb_conn_id arg in quotes.
wasb_conn_id = "{{ conn.wasb }}",
Templated fields are not rendered until the task runs meaning the Jinja expression won't be evaluated until an operator's execute() method is called. This is why you are seeing an exception from your comment below. The literal string "{{ conn.wasb }}" is being evaluated as the conn_id. If you want to use a template field in the custom operator, you need to move that logic to be in the scope of the execute() method.
Why do you need to use a Jinja expression here? Since the format of accessing the Connection object via Jinja is {{ conn.<my_conn_id> }}, you could just use the value "wasb" directly.
My doc-tests in Julia require a qualification with the module name, despite calling using my_module everywhere. If I do not qualify the functions, I get
ERROR: UndefVarError: add not defined
Here is the setup that gives this error. The directory structure with tree is:
.
|____docs
| |____make.jl
| |____src
| | |____index.md
|____src
| |____my_module.jl
The file docs/make.jl is:
using Documenter, my_module
makedocs(
modules = [my_module],
format = :html,
sitename = "my_module.jl",
doctest = true
)
The file docs/src/index.md is:
# Documentation
```#meta
CurrentModule = my_module
DocTestSetup = quote
using my_module
end
```
```#autodocs
Modules = [my_module]
```
The file src/my_module.jl is:
module my_module
"""
add(x, y)
Dummy function
# Examples
```jldoctest
julia> add(1, 2)
3
```
"""
function add(x::Number, y::Number)
return x + y
end
end
If I qualify the doc-test in the src/my_module.jl with my_module.add(1,2), then it works fine.
How can I avoid qualifying function names in doc-tests?
Use a setup name block
This is untested, but something like this should work:
module my_module
"""
add(x, y)
Dummy function
# Examples
```#setup abc
import my_module: add
```
```jldoctest abc
julia> add(1, 2)
3
```
"""
function add(x::Number, y::Number)
return x + y
end
end
Following the comments in this thread, the problem is that the add function is not exported, so it is not brought into scope with using. You can add this line near the top of src/my_module.jl, after the module declaration:
export add
And then the doc-testing works.
I'm working on a Rmd document that I would like to compile to html using knitr package via the HTML export mechanism available in RStudio. The problem can be reproduced with the code below:
Example
# Set up
rm(list = ls())
data(airquality)
attach(airquality)
packs <- c("randomForest", "knitr", "xtable", "xtable", "stargazer")
lapply(packs, require, character.only=T, quietly = TRUE, warn.conflicts = FALSE)
# Model
airquality <- na.roughfix(airquality)
dummy <- randomForest(Ozone ~., data = airquality)
# Problem
kable(dummy)
xtable(dummy)
stargazer(dummy)
The issue is further illustrated by the output below:
Output
> # Problem
> kable(dummy)
Error in as.data.frame.default(x) :
cannot coerce class "c("randomForest.formula", "randomForest")" to a data.frame
> xtable(dummy)
Error in UseMethod("xtable") :
no applicable method for 'xtable' applied to an object of class "c('randomForest.formula', 'randomForest')"
> stargazer(dummy)
% Error: Unrecognized object type.
Is it possible to force the randomForest output into a nice html table that would be presentable in a markdown document?
Is there a method for jinja2 to raise an exception when we pass a variable that is not present in the template?
PS: This is different(or opposite) from raising an exception when a variable is present in the template but it is not passed. For this I use "undefined=StrictUndefined"
When you load your jinja2.Environment, set the 'undefined' parameter to 'jinja2.StrictUndefined', e.g.:
env = jinja2.Environment(loader=<someloader>, undefined=jinja2.StrictUndefined)
You can catch and examine the render exception to see what was missing
EDIT It would help if I read your full question. :)
Maybe this could help you
https://jinja.palletsprojects.com/en/2.11.x/api/#the-meta-api
>>> from jinja2 import Environment, meta
>>> env = Environment()
>>> ast = env.parse('{% set foo = 42 %}{{ bar + foo }}')
>>> meta.find_undeclared_variables(ast)
set(['bar'])
You can also do that:
from jinja2 import Template, StrictUndefined
Template('name: {{ name }} , city: {{ city }}',undefined=StrictUndefined).render(**{"name":"foo","city":"bar"})