Re: [pylons-discuss] Display query and result on the same page

2023-11-30 Thread Oberdan Santos
Hi Mike. I tried many things, including beyond your tip, but I can't make 
any progress. I think it's something simple. Attached is my last attempt. 
If you can help I would appreciate it.

Em quinta-feira, 23 de novembro de 2023 às 18:26:37 UTC-3, Mike Orr 
escreveu:

> On Thu, Nov 23, 2023 at 11:50 AM Oberdan Santos  
> wrote:
> >
> > You should note in the subject statement that in addition to the query, 
> I have the problem of the result being published on another page.
> > query page code
> > # templates/pac_recepx.jinja2
> >
> > 
> > 
> > Consultar cadastro do 
> paciente
> > 
> > http://localhost:6543/queryx"; method="GET">
> > Digite o CPF (11 números)
> >  required maxlength="11" value=''>
> >  type="submit">Consultar
> > 
> > 
> >
> > The result is going to...
> > action="http://localhost:6543/queryx
> >
> > How do I take this result to the same page as the query 
> (templates/pac_recepx.jinja2), that is, place it below the query?
>
> There are two approaches.
>
> SERVER-SIDE ONLY:
>
> Remove the form `action` attribute. The form will post back to the
> same view that contained the form. In the view, add an `if` stanza to
> distinguish whether there's form input or not:
>
> ```
> cpf = request.params.get("cpf", "") # User input, or "" if no input.
> error = "" # Validation error
> message, or None if no error.
> rows = None # Result rows, or None
> if no valid input, or [] if valid input but zero results.
> if cpf: # If value is not "" or None.
> if CPF_IS_VALID:
> rows = request.dbsession...
> else:
> error = "Input is invalid."
> return {"cpf": cpf, "error": error, "rows": rows}
> ```
>
> Then your template might always show the form, but only show the
> results section if there was input, and only show the results table if
> there was at least one result, and only show the error message if
> there was a user error. I use Mako templating so I'll write it that
> way.
>
> ```
> ## page.mako
> 
> % if error:
> ${error}
> %endif
> 
> 
>
> % if results is not None: # If there was valid user input.
> Results
> % if results: # If there was at least one result.
> 
> Header...
> % for r in results:
> ...
> % endfor
> % else: # Else there were zero results.
> No results.
> % endif
> ```
>
> CLIENT-SIDE ALTERNATIVE:
>
> Write Javascript to intercept the Submit click. Send an AJAX request
> to the server to get the results in a JSON array. Use Javascript to
> populate the results table. That's beyond the scope of this mailing
> list. In this case you'd have a view that processes the AJAX request
> and converts the rows list to JSON before returning it.
>

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/pylons-discuss/05058659-a202-4b6b-b845-91514acc0169n%40googlegroups.com.
//templates/pac_recepx.jinja2   # arquivo principal de consulta

{% extends "basefull.jinja2" %}

{% block container %}




UF: Maranhão
REG_SAUDE: São 
Luis
MUN: São Luis
UBS: São 
Francisco


date = new Date();
year = date.getFullYear();
month = date.getMonth() + 1;
day = date.getDate();
document.getElementById("current_date").innerHTML = day + "/" + 
month + "/" + year;









Consultar cadastro do 
paciente

http://localhost:6543/queryx"; method="GET" id="consultaForm">
Digite o CPF (11 números)

Consultar







{% for Paciente in pacientes %}

{{ Paciente.id }} {{ Paciente.name }} 
{{ Paciente.idade }} {{ Paciente.data_nascimento }} {{ 
Paciente.sexo }}
{{ Paciente.raca }} {{ Paciente.fone }} 
{{ Paciente.endereco }} {{ Paciente.cpf }} {{ 
Paciente.cns }}
edit
delete

{% endfor %}





