question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Dynamic function: parse return value as rst

See original GitHub issue

Hi, I’m trying to add user defined dynamic function which returns string in rst format. What I was trying to do is to call that function inside a need and hopefully to have the function return value rendered in the need as rst.

Here is a simplified example to illustrate:

# conf.py

def test_rst_rendering(app, need, needs):
   """Here we're returning a string which in rst should be bold"""

    return "**This should be bold**"

def test_need_content(app, need, needs):
   """And here we're testing if possible to update need content in the function with rst syntax"""

    need["content"] = "**This should be bold**" + need["content"]
    return "content"

needs_functions = [
    test_need_content,
    test_rst_rendering,
]

And when used with our own need_type (maybe we have to somehow configure our custom need type in order to dynamically render rst from function return value):

class Example:
    """
    .. output:: Example
        :id: OUTPUT_100
        :tags: example

        [[test_need_content()]]
        [[test_rst_rendering()]]
    """

What we end up is that none of the functions resulted in rendering output as rst. So trying to modify need content also didn’t work: image

I was able to see in implementation where the function call is replaced with the return value and that it’s executed in the process_need_nodes.

Does anybody maybe know if something like this is possible to do? Am I maybe missing something out, some config which is not set in order this to work?

***I’m using version sphinxcontrib-needs==0.6.0

Many thanks!

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
danwoscommented, Jun 18, 2021

I think this is not possible right now. 2 Reasons:

  1. The rst-rendering is done by Sphinx before the dynamic functions get executed.
  2. dynamic functions mostly do a simple string-replacement: func-call out, return value in. And that directly on the already rendered docutils nodes.

There would be a way to treat the return value as rst and render it (basic rst only, no sphinx directive support). But then Sphinx-Needs would need to deal with a small docutils node-tree and inject this somehow in the already existing node-tree of the content area. So .e.g a simple text-node with a dynamic function must get split and the rendered rst-tree somehow integrated. Possible, but I have the feeling that this would have a lot of corner cases.

Templates are also my suggested workaround. There are executed earlier. Drawback: No direct python/script support for dynamic data. But maybe you could inject a Jinja-statement into the Jinja-Environment and then calculate your values. Maybe this stackoverflow question about jinja and custom python function is a good starter.

However, please keep me updated, if you find a solution for your case. I would be happy to document it and share it with our community.

1reaction
twodropscommented, Jun 18, 2021

@krivi95 Not a solution for the above behavior but is templates a solution for this usecase you have?

Read more comments on GitHub >

github_iconTop Results From Across the Web

dynamically parse a string and return a function in scala using ...
I am trying to dinamically interpret code given as a String. Eg: val myString = "def f(x:Int):Int=x+1".
Read more >
Creating and Using Dynamic Objects (C# and Visual Basic)
The first walkthrough defines a custom dynamic object that searches ... The TryGetMember method returns a Boolean value that returns true if ...
Read more >
Using JSON.NET for dynamic JSON parsing - Rick Strahl
Parsing JSON dynamically rather than statically serializing into ... a new dynamic JSON object and returns it based on data from the first:....
Read more >
Fetch data dynamically - Dart programming language
Parsing JSON data. Use the json.decode() function from the dart:convert library to create Dart objects from a JSON string. The example initially populates...
Read more >
Parsing Outputs of a List Rows action using Parse JSON in a ...
I'll rename the first Parse JSON as Value and add Value from the ... function and JSON Parse to read data from missing...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found