javipalanca / spade

Smart Python Agent Development Environment
MIT License
253 stars 98 forks source link

How to run spade in Flask web application ? Because flask web application is runs in port 8080. How to run spade dashboard with flask web application #59

Closed GayanSamuditha closed 5 years ago

GayanSamuditha commented 5 years ago

Description

I'm trying to integrate the spade code in flask application. While I add some code - DummyAgent with a web interface, in the flask application app.py, the flask application running on port 8080 and the spade is not working.

But I tried with DummyAgent without a web interface. It's working correctly

from flask import Flask, render_template from flask import redirect from urllib.parse import urljoin from flask_cdn import CDN from spade import agent

app = Flask(name)

@app.route('/') def hello_world(): return render_template("main.html")

class DummyAgent(agent.Agent): def setup(self): print("Hello World! I'm agent {}".format(str(self.jid)))

dummy = DummyAgent("agent1@GAYAN-SAMUDITHA", "agent1") dummy.start()

dummy.stop()

if name == 'main': app.run()

WITH WEB INTERFCE

from flask import Flask, render_template from flask import redirect from urllib.parse import urljoin from flask_cdn import CDN from spade import agent

app = Flask(name)

@app.route('/') def hello_world(): return render_template("main.html")

from spade import agent

class WebAgent(agent.Agent): def setup(self): print("Hello World! I'm agent {}".format(str(self.jid)))

dummy = WebAgent("admin@GAYAN-SAMUDITHA", "admin")

async def hello_controller(request): return {"number": 42}

a = WebAgent("agent1@GAYAN-SAMUDITHA", "agent1")

a.web.add_get("/hello", hello_controller, "hello.html")

a.start(auto_register=True) a.web.start(port=10000)

if name == 'main': app.run()

==> Not working

hdonancio commented 5 years ago

@GayanSamuditha , I don't know if it is exactly what do you want, but I have made a application a few months ago using SPADE 2 with Werkzeug on this link

GayanSamuditha commented 5 years ago

Simply, how we run the SPADE server inside the Flask Server. OR how we connect we create agents and start agents in Flask web server

javipalanca commented 5 years ago

Note that in SPADE each agent has its own web server (uses aiohttp) so it is not easy to integrate into another application such as Flask. What you can do is change the port where the agent launches its web service (or even not launch it if you don't need it). When you start the web of an agent you can choose the hostname and port: agent.web.start(hostname="127.0.0.1", port="10000")