function consultarCPF() {
var cpf = document.getElementById("cpf").value;
if (cpf !== "") {
// Realizar a requisição para buscar os pacientes com o CPF
$.ajax({
type: "GET",
url: "http://localhost:6543/queryx";, // Sua URL de consulta
data: { cpf: cpf },
success: function (data) {
// Atualizar a tabela de pacientes com o resultado da 
consulta
$('#pacientesTable tbody').empty(); // Limpar tabela
if (data.pacientes.length > 0) {
  

Re: [pylons-discuss] Display query and result on the same page

2023-11-23 Thread Mike Orr
On Thu, Nov 23, 2023 at 11:50 AM Oberdan Santos  wrote:
>
> You should note in the subject statement that in addition to the query, I 
> have the problem of the result being published on another page.
> query page code
> # templates/pac_recepx.jinja2
>
> 
> 
> Consultar cadastro do 
> paciente
> 
>  action="http://localhost:6543/queryx"; method="GET">
> Digite o CPF (11 números)
>  name="cpf" required maxlength="11" value=''>
>  type="submit">Consultar
> 
> 
>
> The result is going to...
> action="http://localhost:6543/queryx
>
> How do I take this result to the same page as the query 
> (templates/pac_recepx.jinja2), that is, place it below the query?

There are two approaches.

SERVER-SIDE ONLY:

Remove the form `action` attribute. The form will post back to the
same view that contained the form. In the view, add an `if` stanza to
distinguish whether there's form input or not:

```
cpf = request.params.get("cpf", "")   # User input, or "" if no input.
error = ""  # Validation error
message, or None if no error.
rows = None   # Result rows, or None
if no valid input, or [] if valid input but zero results.
if cpf:   # If value is not "" or None.
if CPF_IS_VALID:
rows = request.dbsession...
else:
error = "Input is invalid."
return {"cpf": cpf, "error": error, "rows": rows}
```

Then your template might always show the form, but only show the
results section if there was input, and only show the results table if
there was at least one result, and only show the error message if
there was a user error. I use Mako templating so I'll write it that
way.

```
## page.mako

% if error:
${error}
%endif



% if results is not None:# If there was valid user input.
Results
% if results:# If there was at least one result.

Header...
% for r in results:
...
% endfor
% else:   # Else there were zero results.
No results.
% endif
```

CLIENT-SIDE ALTERNATIVE:

Write Javascript to intercept the Submit click. Send an AJAX request
to the server to get the results in a JSON array. Use Javascript to
populate the results table. That's beyond the scope of this mailing
list. In this case you'd have a view that processes the AJAX request
and converts the rows list to JSON before returning it.

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/pylons-discuss/CAH9f%3DuotPq%3D-%2Boojik5HLXWY%3Dg7QX3k7SAx5nZ%3D2v1g8n7GdiA%40mail.gmail.com.


Re: [pylons-discuss] Display query and result on the same page

2023-11-23 Thread Laurent Daverio
Well, to be honest, it's not a question about Pyramid, it's a question
about an absolutely basic pattern in web programming. I was doing that with
Perl CGI scripts 25 years ago, when Python was still in infancy, and
Pyramid didn't exist.

The problem is actually very simple : you use *one* view, not two, i.e. the
form must call its own URL (e.g. action="#")

The view tests if a non-null parameter "cpf" had been provided. If that's
the case, you compute the results (SQL query), and you send the two values
to the template (cpf, and the rows).

The template tests if it receives a "cpf" parameter. If so, it auto-fills
the form, and displays the rows (using a combination of {%if ...}, {%for
...} etc. as appropriate).

Again, it's not a Pyramid question, or a Pyramid solution, I would have
given you exactly the same answer with FastAPI, PHP, Rails, Flask, etc.

Le jeu. 23 nov. 2023 à 20:50, Oberdan Santos  a
écrit :

> You should note in the subject statement that in addition to the query, I
> have the problem of the result being published on another page.
> query page code
> # templates/pac_recepx.jinja2
>
> 
> 
> Consultar cadastro do
> paciente
> 
> http://localhost:6543/queryx"; method="GET">
> Digite o CPF (11 números)
>  name="cpf" required maxlength="11" value=''>
>  type="submit">Consultar
> 
> 
>
> The result is going to...
> action="http://localhost:6543/queryx
>
> How do I take this result to the same page as the query
> (templates/pac_recepx.jinja2), that is, place it below the query?
>
> Thank you in advance for your support.
>
> Oberdan Costa
>
> Em quinta-feira, 23 de novembro de 2023 às 16:19:16 UTC-3, Laurent Daverio
> escreveu:
>
>> Hi Mike,
>>
>> .filter() and .filter_by() are still both valid in SQLAlchemy 2.x. I
>> think .filter() is a synonym of .where().
>>
>> Note : what is deprecated, but still available, is the "query syntax". I
>> even think it's no longer documented. Recommended syntaxes are here:
>>
>>
>> https://docs.sqlalchemy.org/en/20/changelog/migration_20.html#migration-orm-usage
>>
>> It's a bit more verbose, but closer to SQL :)
>>
>> Laurent.
>>
>> Le jeu. 23 nov. 2023 à 20:08, Mike Orr  a écrit :
>>
>>> On Thu, Nov 23, 2023 at 7:03 AM Oberdan Santos 
>>> wrote:
>>> > O codigo no formato acima não executou, apresentou erro, mas foi de
>>> grande valia,  muito obrigado). Fiz uma pequena alteração e deu certo.
>>> > Funcionou assim:
>>> > cpf = request.params["cpf"]
>>> >rows =
>>> request.dbsession.query(Paciente).filter(Paciente.cpf==cpf).all()
>>>
>>> I remembered that after I wrote the comment, that it's
>>> `filtey(Paciente.cpf==cpf)` and `filter_by(cpf=cpf)`. And `filter_by'
>>> may not be supported in SQLAlchemy 2.0? I'm still on 1.4/1.3.
>>>
>>> I'm afraid I don't know enough Portuguese to understand the rest of the
>>> message.
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "pylons-discuss" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to [email protected].
>>>
>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/pylons-discuss/CAH9f%3DupAGsreB_H48SxC0_5fRctbdKSxs7kn1dcOrfZRaycJbw%40mail.gmail.com
>>> .
>>>
>> --
> You received this message because you are subscribed to the Google Groups
> "pylons-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/pylons-discuss/b800223f-dcb3-4a1a-9cd3-ef8c74255b6dn%40googlegroups.com
> 
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/pylons-discuss/CAB7cU6zhNwdSpUgDJKDxiwa1Ri1b5HPg1XG-UHjo1M%3DTZtrogg%40mail.gmail.com.


Re: [pylons-discuss] Display query and result on the same page

2023-11-23 Thread Oberdan Santos
You should note in the subject statement that in addition to the query, I 
have the problem of the result being published on another page.
query page code
# templates/pac_recepx.jinja2

 
  
Consultar cadastro do 
paciente

http://localhost:6543/queryx"; method="GET">
Digite o CPF (11 números)

Consultar



The result is going to...
action="http://localhost:6543/queryx

How do I take this result to the same page as the query 
(templates/pac_recepx.jinja2), that is, place it below the query?

Thank you in advance for your support.

Oberdan Costa

Em quinta-feira, 23 de novembro de 2023 às 16:19:16 UTC-3, Laurent Daverio 
escreveu:

> Hi Mike,
>
> .filter() and .filter_by() are still both valid in SQLAlchemy 2.x. I think 
> .filter() is a synonym of .where().
>
> Note : what is deprecated, but still available, is the "query syntax". I 
> even think it's no longer documented. Recommended syntaxes are here:
>
>
> https://docs.sqlalchemy.org/en/20/changelog/migration_20.html#migration-orm-usage
>
> It's a bit more verbose, but closer to SQL :)
>
> Laurent.
>
> Le jeu. 23 nov. 2023 à 20:08, Mike Orr  a écrit :
>
>> On Thu, Nov 23, 2023 at 7:03 AM Oberdan Santos  
>> wrote:
>> > O codigo no formato acima não executou, apresentou erro, mas foi de 
>> grande valia,  muito obrigado). Fiz uma pequena alteração e deu certo.
>> > Funcionou assim:
>> > cpf = request.params["cpf"]
>> >rows = 
>> request.dbsession.query(Paciente).filter(Paciente.cpf==cpf).all()
>>
>> I remembered that after I wrote the comment, that it's
>> `filtey(Paciente.cpf==cpf)` and `filter_by(cpf=cpf)`. And `filter_by'
>> may not be supported in SQLAlchemy 2.0? I'm still on 1.4/1.3.
>>
>> I'm afraid I don't know enough Portuguese to understand the rest of the 
>> message.
>>
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "pylons-discuss" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to [email protected].
>>
> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/pylons-discuss/CAH9f%3DupAGsreB_H48SxC0_5fRctbdKSxs7kn1dcOrfZRaycJbw%40mail.gmail.com
>> .
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/pylons-discuss/b800223f-dcb3-4a1a-9cd3-ef8c74255b6dn%40googlegroups.com.


Re: [pylons-discuss] Display query and result on the same page

2023-11-23 Thread Laurent Daverio
Hi Mike,

.filter() and .filter_by() are still both valid in SQLAlchemy 2.x. I think
.filter() is a synonym of .where().

Note : what is deprecated, but still available, is the "query syntax". I
even think it's no longer documented. Recommended syntaxes are here:

https://docs.sqlalchemy.org/en/20/changelog/migration_20.html#migration-orm-usage

It's a bit more verbose, but closer to SQL :)

Laurent.

Le jeu. 23 nov. 2023 à 20:08, Mike Orr  a écrit :

> On Thu, Nov 23, 2023 at 7:03 AM Oberdan Santos 
> wrote:
> > O codigo no formato acima não executou, apresentou erro, mas foi de
> grande valia,  muito obrigado). Fiz uma pequena alteração e deu certo.
> > Funcionou assim:
> > cpf = request.params["cpf"]
> >rows =
> request.dbsession.query(Paciente).filter(Paciente.cpf==cpf).all()
>
> I remembered that after I wrote the comment, that it's
> `filtey(Paciente.cpf==cpf)` and `filter_by(cpf=cpf)`. And `filter_by'
> may not be supported in SQLAlchemy 2.0? I'm still on 1.4/1.3.
>
> I'm afraid I don't know enough Portuguese to understand the rest of the
> message.
>
> --
> You received this message because you are subscribed to the Google Groups
> "pylons-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/pylons-discuss/CAH9f%3DupAGsreB_H48SxC0_5fRctbdKSxs7kn1dcOrfZRaycJbw%40mail.gmail.com
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/pylons-discuss/CAB7cU6zkW0WfNdsjtUd-iHPsTJy%2BYZsophLZRht1BSNRwjWo%3Dg%40mail.gmail.com.


Re: [pylons-discuss] Display query and result on the same page

2023-11-23 Thread Mike Orr
On Thu, Nov 23, 2023 at 7:03 AM Oberdan Santos  wrote:
> O codigo no formato acima não executou, apresentou erro, mas foi de grande 
> valia,  muito obrigado). Fiz uma pequena alteração e deu certo.
> Funcionou assim:
> cpf = request.params["cpf"]
>rows = request.dbsession.query(Paciente).filter(Paciente.cpf==cpf).all()

I remembered that after I wrote the comment, that it's
`filtey(Paciente.cpf==cpf)` and `filter_by(cpf=cpf)`. And `filter_by'
may not be supported in SQLAlchemy 2.0? I'm still on 1.4/1.3.

I'm afraid I don't know enough Portuguese to understand the rest of the message.

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/pylons-discuss/CAH9f%3DupAGsreB_H48SxC0_5fRctbdKSxs7kn1dcOrfZRaycJbw%40mail.gmail.com.


Re: [pylons-discuss] Display query and result on the same page

2023-11-23 Thread Oberdan Santos

The simplest way is:
```
cpf = request.params["cpf"]
rows = request.dbsession.query(Paciente).filter(cpf=cpf),all()
```
O codigo no formato acima não executou, apresentou erro, mas foi de grande 
valia,  muito obrigado). Fiz uma pequena alteração e deu certo. 
Funcionou assim:
cpf = request.params["cpf"]
   rows = request.dbsession.query(Paciente).filter(Paciente.cpf==cpf).all()

Você deve notado  no enuciado do assunto, que além da consulta, tenho o 
problema do resultado ser publicado em outra pagina.  

codigo da pagina de consulta
# templates/pac_recepx.jinja2

 
  
Consultar cadastro do 
paciente

http://localhost:6543/queryx"; method="GET">
Digite o CPF (11 números)

Consultar




O resultado  esta indo para ...
action="http://localhost:6543/queryx

Como faço para levar esse resultado para dentro da mesma  da pagina que 
esta fazendo a  consulta (templates/pac_recepx.jinja2 ), ou seja, coloca-lo 
abaixo da consulta?

Desde já agradeço seu apoio.

Oberdan Costa


Em quarta-feira, 22 de novembro de 2023 às 20:33:04 UTC-3, Mike Orr 
escreveu:

> The simplest way is:
>
> ```
> cpf = request.params["cpf"]
> rows = request.dbsession.query(Paciente).filter(cpf=cpf),all()
> ```
>
> This doesn't do any validation or graceful error reporting on the
> input value, so you may want to use Colander or FormEncode for that. I
> use FormEncode. Otherwise invalid input may lead to a Python exception
> and an Internal Server Error for the user, and 'cpf' will be a string
> even if the database field is integer.
>
> On Wed, Nov 22, 2023 at 1:34 PM Oberdan Santos  wrote:
> >
> > Hello!!! I'm trying to make the code below work as follows.
> > When filling out the data entry, click on the query button.
> >
> > # template/pac_recepx. jinja2
> >
> > 
> > 
> > Consultar cadastro do 
> paciente
> > 
> > http://localhost:6543/queryx"; method="GET">
> > Digite o CPF (11 números)
> >  required maxlength="11" value=''>
> >  type="submit">Consultar
> > 
> > 
> >
> > In this view, using the code below, it shows all registered patients.
> >
> > @view_config(route_name='queryp', 
> renderer='piprdc:templates/pac_query.jinja2')
> > def queryp(request):
> > rows = request.dbsession.query(Paciente).all()
> > pacientes=[]
> > for row in rows:
> > pacientes.append({"id":row.id, "name":row.name, "idade":row.idade, 
> "data_nascimento":row.data_nascimento, "sexo":row.sexo,
> > "raca":row.raca, "fone":row.fone, "endereco":row.endereco, 
> "cpf":row.cpf, "cns":row.cns})
> > return{'pacientes':pacientes}
> >
> > The question is, how do I make it show only the patient I requested the 
> appointment with? I've looked at a lot of content, but I can't create a 
> logic for it to take the past information (cpf), compare it and display it.
> >
> > Every help is welcome.
> >
> > Oberdan costa
> >
> >
> >
> > --
> > You received this message because you are subscribed to the Google 
> Groups "pylons-discuss" group.
> > To unsubscribe from this group and stop receiving emails from it, send 
> an email to [email protected].
> > To view this discussion on the web visit 
> https://groups.google.com/d/msgid/pylons-discuss/25b36bc3-0c71-4587-b79f-dfb26e83e7f7n%40googlegroups.com
> .
>
>
>
> -- 
> Mike Orr 
>

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/pylons-discuss/10bef9da-a8cc-4553-b625-614128268bccn%40googlegroups.com.


Re: [pylons-discuss] Display query and result on the same page

2023-11-22 Thread Mike Orr
The simplest way is:

```
cpf = request.params["cpf"]
rows = request.dbsession.query(Paciente).filter(cpf=cpf),all()
```

This doesn't do any validation or graceful error reporting on the
input value, so you may want to use Colander or FormEncode for that. I
use FormEncode. Otherwise invalid input may lead to a Python exception
and an Internal Server Error for the user, and 'cpf' will be a string
even if the database field is integer.

On Wed, Nov 22, 2023 at 1:34 PM Oberdan Santos  wrote:
>
> Hello!!! I'm trying to make the code below work as follows.
> When filling out the data entry, click on the query button.
>
> # template/pac_recepx. jinja2
>
> 
> 
> Consultar cadastro do 
> paciente
> 
>  action="http://localhost:6543/queryx"; method="GET">
> Digite o CPF (11 números)
>  name="cpf" required maxlength="11" value=''>
>  type="submit">Consultar
> 
> 
>
> In this view, using the code below, it shows all registered patients.
>
> @view_config(route_name='queryp', 
> renderer='piprdc:templates/pac_query.jinja2')
> def queryp(request):
>rows = request.dbsession.query(Paciente).all()
>pacientes=[]
>for row in rows:
>   pacientes.append({"id":row.id, "name":row.name, "idade":row.idade, 
> "data_nascimento":row.data_nascimento, "sexo":row.sexo,
> "raca":row.raca, "fone":row.fone, 
> "endereco":row.endereco, "cpf":row.cpf, "cns":row.cns})
>return{'pacientes':pacientes}
>
> The question is, how do I make it show only the patient I requested the 
> appointment with? I've looked at a lot of content, but I can't create a logic 
> for it to take the past information (cpf), compare it and display it.
>
> Every help is welcome.
>
> Oberdan costa
>
>
>
> --
> You received this message because you are subscribed to the Google Groups 
> "pylons-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected].
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/pylons-discuss/25b36bc3-0c71-4587-b79f-dfb26e83e7f7n%40googlegroups.com.



-- 
Mike Orr 

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/pylons-discuss/CAH9f%3DupkZ8LkurvA-zShkYF4PORqtCW%3D0dDSbshjTUW87PSdEw%40mail.gmail.com.