[web2py] Re: json Data from the server not displaying!

2023-03-20 Thread Leonel Câmara
The problem is that you are creating the string you want to return and then 
putting it in a dictionary. You could just have returned the string. That 
said it's better to use response.json which does a few other things for you.

Try this

def dashboard():
call_clicks = db().select(db.dashboard.call_clicks)
social_clicks = db().select(db.dashboard.social_clicks)
profile_views = db().select(db.dashboard.profile_views)
data = {
'call_clicks': [c.call_clicks for c in call_clicks],
'social_clicks': [s.social_clicks for s in social_clicks],
'profile_views': [p.profile_views for p in profile_views]
}
return response.json(data) 

A segunda-feira, 20 de março de 2023 à(s) 10:21:21 UTC, mostwanted escreveu:

> I have data that is in json format from the database, i want to draw a 
> graph with data but i'm not sure how to display it in my view in graph 
> format using the plotly library. My view code display nothing!
>
> *CONTROLLER*
> def dashboard():
> call_clicks = db().select(db.dashboard.call_clicks)
> social_clicks = db().select(db.dashboard.social_clicks)
> profile_views = db().select(db.dashboard.profile_views)
> data = {
> 'call_clicks': [c.call_clicks for c in call_clicks],
> 'social_clicks': [s.social_clicks for s in social_clicks],
> 'profile_views': [p.profile_views for p in profile_views]
> }
> data_json = json.dumps(data)
> return dict(data_json=data_json)
>
> *VIEW*
> *This code displays nothing*
>
> {{extend 'layout.html'}}
>
> 
> Data
> 
> 
>
> https://cdn.plot.ly/plotly-latest.min.js";>
>
> 

[web2py] Re: Validator not saving images

2023-03-20 Thread Leonel Câmara
Like I said I didn't test it :) apparently you need to seek to zero after 
tesseract and not after image.open as tesseract is reading it. Fixed 
version (which I actually tested):

# -*- coding: utf-8 -*-
import pytesseract
from PIL import Image
from pydal.validators import Validator, ValidationError

class IMG_HAS_TEXT(Validator):

