Add python example for websocket api docs
This commit is contained in:
		
							parent
							
								
									0a645431ae
								
							
						
					
					
						commit
						fc57effd5c
					
				| @ -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, | ||||
|   codeSampleTestnet: emptyCodeSample, | ||||
|  | ||||
| @ -30,6 +30,13 @@ | ||||
|         <pre><code [innerText]="wrapEsModule(code)"></code></pre> | ||||
|       </ng-template> | ||||
|     </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> | ||||
|   <div [ngbNavOutlet]="navCodeTemplate"></div> | ||||
|   <div *ngIf="code.codeTemplate && wrapResponse(code) !== ''" class="response"> | ||||
|  | ||||
| @ -287,6 +287,10 @@ yarn add @mempool/liquid.js`; | ||||
|     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) { | ||||
|     for (let index = 0; index < code.length; index++) { | ||||
|       const textReplace = code[index]; | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user