mirror of
https://github.com/mempool/mempool.git
synced 2025-02-23 22:46:54 +01:00
Add python example for websocket api docs
This commit is contained in:
parent
0a645431ae
commit
fc57effd5c
3 changed files with 45 additions and 0 deletions
|
@ -62,6 +62,40 @@ console.log(res["mempool-blocks"]);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
`,
|
`,
|
||||||
|
python: `import websocket
|
||||||
|
import _thread
|
||||||
|
import time
|
||||||
|
import rel
|
||||||
|
import json
|
||||||
|
|
||||||
|
rel.safe_read()
|
||||||
|
|
||||||
|
def on_message(ws, message):
|
||||||
|
print(json.loads(message))
|
||||||
|
|
||||||
|
def on_error(ws, error):
|
||||||
|
print(error)
|
||||||
|
|
||||||
|
def on_close(ws, close_status_code, close_msg):
|
||||||
|
print("### closed ###")
|
||||||
|
|
||||||
|
def on_open(ws):
|
||||||
|
message = { "action": "init" }
|
||||||
|
ws.send(json.dumps(message))
|
||||||
|
message = { "action": "want", "data": ['blocks', 'stats', 'mempool-blocks', 'live-2h-chart', 'watch-mempool'] }
|
||||||
|
ws.send(json.dumps(message))
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
ws = websocket.WebSocketApp("wss://mempool.space/api/v1/ws",
|
||||||
|
on_open=on_open,
|
||||||
|
on_message=on_message,
|
||||||
|
on_error=on_error,
|
||||||
|
on_close=on_close)
|
||||||
|
|
||||||
|
ws.run_forever(dispatcher=rel) # Set dispatcher to automatic reconnection
|
||||||
|
rel.signal(2, rel.abort) # Keyboard Interrupt
|
||||||
|
rel.dispatch()
|
||||||
|
`,
|
||||||
},
|
},
|
||||||
codeSampleMainnet: emptyCodeSample,
|
codeSampleMainnet: emptyCodeSample,
|
||||||
codeSampleTestnet: emptyCodeSample,
|
codeSampleTestnet: emptyCodeSample,
|
||||||
|
|
|
@ -30,6 +30,13 @@
|
||||||
<pre><code [innerText]="wrapEsModule(code)"></code></pre>
|
<pre><code [innerText]="wrapEsModule(code)"></code></pre>
|
||||||
</ng-template>
|
</ng-template>
|
||||||
</li>
|
</li>
|
||||||
|
<li ngbNavItem *ngIf="showCodeExample[network] && network !== 'liquid' && network !== 'liquidtestnet'" role="presentation">
|
||||||
|
<a ngbNavLink (click)="adjustContainerHeight( $event )" role="tab">Python</a>
|
||||||
|
<ng-template ngbNavContent>
|
||||||
|
<div class="subtitle"><ng-container i18n="API Docs code example">Code Example</ng-container> <app-clipboard [text]="wrapEsModule(code)"></app-clipboard></div>
|
||||||
|
<pre><code [innerText]="wrapPythonTemplate(code)"></code></pre>
|
||||||
|
</ng-template>
|
||||||
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
<div [ngbNavOutlet]="navCodeTemplate"></div>
|
<div [ngbNavOutlet]="navCodeTemplate"></div>
|
||||||
<div *ngIf="code.codeTemplate && wrapResponse(code) !== ''" class="response">
|
<div *ngIf="code.codeTemplate && wrapResponse(code) !== ''" class="response">
|
||||||
|
|
|
@ -287,6 +287,10 @@ yarn add @mempool/liquid.js`;
|
||||||
return code.codeSampleMainnet.response;
|
return code.codeSampleMainnet.response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
wrapPythonTemplate(code: any) {
|
||||||
|
return ( ( this.network === 'testnet' || this.network === 'signet' ) ? ( code.codeTemplate.python.replace( "wss://mempool.space/api/v1/ws", "wss://mempool.space/" + this.network + "/api/v1/ws" ) ) : code.codeTemplate.python );
|
||||||
|
}
|
||||||
|
|
||||||
replaceJSPlaceholder(text: string, code: any) {
|
replaceJSPlaceholder(text: string, code: any) {
|
||||||
for (let index = 0; index < code.length; index++) {
|
for (let index = 0; index < code.length; index++) {
|
||||||
const textReplace = code[index];
|
const textReplace = code[index];
|
||||||
|
|
Loading…
Add table
Reference in a new issue