def __init__(self, check_tokens=None, error_message="Image doesn't have 
the required text"):
self.error_message = error_message
self.check_tokens = check_tokens or []

def validate(self, value, record_id=None):
try:
image = Image.open(value.file)
text = pytesseract.image_to_string(image).lower()
value.file.seek(0)
if not text or not all(token in text for token in 
self.check_tokens):
raise ValidationError(self.translator(self.error_message))
return value
except Exception as e:
raise ValidationError(self.translator(self.error_message))

class IS_RECEIPT_OR_INVOICE(IMG_HAS_TEXT):

def __init__(self, error_message="The uploaded file is not a receipt or 
invoice!"):
self.error_message = error_message
self.check_tokens = ("receipt", "invoice")




A sábado, 18 de março de 2023 à(s) 07:20:04 UTC, mostwanted escreveu:

> Hey Leonel, thanks for your reply but the above code is still saving 
> empty (0byte) images
>
> On Friday, March 17, 2023 at 5:22:07 PM UTC+2 Leonel Câmara wrote:
>
>> What's happening here is that your validator is reading the file and it's 
>> not "rewinding" it afterwards so when the next step gets it the file 
>> descriptor is at the end of the file hence the ' bytes size.
>>
>> I would recommend rewriting it to something like this (the important part 
>> being the value.file.seek(0)) note that I haven't tested this code at all.
>>
>> import pytesseract
>> from PIL import Image
>> from pydal.validators import Validator, ValidationError
>>
>> class IMG_HAS_TEXT(Validator):
>>
>> def __init__(self, check_tokens=None, error_message="Image doesn't 
>> have the required text"):
>> self.error_message = error_message
>> self.check_tokens = check_tokens or []
>>
>> def validate(self, value, record_id=None):
>> try:
>> image = Image.open(value.file)
>> value.file.seek(0)
>> text = pytesseract.image_to_string(image).lower()
>> if not text or not all(token in text for token in 
>> self.check_tokens):
>> raise ValidationError(self.translator(self.error_message))
>> return value
>> except:
>> raise ValidationError(self.translator(self.error_message))
>>
>> class IS_RECEIPT_OR_INVOICE(IMG_HAS_TEXT):
>>
>> def __init__(self, error_message="The uploaded file is not a receipt 
>> or invoice!"):
>> self.error_message = error_message
>> self.check_tokens = ("receipt", "invoice")
>>
>>
>> A terça-feira, 14 de março de 2023 à(s) 20:55:11 UTC, mostwanted escreveu:
>>
>>> I have a little problem with my validator code, its saving 0 byte images 
>>> and I just don't know why, the value variable is an image that actually 
>>> contains an image, I have tested it several times to see what it contains 
>>> and its an image but  for some reason when it comes to saving it in the 
>>> database it saves an empty image, please assist me where I could be going 
>>> wrong. Regards
>>>
>>> from gluon import *
>>> import pytesseract
>>> from PIL import Image
>>>
>>> class IS_RECEIPT_OR_INVOICE(object):
>>> def __init__(self, error_message="The uploaded file is not a receipt 
>>> or invoice!"):
>>> self.error_message = error_message
>>>
>>> def __call__(self, value):
>>> error = None
>>> try:
>>> image = Image.open(value.file)
>>> text = pytesseract.image_to_string(image)
>>> if "receipt" not in text.lower() and "invoice" not in 
>>> text.lower() and "RECEIPT" not in text and "INVOICE" not in text:
>>> error = self.error_message
>>> except:
>>> error = self.error_message
>>> return (value, error)
>>
>>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/web2py/eb4345c4-46c1-40b6-ae9c-86a033444b24n%40googlegroups.com.


[web2py] Re: xmlrpc python3 error

2023-03-20 Thread Lisandro
Just in case it helps someone, I was able to change my code to make it 
work. It's not a complete solution: I just started using args instead of 
vars.
I'll explain: in the client I was calling the service with some query 
string arguments, like this:

from gluon.contrib.simplejsonrpc import ServerProxy
ws = 
ServerProxy('http://mysite.com/ws/call/jsonrpc?token=ce3e1298-b7b3-40f6-83f2-f78c4360db8d')

... and in the function exposing the service I was using the token to 
identify the client:

from gluon.tools import Service
service = Service()

def call():
if not db(db.clients.token == request.vars.token).count():
raise HTTP(403)
return service()


So I just had to make this change in the client:

ws = 
ServerProxy('http://mysite.com/ws/call/jsonrpc/ce3e1298-b7b3-40f6-83f2-f78c4360db8d')


... and this change in the service:

def call():
if not db(db.clients.token == request.args(1)).count():
raise HTTP(403)
return service()

I hope it helps someone.
Best regards,
Lisandro
El domingo, 19 de marzo de 2023 a la(s) 19:56:20 UTC-3, Lisandro escribió:

> Hey there! 
> I'm experiencing the same error after migrating to python3 and web2py 
> version 2.23.1-stable+timestamp.2023.01.31.08.01.46
> The traceback is the same, the error is originated when I try to access 
> request.vars:
>
> Traceback (most recent call last):
> File "/home/limon/medios/gluon/restricted.py", line 219, in restricted
> exec(ccode, environment)
> File "/home/limon/medios/applications/webmedios/controllers/ws.py", line 
> 1423, in 
> File "/home/limon/medios/gluon/globals.py", line 430, in 
> self._caller = lambda f: f()
> File "/home/limon/medios/applications/webmedios/controllers/ws.py", line 
> 13, in call
> if not request.vars.token or not db(db.sitios.token == request.vars.token
> ).count():
> File "/home/limon/medios/gluon/globals.py", line 325, in vars
> self.parse_all_vars()
> File "/home/limon/medios/gluon/globals.py", line 296, in parse_all_vars
>
> for key, value in iteritems(self.post_vars):
> File "/home/limon/medios/gluon/globals.py", line 317, in post_vars
> self.parse_post_vars()
> File "/home/limon/medios/gluon/globals.py", line 253, in parse_post_vars
> dpost = cgi.FieldStorage(fp=body, environ=env, headers=headers, 
> keep_blank_values=1)
> File "/usr/lib64/python3.6/cgi.py", line 569, in __init__
> self.read_single()
> File "/usr/lib64/python3.6/cgi.py", line 761, in read_single
> self.read_binary()
> File "/usr/lib64/python3.6/cgi.py", line 783, in read_binary
> self.file.write(data)
> TypeError: write() argument must be str, not bytes
>
> I've found that this was related to a python bug 
>  that is still opened, but at the 
> same time I've seen that web2py received a workaround fix 
> 
> . 
> I'm running the last web2py stable version. And just in case I checked the 
> source code and it is running with the workaround fix. But the error still 
> remains.
> What should I try? I'm a bit lost :/
>
> El lunes, 7 de octubre de 2019 a la(s) 18:08:12 UTC-3, Mark escribió:
>
>> I submitted the bug report to the github. 
>>
>> Thank you very much.
>>
>>
>> On Sunday, October 6, 2019 at 5:29:31 PM UTC-4, Dave S wrote:
>>>
>>>
>>>
>>> On Friday, September 27, 2019 at 6:39:00 AM UTC-7, Mark wrote:

 I am using either Rocket or Azure, and get the same error.

 Yes, there is a ticket, which I didn't realize before:

 Traceback (most recent call last):
   File "R:\web2py\gluon\restricted.py", line 219, in restricted
 exec(ccode, environment)
   File "R:\web2py\applications\myapp\models\db.py", line 321, in 
 
 '')
   File "R:\web2py\gluon\tools.py", line 884, in __init__
 self.request_vars = request and request.vars or current.request.vars
   File "R:\web2py\gluon\globals.py", line 316, in vars
 self.parse_all_vars()
   File "R:\web2py\gluon\globals.py", line 287, in parse_all_vars
 for key, value in iteritems(self.post_vars):
   File "R:\web2py\gluon\globals.py", line 308, in post_vars
 self.parse_post_vars()
   File "R:\web2py\gluon\globals.py", line 244, in parse_post_vars
 dpost = cgi.FieldStorage(fp=body, environ=env, keep_blank_values=1)
   File "c:\python37\lib\cgi.py", line 491, in __init__
 self.read_single()
   File "c:\python37\lib\cgi.py", line 682, in read_single
 self.read_binary()
   File "c:\python37\lib\cgi.py", line 704, in read_binary
 self.file.write(data)
   File "c:\python37\lib\tempfile.py", line 481, in func_wrapper
 return func(*args, **kwargs)
 TypeError: write() argument must be str, not bytes


 Thanks!

>>>
>>> This looks like a place where something got missed in the Py3 work.  I 
>>> suspect in cgi.py, maybe because testing used a uwsgi 

[web2py] json Data from the server not displaying!

2023-03-20 Thread mostwanted
I have data that is in json format from the database, i want to draw a 
graph with data but i'm not sure how to display it in my view in graph 
format using the plotly library. My view code display nothing!

*CONTROLLER*
def dashboard():
call_clicks = db().select(db.dashboard.call_clicks)
social_clicks = db().select(db.dashboard.social_clicks)
profile_views = db().select(db.dashboard.profile_views)
data = {
'call_clicks': [c.call_clicks for c in call_clicks],
'social_clicks': [s.social_clicks for s in social_clicks],
'profile_views': [p.profile_views for p in profile_views]
}
data_json = json.dumps(data)
return dict(data_json=data_json)

*VIEW*
*This code displays nothing*

{{extend 'layout.html'}}


Data



https://cdn.plot.ly/plotly-latest.min.js";>