简体   繁体   中英

Is there any way to open python's data in a browser page without needing to create a server?

Part 1 (solved from the help indicated in the comments of this question):

I would like to open this JSON response print() in a page of my default browser (Chrome), as I have a JSON Viewer extension and I would like to study this data with an easier view than in the Visual Studio Code terminal.

Is there any simple method for this to be done without the need to create an html file with this data, create a specific server and only then be able to open it in the browser?

I tried to use the webbrowser but it just opened any page, it didn't open the data.

import requests
import json
import webbrowser
 
url="https://api.betfair.com/exchange/betting/json-rpc/v1"
header = { 'X-Application' : 'AAAAAAAAAAAAA', 'X-Authentication' : 'BBBBBBBBBBBB' ,'content-type' : 'application/json' }
 
jsonrpc_req='{"jsonrpc": "2.0", "method": "SportsAPING/v1.0/listCompetitions", "params": {"filter":{"eventTypeIds": [1]}}, "id": 2}'
 
response = requests.post(url, data=jsonrpc_req, headers=header)
 
data = json.dumps(json.loads(response.text), indent=3)

webbrowser.open(data)

Part 2:

I'm trying to open a .json file by the browser so that I can analyze the data more easily, according to the tips in the comments, I started using the webbrowser , but when I try to open .json the page doesn't open and when I try to open it as .txt or .html the extension that improves the visualization of JSON in the browser, doesn't recognize the data and doesn't format for it.

How could I manage to do this?

My extension in Chrome:
https://chrome.google.com/webstore/detail/json-viewer/gbmdgpbipfallnflgajpaliibnhdgobh?hl=pt-BR

Github project extension:
https://github.com/tulios/json-viewer

I post my Python code here just to make it easier to see print() :

import requests
import json
 
url="https://api.betfair.com/exchange/betting/json-rpc/v1"
header = { 'X-Application' : 'AAAAAAAAAAAAA', 'X-Authentication' : 'BBBBBBBBBBBB' ,'content-type' : 'application/json' }
 
jsonrpc_req='{"jsonrpc": "2.0", "method": "SportsAPING/v1.0/listCompetitions", "params": {"filter":{"eventTypeIds": [1]}}, "id": 2}'
 
response = requests.post(url, data=jsonrpc_req, headers=header)

with open("ApiBetfair.html", "w+", newline="", encoding="UTF-8") as f:
    data = json.dumps(json.loads(response.text), indent=3)
    f.write(data)

new = 2
webbrowser.open('file://' + os.path.realpath('ApiBetfair.html'),new=new)

You could store the JSON in a local file and open it with the webbrowser module. As we figured out in the comments above, it is necessary to enable local file support in chrome://extensions for the extension to work properly.

import webbrowser

fp = "ApiBetfair.html"

with open(fp, "w+", newline="", encoding="UTF-8") as f:
    data = json.dumps(json.loads(response.text), indent=3)
    f.write(data)

new = 2 # open in new tab
webbrowser.open('file://' + os.path.realpath(fp),new=new)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM