Forums

Send and Get information from Flask Application

Hi everyone,

This is my first Flask project, and I would like to make a few questions.

I have this Flask application, where I enter a phrase, make the analysis with python, and return the answer to the Flask app, I think you know how it works...

So..

I want to creat a Chrome Extension, where a select a phase or text, and send it to the application, but I have no idea how to do that. Can anyone bring me some light into this?

For example, I have another extension where I search on YouTube, but in this case it is easy because I can use the "youtube.com/results?search_query=Phrase+or+text" . But how do I solve this problem using Flask?

Besides the python code I have this in my main page: <p><input name="texto" placeholder="Write a phrase or text" /></p> <p><input type="submit" value="Check Phrase" /></p>

Not sure I am clear, but as I said, this is my fisrt time with Flask.

Thank you.

Could you give some more details about what the problem is? Is it that you need to have the parameters for your extension in the URL rather than hidden in the POST data?

Is it that you need to have the parameters for your extension in the URL rather than hidden in the POST data?

Is there an easy way to do that without changing the whole code?

Take a look at the project: http://rjlimauerj.pythonanywhere.com/

I want to send parameters to it and get the response, but using an outside tool, like a chrome extensions

If all you want to do is send parameters from an external program, that's already possible with your existing site. For example, if you were to make a POST request to the site like this in Python:

import requests
resp = requests.post("http://rjlimauerj.pythonanywhere.com/", data={"texto": "bom dia"})

...then you would get a response:

>>> resp.status_code
200
>>> resp.content
'\n                <html>\n                <title>V.G.S.O.</title>\n                    <body background="https://visme.co/blog/wp-content/uploads/2017/07/50-Beautiful-and-Minimalist-Presentation-Backgrounds-03.jpg">\n                        <center>\n                        <h1><b>Verificador Gramatical Sociolinguisticamente Orientado - Vers\xc3\xa3o 1.0</b></h1>\n                        <p>\n                        <table border="1" width="60%">\n\n            <thead>\n                <tr><td>\n                    <p><b>Resultado:</b></p>\n                        <p><b>Frase digitada:</b> bom dia</p>\n\n                        <p>N\xc3\xa3o h\xc3\xa1 forma divergente da norma padr\xc3\xa3o em seu texto.</p>\n\n                        <p></p>\n                        <p></p>\n                        <p><b></b></p>\n                        <p></p>\n                        <p><i></i></p>\n\n                        <p></p>\n                        <p></p>\n                        <p><b></b></p>\n                        <p></p>\n                        <p><i></i></p>\n\n                        <p></p>\n                        <p></p>\n                        <p><b></b></p>\n                        <p></p>\n                        <p><i></i></p>\n\n                        <p></p>\n                        <p></p>\n                        <p><b></b></p>\n                        <p></p>\n                        <p><i></i></p>\n\n                        <p></p>\n                        <p></p>\n                        <p><b></b></p>\n                        <p></p>\n                        <p><i></i></p>\n\n                        <p></p>\n                        <p></p>\n                        <p><b></b></p>\n                        <p></p>\n                        <p><i></i></p>\n\n                        <p></p>\n                        <p></p>\n                        <p><b></b></p>\n                        <p></p>\n                        <p><i></i></p>\n\n                        <p></p>\n                        <p></p>\n                        <p><b></b></p>\n                        <p></p>\n                        <p><i></i></p>\n\n                        <p></p>\n                        <p></p>\n                        <p><b></b></p>\n                        <p></p>\n                        <p><i></i></p>\n\n                        <p></p>\n                        <p></p>\n                        <p><b></b></p>\n                        <p></p>\n                        <p><i></i></p>\n\n                        <p></p>\n                        <p></p>\n                        <p><b></b></p>\n                        <p></p>\n                        <p><i></i></p>\n\n                        <p></p>\n                        <p></p>\n                        <p><b></b></p>\n                        <p></p>\n                        <p><i></i></p>\n\n                        <p></p>\n                        <p></p>\n                        <p><b></b></p>\n                        <p></p>\n                        <p><i></i></p>\n\n                        <p></p>\n                        <p></p>\n                        <p><b></b></p>\n                        <p></p>\n                        <p><i></i></p>\n\n                        <p></p>\n                        <p></p>\n                        <p><b></b></p>\n                        <p></p>\n                        <p><i></i></p>\n\n                        <p></p>\n                        <p></p>\n                        <p><b></b></p>\n                        <p></p>\n                        <p><i></i></p>\n\n                        <p></p>\n                        <p></p>\n                        <p><b></b></p>\n                        <p></p>\n                        <p><i></i></p>\n\n                        <p></p>\n                        <p></p>\n                        <p><b></b></p>\n                        <p></p>\n                        <p><i></i></p>\n\n                        <p></p>\n                        <p></p>\n                        <p><b></b></p>\n                        <p></p>\n                        <p><i></i></p>\n\n\n\n\n\n\n                        <p><a href="/">Digitar nova frase</a>\n\n                </td></tr>\n\n            </thead>\n\n            <tfoot>\n \t        <tr><td>\n \t        <center>\n            <h3>\n \t        Ent\xc3\xa3o, o que achou?\n            Conta pra gente se deu tudo certo, se teve algum problema ou qualquer observa\xc3\xa7\xc3\xa3o que voc\xc3\xaa queira fazer.\n            Pode escrever pra gente por e-mail <u>rjlimauerj@gmail.com</u>. Obrigado!\n            </h3>\n\n\n            </center>\n            </td></tr>\n            <tr><td>\n            <center>\n\n            <b>Projeto de Inicia\xc3\xa7\xc3\xa3o Cient\xc3\xadfica (UERJ) - Coord. Prof. Ricardo Joseh Lima (rjlimauerj@gmail.com) - 2019</b>\n\n\n            </td></tr>\n\n            </tfoot>\n            </center>\n\n                    </body>\n                </html>\n            '
>>>

So, as long as you can make a POST request from a Chrome extension, then you could do what you want to do right now.

That's why I asked about whether the problem was whether the parameters have to be in the URL. If you can't make POST requests from Chrome extensions, and have to use a GET request like http://rjlimauerj.pythonanywhere.com/texto=bom+dia, then you will need to make some changes to the view that handles requests; if you post the code that you have right now, we might be able to suggest what those changes would be.

Here I get the text from the form:

 @app.route('/', methods=["GET", "POST"])
 def adder_page():
    errors = ""

if request.method == "POST":
    texto = None
    try:
        texto = str(request.form["texto"])

and then I send it to make the python verification :

if texto is not None:
        result_haver = main_haver(texto)

After that I make some treatment in the response I get from python code and show it on the HTML:

if result_haver == "haverErrado":
            haver1 = """Na norma-padrão, o verbo “haver” é considerado impessoal quando tem sentido de “existir” e por isso nem ele nem qualquer verbo ligado a ele vai para o plural nesse registro, mesmo com elementos no plural diretamente relacionados a ele. Assim:"""

<thead>
<tr><td>
<p><b>Resultado:</b></p>
<p><b>Frase digitada:</b> {texto}</p>
<p>{mensagemPadrao}</p>
<p>{haver1}</p>

The thing I want to do is: Get the text from outside. I want to know how to get the parameters and put it here:

 texto = str(request.form["texto"])

I'm not sure I understand what you mean by "get the text from outside". Can you explain that? Do you mean using some chrome extension to get some text? If that is the case then the PythonAnywhere server side stuff is fine, but you need to figure out the chrome extension bit- what exactly it does, and also if chrome extensions allow you to do it.

Do you mean using some chrome extension to get some text? If that is the case then the PythonAnywhere server side stuff is fine

That's it! Can you give an example of how to do that?

You know when you select a text and use Translate Chrome extension to the translate that text? So I wanto to do the same, but instead of translating it, I want to analyze the selected text (via my application at PythonAnywhere ) and get the answer back, either in a notification balloon or a tab set for it.

Printscreen of my extensions: https://ibb.co/MDprjSY

So none of that would by PythonAnywhere stuff. It would have to do with how to write a Chrome extension, and then afterwards from your Chrome extension, send data to Pythonanywhere server. If it's just for your own use I would consider using something like greasemonkey- which would probably be easier. Otherwise you will have to read a Chrome extension tutorial

Yeah, it is not a PythonAnywhere stuff, but, once we are here, could you help send data to Pythonanywhere server? How would you do that? How would you send information and get the result back from outside Pythonanywhere ?

The solution:

@app.route('/parameters', methods=['GET'])
def get_query_string():

     text = request.args['text']
     result = main(text)

 (Here I work with my result and manage to show it.)

if __name__ == '__main__':
    app.run(debug=True)

Example.pythonanywhere.com/parameters?My%20Text%20here