GithubHelp home page GithubHelp logo

Nested JSON about flask-shell2http HOT 2 CLOSED

madhavajay avatar madhavajay commented on August 16, 2024
Nested JSON

from flask-shell2http.

Comments (2)

eshaan7 avatar eshaan7 commented on August 16, 2024 1

🚨 Important!

Because of #31, I would not recommend using callback functions to modify the future's result anymore. So you are left with 2 other options:

  1. Don't change the behavior on flask-shell2http side, handle the JSON parsing on the client side. Shouldn't be too painful IMO.
  2. Use a decorator to modify the JSON response and return the modified response. See below example:
from flask import make_response, jsonify
import functools

def json_parser_decorator(f):
    @functools.wraps(f)
    def decorator(*args, **kwargs):
        response = f(*args, **kwargs)
        resp_json = response.json.copy()
        resp_json["report"] = json.loads(resp_json["report"])
        return make_response(jsonify(resp_json), response.status_code)
    return decorator

shell2http.register_command(
    endpoint="mytool", command_name="mytool", decorators=[json_parser_decorator]
)

from flask-shell2http.

eshaan7 avatar eshaan7 commented on August 16, 2024

It's possible to "intercept" the output providing a callback function. See this example.

You should probably be able to write a callback function that does something like:

res: dict = future.result()  # 1. get current result
res["report"] = json.loads(res["report"]) # 2. manipulate report however u want
future._result = res # 3. set new result

from flask-shell2http.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.