Tuesday 31 October 2023

how keep the hover enabled while the submenu is open

I have a simple table on my website listing devices and their characteristics (in the example at the link below there will be a shortened version of the table).

import "./styles.css";
import { SubMenu } from "./SubMenu";

const subMenuSlice = <SubMenu />;

const nodes = [
  {
    id: "0",
    name: "Samsung Galaxy",
    subMenu: subMenuSlice
  },
  {
    id: "0",
    name: "Iphone",
    subMenu: subMenuSlice
  }
];

export default function App() {
  return (
    <table>
      <tbody>
        {nodes.map((val, key) => (
          <tr key={key}>
            <td>{val.name}</td>
            <td>{val.subMenu}</td>
          </tr>
        ))}
      </tbody>
    </table>
  );
}

https://codesandbox.io/s/romantic-rgb-5t7xkq

As you can see, when you hover over any of the lines, the entire line turns gray and an additional button appears. By clicking on this button the user gets a submenu.

Description of the problem: the problem is that when the user moves the cursor to the submenu, the hover (gray) disappears from the table row. Please tell me how to keep the hover enabled while the submenu is active (open)



from how keep the hover enabled while the submenu is open

How can I resolve this DOMContentLoaded or addEventListener issue?

This is a finicky issue and may be difficult to figure out.

This JavaScript controls the intro, play and stop/start of an mp4 video. Most of the code has to do with the video playback and position of the navigation images and links, and isn't really relevant to this question.

The problem that I need to track down is that on initial page load, sometimes the canvas div is hidden by the display:none in the <video> tag and the video start and canvas div is blank. This doesn't happen all the time; it happens less often in Firefox, but most of the time in Chrome. A mouse click in the blank canvas div will show the video and introstill.png. A hard refresh will sometimes bring up the video start image in the canvas div.

I'm thinking it has something to do with the way the function at the end of the script is constructed with myvideo.init and maybe breaks addEventListener, but I don't know.

I see the issue in both an .html file in localhost and on the live server. I've tried wrapping the whole function in jQuery('document').ready(function() with no luck. I've tried disabling caching of the mp4 at the webhost. There are no browser console errors, and no browser add-ons conflicting.

As per @titus' comment: how would I change document.addEventListener('DOMContentLoaded', () => { ?

And, I don't know enough JavaScript to strip out the irrelevant code to make a minimal reproducible example of addEventListener or DOMContentLoaded.

<script>

    var myvideo = (function (window) {
        var app = {};
    
        var buttonBg_out = new Image();
        var buttonBg_over = new Image();
        var nextText = new Image();
        var beginText = new Image();
        var startAgain = new Image();
        var introStill = new Image();
        var pauseOverlay = new Image();
        
        var REAL_WIDTH = 656;
        var REAL_HEIGHT = 386;
        var ASPECT_RATIO = REAL_WIDTH / REAL_HEIGHT;
        var SCALE = 1.0;
    
        app.init = function (root) {
        buttonBg_out.src = root + 'button-out.png';
        buttonBg_over.src = root + 'button-over.png';
        nextText.src = root + 'next.png';
        beginText.src = root + 'begin.png';
        startAgain.src = root + 'startagain.png';
        introStill.src = root + 'introstill.png';   
        pauseOverlay.src = root + 'pauseoverlay.png';
    
        var document = window.document;
    
        document.addEventListener('DOMContentLoaded', () => {          // addEventListener
            var video = document.getElementById('myvid');
            var canvas = document.getElementById('playbackcontainer');
    
            var ctx = canvas.getContext('2d');
                
            introStill.onload = () => {
            ctx.drawImage(introStill,0,31,introStill.width,introStill.height);
            };
    
            var rescaling = function () {
            SCALE = canvas.clientWidth / REAL_WIDTH;
            setTimeout(rescaling, 10);
            };
            rescaling();
            
            var drawVideo = function () {
            if (video.paused || video.ended) return;
    
            ctx.drawImage(video,0,0,canvas.width,canvas.height);
            drawButton();
            setTimeout(drawVideo,1); // might fiddle with this timeout;
            };
            
            var button = {x:0,y:0,
                  w:127,
                  h:66,
                  state: buttonBg_out,
                  text: beginText,
                  visible: false
                 };
            
            var localXY = function (e) {
            var rect = canvas.getBoundingClientRect();
            var x = e.clientX - rect.left;
            var y = e.clientY - rect.top;
            return {x:x,y:y};
            };
            
            var isOverButton = function (e) {
            var xy = localXY(e);
            var x = xy.x;
            var y = xy.y;
    
            return (x >= (button.x * SCALE) && y >= (button.y * SCALE) &&
                x <= ((button.x + button.w)*SCALE) &&
                y <= ((button.y + button.h)*SCALE));
            };
            
            var drawButton = function () {
            if (button.visible) {
                ctx.drawImage(button.state,
                      button.x ,button.y ,
                      button.w ,button.h );
                
                ctx.drawImage(button.text,
                      (button.x + 14) ,
                      (button.y + 23) ,
                      button.text.width ,
                      button.text.height );
            }
            };
            
            var isOverStartOver = function (e) {
            var xy = localXY(e);
            return xy.x >= 9*SCALE && xy.x <= 92*SCALE && xy.y >= 367*SCALE && xy.y <= 379*SCALE;
            };
            
            var isOverReplayScene = function (e) {
            var xy = localXY(e);
            return xy.x >= 102*SCALE && xy.x <= 186*SCALE && xy.y >= 367*SCALE && xy.y <= 379*SCALE;
            };
            
            var permissionGiven = false;
            
            var currentScene = 0;
            var sceneBounds = [{start:4.583, showButton:13.849,
                    loopStart:15.116, loopEnd:22.183,
                    afterPress:23.416},
                       
                       {start:25.649, showButton:53.416,
                    afterPress: 55.983},
                       
                       {start:58.216, showButton:99.0,
                    afterPress:100.483},
                       
                       {start:103.649, showButton:156.149,
                    afterPress:156.816},
                       
                       {start:159.049, showButton:229.883,
                    loopStart:230.5, loopEnd:236.383,
                    afterPress:242.00}];
            
            video.addEventListener('play', drawVideo, false);
            video.addEventListener('timeupdate', () => {
            var bounds = sceneBounds[currentScene];
            
            if (video.currentTime >= bounds.showButton) {
                button.visible = true;
                drawButton();
            }
            
            if (bounds.loopEnd != null) { // keep playing
                if (video.currentTime >= bounds.loopEnd) {
                video.currentTime = bounds.loopStart;
                }
            } else if (video.currentTime >= bounds.showButton) {        // pause
                video.pause();
            }
            });
            
            canvas.addEventListener('mousemove', (e) => {
            if (isOverButton(e)) {
                button.state=buttonBg_over;
                drawButton();
            } else {
                button.state=buttonBg_out;
                drawButton();
            }
            });
            
            canvas.addEventListener('mousedown', (e) => {
            if (!permissionGiven)
            {
                permissionGiven = true;
                video.currentTime = sceneBounds[0].start;
                video.play();
                button.x = 322;
                button.y = 244;
    
            } else {
    
                if (video.paused && currentScene > 0 && currentScene < 4 && !isOverButton(e)) {
                if (isOverStartOver(e)) {
                    video.currentTime = sceneBounds[0].start;
                    button.visible = false;
                    button.x = 322;
                    button.y = 244;
                    button.text = beginText;
                    currentScene = 0;
                    video.play();
                } else if (isOverReplayScene(e)) {
                    button.visible = false;
                    video.currentTime = sceneBounds[currentScene].start;
                    video.play();
                } else {
                    video.play();
                }
    
                } else if (currentScene == 4 && !isOverButton(e)) {
    
                if (isOverStartOver(e)) {
                    button.visible = false;
                    video.currentTime = sceneBounds[currentScene].start;
                    video.play();
                }
    
                } else if (button.visible && isOverButton(e)) { // button was presssed
                
                currentScene = (currentScene +  1) % sceneBounds.length;
                
                if (currentScene == 0)
                {
                    button.text = beginText;
                    video.currentTime = sceneBounds[currentScene].start;
                    button.x = 322;
                    button.y = 244;
                } else {
                    button.text = nextText;
                    button.x = 515;
                    button.y = 304;
                    video.currentTime = sceneBounds[currentScene-1].afterPress;
                }
                
                if (currentScene == 4)
                    button.text = startAgain;
                
                button.visible = false;
                
                video.play();
    
                } else if (video.paused) {
                video.play();
                } else {
                video.pause();
                ctx.drawImage(pauseOverlay,0,0,canvas.width,canvas.height);
                }
            }
            });
        });
        };
        return app;
    })(window);
    
    (function(){
    var thevid = document.getElementById("myvid");
    myvideo.init("/path/to/folder/with/images/and/mp4/");
    })();
    
    </script>
    
    <div class="flex-video">
    <video id="myvid" style="display:none;" playsinline>
    <source src="/path/to/folder/with/images/and/mp4/the_video.mp4" type="video/mp4"/></video>
    <canvas id="playbackcontainer" width="656" height="386"></canvas>
    </div>


from How can I resolve this DOMContentLoaded or addEventListener issue?

Supervisor (superuser) approval of root superuser login in Django

In my Django project with two superusers called root and supervisor, I'm trying to implement the following flow:

  • When root logs into the application, he is directed to a 'waiting for authorisation' page

  • In the meantime, an email is sent to the supervisor containing the OTP in plain text as well as a link to the page where the otp needs to be entered.

  • When the supervisor clicks this link, enters the OTP and clicks 'Approve', OTP verification for root occurs

  • If authentication is successful, the supervisor sees a message saying OTP authentication successful, and in the meantime, the root user is redirected from the waiting page to the landing page

  • If authentication fails, root and the supervisor are shown a message on the page saying that the OTP authorisation failed

The only user who needs OTP authentication is root and approval can only be granted by supervisor.

I have everything down except for the part where I login the root user.

Using debugging statements, I can see that I am able to access the root user's session details correctly and create a new request to associate with that session, but ultimately, supervisor ends up getting redirected to the landing page.

I am sharing the relevant parts of my code below:

in models.py:

class OTP(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    otp_secret = models.CharField()
    is_verified = models.BooleanField(default=False)

    def __str__(self):
        return self.user.email

class UserSession(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    session = models.ForeignKey(Session, on_delete=models.CASCADE)

in views.py:

User = get_user_model()

user = User.objects.get(username='supervisor')
SUPERVISOR_EMAIL = user.email

user2 = User.objects.get(username='root')
ROOT_EMAIL = user2.email

logger = logging.getLogger(__name__)

#Interval (in seconds) for OTP verification
otp_interval = 300 # 300 seconds (5 minutes)

@login_required(login_url='custom_login')
def logout(request):
    delete_user_sessions(request.user)
    django_logout(request)
    return redirect('custom_login')

def user_logged_in_handler(sender, request, user, **kwargs):
    UserSession.objects.get_or_create(
        user = user,
        session_id = request.session.session_key
    )

def delete_user_sessions(user):
    user_sessions = UserSession.objects.filter(user = user)
    for user_session in user_sessions:
        user_session.session.delete()


def get_supervisor_email():
    try:
        supervisor_user = User.objects.get(username='supervisor')
        return supervisor_user.email
    except User.DoesNotExist:
        # Handle the case where the 'supervisor' user does not exist
        raise User.DoesNotExist('The \'supervisor\' user does not exist.')

def get_root_email():
    try:
        root_user = User.objects.get(username='root')
        return root_user.email
    except User.DoesNotExist:
        # Handle the case where the 'supervisor' user does not exist
        raise User.DoesNotExist('The \'root\' user does not exist.')

def is_root_user(user):
    return user.is_authenticated and user.is_superuser and user.username == 'root'
    
def generate_otp(request, user):
    logger.debug('inside generate_otp method')
    
    if request is None:
        logger.error('generate_otp: The \'request\' object is missing for generating the OTP.')
        messages.error(request, 'Something went wrong. Please try again.')
        return render(request, 'something_went_wrong.html')

    elif request is not None and user.username=='root' and user.is_authenticated:
        try:
            logger.debug('generate_otp: request is not None and user.username==root and user.is_authenticated')
            logger.debug('generate_otp: inside the try block in generate_otp method')
            supervisor_email = get_supervisor_email()
            ROOT_EMAIL = get_root_email()
            

            if not request.session.session_key:
                logger.debug('generate_otp: saving session because session key is none')
                request.session.save()

            session_data = {
                "user_id": user.id,
                "role": "root",
                "session_key": request.session.session_key
            }

            logger.debug('generate_otp: created session_data dictionary')

            request.session['root_data'] = session_data
            request.session['root_target_url'] = reverse('landingpage')
            request.session.save()

            session_key = request.session.session_key
            session = Session.objects.get(session_key=session_key)

            
            user_session = UserSession(user=user, session=session)
            user_session.save()
            logger.debug('generate_otp: saved session data to main_usersession')

            otp_secret = pyotp.random_base32()
            logger.debug('inside generate_otp, otp_secret is: %s', otp_secret)
            otp = pyotp.TOTP(otp_secret, interval=otp_interval)
            otp_code = otp.now()
            logger.debug('otp_code generated from within generate_otp is %s', otp_code)
            
            # Save OTP to the database
            otp_obj, created = OTP.objects.get_or_create(user=user)
            otp_obj.otp_secret = str(otp_secret)
            logger.debug('otp_secret is %s', otp_secret)
            otp_obj.save()
            logger.debug('otp saved to db, in generate_otp method')
            
            # Prepare the verification link
            current_site = get_current_site(request)
            verification_link = f"http://{current_site}{reverse('enter_otp')}"
            
            # Prepare email
            subject = 'OTP for Root Super User Login Approval'
            message = f'The OTP to approve the superuser with email address {ROOT_EMAIL} is: {otp_code}. Navigate to this page to enter the OTP and approve this user\'s login {verification_link}'
            from_email = settings.DEFAULT_FROM_EMAIL
            recipient_list = [supervisor_email]
            send_mail(subject, message, from_email, recipient_list)
            logger.debug('Email message: %s %s', message, user.username)
            messages.success(request, f'OTP has been shared with your supervisor on email at {supervisor_email}')
            return render(request, 'login_redirect.html')
            
        except Exception as e:
            logger.error(f'An error occurred during OTP generation and email sending: {str(e)} %s', user.username)
            messages.error(request, 'An error occurred during login. Please try again.')
            return render(request, 'something_went_wrong.html')


@login_required(login_url='custom_login')
def verify_otp(request):
    logger.debug('inside verify_otp. request.user is %s', request.user)
    request.session.save()
    otp = request.POST.get('otp')
    logger.debug('inside verify_otp: otp is %s. Request.user is: %s', otp, request.user)

    try:
        latest_root_session = UserSession.objects.filter(user=user2, user__otp__is_verified=False).latest('session__expire_date')
        logger.debug('inside verify_otp, latest_root_session fetched. Request.user is %s', request.user)

        latest_root_session_user_id = latest_root_session.user.id
        logger.debug('inside verify_otp, latest_root_session_user_id fetched. Request.user is %s', request.user)

    except ObjectDoesNotExist:
        latest_root_session = None

    if latest_root_session: 
        if latest_root_session_user_id == user2.id:
            logger.debug('inside verify_otp, if latest_root_session and latest_root_session_user_id = user2.id is true. Request.user is %s', request.user)
            try:
                logger.debug('inside verify_otp, inside try block. Request.user is %s', request.user)
                session = latest_root_session.session
                session_key = session.session_key
                logger.debug('inside verify_otp, session and session key extracted from main_usersession. Request.user is %s', request.user)

                if otp is not None and otp != 'None':
                    try:
                        int_otp = int(otp)
                    except ValueError:
                        messages.error(request, 'Invalid OTP format.')
                        return render(request, 'something_went_wrong.html')
                else:
                    messages.error(request, 'OTP is missing.')
                    return render(request, 'something_went_wrong.html')

                otp_obj = OTP.objects.filter(user__pk=user2.id).first()
                otp_secret = otp_obj.otp_secret
                logger.debug('inside verify_otp, fetched otp_obj for user2 which is root. Request.user is %s', request.user)
                logger.debug('otp_obj: %s. Request.user is', otp_obj, request.user)
                logger.debug('otp_secret: %s. Request.user is %s', str(otp_secret), request.user)
                logger.debug('int_otp: %s. Request.user is %s', int_otp, request.user)
                
                if otp_obj and int_otp:
                    logger.debug('inside verify_otp, if otp_obj and int_otp is true. Request.user is %s', request.user)
                    otp = pyotp.TOTP(otp_secret, interval=otp_interval)
                    logger.debug('inside verify_otp, otp extracted from otp_obj otp secret: %s. Request.user is %s', otp, request.user)
                    logger.debug('inside verify_otp, int_otp: %s. Request.user is %s', int_otp, request.user)
                    verification_result = otp.verify(int_otp)
                    logger.debug('verification_result: %s. Request.otp is %s', verification_result, request.user)

                    if verification_result:
                        logger.debug('inside verify_otp, verification_result is true, OTP verification succeeded. Request.user is %s', request.user)
                        # Retrieve the target URL from the root user's session
                        session_data = latest_root_session.session.get_decoded()
                        root_target_url = session_data.get('root_target_url', None)
                        logger.debug('inside verify_otp, root_target_url with value %s has been fetched. Request.user is %s', root_target_url, request.user)

                        if root_target_url:
                            logger.debug('root_target_url exists. Request.user is %s', request.user)

                            session = latest_root_session.session
                            logger.debug('executed session = latest_root_session.session: %s. Request.user is %s', str(session), request.user)
                            
                            session_key = session.session_key
                            logger.debug('session_key = session.session_key: %s. Request.user is %s', str(session_key), request.user)
                            logger.debug('data type for session_key: %s. Request.user is %s', type(session_key), request.user)
                            
                            user = latest_root_session.user
                            logger.debug('user = latest_root_session.user: %s. Request.user is %s', str(user), request.user)
                            

                            # Create a new HttpRequest object based on the session
                            request = HttpRequest()
                            logger.debug('request = HttpRequest() executed.')
                            
                            request.session = SessionStore(session_key=session_key)
                            logger.debug('request.session = SessionStore(session_key=session_key) executed.')

                            request.user = user
                            logger.debug('request.user = user executed. Request.user is %s', request.user)
                            logger.debug('currently, request is %s and user is %s', request, user)
                            login(request, user)
                            return redirect('landingpage')
                        
                    else:
                        logger.debug('if verification_result evaluated to false')
                        messages.error(request, 'Root target URL is missing or user is not root.')
                        return render(request, 'something_went_wrong.html')

                else:
                    logger.debug('if otp_obj and int_otp is false')
                    messages.error(request, 'OTP authorization failed. Please try logging in again.')
                    return render(request, 'something_went_wrong.html')

            except signing.SignatureExpired:
                messages.error(request, 'The verification link has expired. Please try again.')
                return render(request, 'something_went_wrong.html')

            except signing.BadSignature:
                messages.error(request, 'Invalid or tampered verification link. Please try again.')
                return render(request, 'something_went_wrong.html')

        else:
            logger.debug('if latest_root_session_user_id == user2.id is false')
            messages.error(request, 'There are no sessions for the root user pending approval from supervisor.')
            return render(request, 'something_went_wrong.html')
    else:
        logger.debug('if latest_root_session condition failed')
        messages.error(request, 'There are no sessions for the root user pending approval from supervisor.')
        return render(request, 'something_went_wrong.html')

    messages.error(request, 'Something went wrong. Please try again after some time.')
    return render(request, 'something_went_wrong.html')

In the logs that are generated, at the end I see the following:

DEBUG 2023-10-31 16:04:08,275 views data type for session_key: <class 'str'>. Request.user is supervisor
DEBUG 2023-10-31 16:04:08,275 views user = latest_root_session.user: root. Request.user is supervisor
DEBUG 2023-10-31 16:04:08,279 views request = HttpRequest() executed.
DEBUG 2023-10-31 16:04:08,279 views request.session = SessionStore(session_key=session_key) executed.
DEBUG 2023-10-31 16:04:08,281 views request.user = user executed. Request.user is root
DEBUG 2023-10-31 16:04:08,281 views currently, request is <HttpRequest> and user is root

and the superuser is directed to the landing page, but nothing happens to the root user's page.

I would deeply appreciate any guidance or help. Thank you!



from Supervisor (superuser) approval of root superuser login in Django

Pytesseract OSError: [WinError 740] The requested operation requires elevation

i am following this tutorial for recognizing text from a .png image with pytesseract python library but i got this error:

---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
Cell In[2], line 18
    14 pytesseract.tesseract_cmd = path_to_tesseract
    16 # Passing the image object to image_to_string() function
    17 # This function will extract the text from the image
---> 18 text = pytesseract.image_to_string(img)
    20 # Displaying the extracted text
    21 print(text[:-1])

File ~\anaconda3\lib\site-packages\pytesseract\pytesseract.py:423, in image_to_string(image, lang, config, nice, output_type, timeout)
    418 """
    419 Returns the result of a Tesseract OCR run on the provided image to string
    420 """
    421 args = [image, 'txt', lang, config, nice, timeout]
--> 423 return {
    424     Output.BYTES: lambda: run_and_get_output(*(args + [True])),
    425     Output.DICT: lambda: {'text': run_and_get_output(*args)},
    426     Output.STRING: lambda: run_and_get_output(*args),
    427 }[output_type]()

File ~\anaconda3\lib\site-packages\pytesseract\pytesseract.py:426, in image_to_string.<locals>.<lambda>()
    418 """
    419 Returns the result of a Tesseract OCR run on the provided image to string
    420 """
    421 args = [image, 'txt', lang, config, nice, timeout]
    423 return {
    424     Output.BYTES: lambda: run_and_get_output(*(args + [True])),
    425     Output.DICT: lambda: {'text': run_and_get_output(*args)},
--> 426     Output.STRING: lambda: run_and_get_output(*args),
    427 }[output_type]()

File ~\anaconda3\lib\site-packages\pytesseract\pytesseract.py:288, in run_and_get_output(image, extension, lang, config, nice, timeout, return_bytes)
    277 with save(image) as (temp_name, input_filename):
    278     kwargs = {
    279         'input_filename': input_filename,
    280         'output_filename_base': temp_name,
(...)
    285         'timeout': timeout,
    286     }
--> 288     run_tesseract(**kwargs)
    289     filename = f"{kwargs['output_filename_base']}{extsep}{extension}"
    290     with open(filename, 'rb') as output_file:

File ~\anaconda3\lib\site-packages\pytesseract\pytesseract.py:255, in run_tesseract(input_filename, output_filename_base, extension, lang, config, nice, timeout)
    252     cmd_args.append(extension)
    254 try:
--> 255     proc = subprocess.Popen(cmd_args, **subprocess_args())
    256 except OSError as e:
    257     if e.errno != ENOENT:

File ~\anaconda3\lib\subprocess.py:951, in Popen.__init__(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags, restore_signals, start_new_session, pass_fds, user, group, extra_groups, encoding, errors, text, umask)
    947         if self.text_mode:
    948             self.stderr = io.TextIOWrapper(self.stderr,
    949                     encoding=encoding, errors=errors)
--> 951     self._execute_child(args, executable, preexec_fn, close_fds,
    952                         pass_fds, cwd, env,
    953                         startupinfo, creationflags, shell,
    954                         p2cread, p2cwrite,
    955                         c2pread, c2pwrite,
    956                         errread, errwrite,
    957                         restore_signals,
    958                         gid, gids, uid, umask,
    959                         start_new_session)
    960 except:
    961     # Cleanup if the child failed starting.
    962     for f in filter(None, (self.stdin, self.stdout, self.stderr)):

File ~\anaconda3\lib\subprocess.py:1420, in Popen._execute_child(self, args, executable, preexec_fn, close_fds, pass_fds, cwd, env, startupinfo, creationflags, shell, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite, unused_restore_signals, unused_gid, unused_gids, unused_uid, unused_umask, unused_start_new_session)
1418 # Start the process
1419 try:
-> 1420     hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
1421                              # no special security
1422                              None, None,
1423                              int(not close_fds),
1424                              creationflags,
1425                              env,
1426                              cwd,
1427                              startupinfo)
1428 finally:
1429     # Child is launched. Close the parent's copy of those pipe
1430     # handles that only the child should have open.  You need
(...)
1433     # pipe will not close when the child process exits and the
1434     # ReadFile will hang.
1435     self._close_pipe_fds(p2cread, p2cwrite,
1436                          c2pread, c2pwrite,
1437                          errread, errwrite)

OSError: [WinError 740] The requested operation requires elevation

is there any way to solve this problem?

this is my code:

from PIL import Image
from pytesseract import pytesseract

# Defining paths to tesseract.exe
# and the image we would be using
path_to_tesseract = r"tesseract.exe"
image_path = r"pdf2png/ActaConstitutiva0/ActaConstitutiva0-01.png"

# Opening the image & storing it in an image object
img = Image.open(image_path)

# Providing the tesseract executable
# location to pytesseract library
pytesseract.tesseract_cmd = path_to_tesseract

# Passing the image object to image_to_string() function
# This function will extract the text from the image
text = pytesseract.image_to_string(img)

# Displaying the extracted text
print(text[:-1])

i am installing tesseract.exe from here

i have python version 3.10.6 and pytesseract 0.3.10

i am running this code from jupyter notebook 6.4.1

i also tried following this answer from stack overflw but it didnt worked...



from Pytesseract OSError: [WinError 740] The requested operation requires elevation

Monday 30 October 2023

Customizing Autodesk Forge Viewer Document Browser Extension for Multi-Model Support

I am working with the Autodesk Forge Viewer and using the Autodesk.DocumentBrowser extension. My goal is to extend the functionality of the Document Browser to support multiple models simultaneously. Currently, the extension only shows sheets and views for a single model, and I want to customize it to display sheets and views from all the loaded models in the viewer.

I've looked into the existing answer here and it seems that this feature is not implemented by default. Therefore, I'm trying to create a custom extension based on the source code of the Document Browser extension here.

In the Document Browser source code, there's a method _hookToModel which is used to set up the Autodesk.Viewing.UI.Tree root node. The TreeView currently accepts one parent BubbleNode, which can be retrieved from a single model. I tried to create a parent node containing all the models' root nodes as illustrated in the below code but it shows an empty tree. What are the issues in the below trial?

_hookToModel() {
                var models = this.viewer.getVisibleModels();//Get array of all loaded models
                this.rootNodes = models.map(model => {
                    var docNode = model.getDocumentNode();
                    if (!docNode) return null;
                    this.currNode = docNode.findParentGeom2Dor3D();
                    let rootNode = docNode.getRootNode();
                    this.geometries = rootNode.search({ 'type': 'geometry' });
                    return rootNode;
                }).filter(node => node !== null);


                let cache = this.getCache();
                if (cache.ui) {
                    this.ui = cache.ui;
                    cache.ui = null;
                } else {
                    this.ui = new _uiController__WEBPACK_IMPORTED_MODULE_0__.UiController(this.viewer);
                }
                this.ui.createUi(this.currNode, this.options, this.rootNodes); // Here, I'm trying to pass the models' root nodes to adjust the tree and thumbnails to the active models
                this.ui.setChangeModelHandler(this._changeModel.bind(this));
            }
/**
* Creates the UI.
* @param {Autodesk.Viewing.BubbleNode} currNode  - The node loaded into the Viewer
* @param {Object} [options]
* @param {bool} [options.openDocumentBrowserOnLoad=false] - Whether the panel is opened when the extension loads.
* @param {bool} [options.showThumbnails=true] - show or hide thumbnails in the document browser. By default, thumbnails will be shown.
*/
createUi(currNode, options, rootNodes) {

                    this.currNode = currNode;
                    this.rootNodes = rootNodes;
                    this._addToolbarButton();

                    if (!this.panel) {
                        this.panel = new _Panel__WEBPACK_IMPORTED_MODULE_0__.Panel(this.viewer, this.currNode, this.rootNodes);//send rootNodes to the panel constructor
                        this.panel.setChangeModelHandler(this._changeModelFn);
                        this.panel.addVisibilityListener(this._onPanelVisibilityChange);

                        // Show or hide the thumbnails
                        if (options && options.showThumbnails !== undefined) {
                            this.panel.setThumbnailVisibility(options.showThumbnails);
                        }

                        if (options && options.openDocumentBrowserOnLoad) {
                            this.panel.toggleVisibility();
                        }
                    } else {
                        // Some UI change to sync the selection status change triggered by hyperlink
                        this.panel.setCurrentNode(currNode);
                    }

                    this._updateButtonState();
                }

_createTreeView() {
                        if (this.myTree) return;
                        var rootNode = new Autodesk.Viewing.BubbleNode("Aggregated-Models", this.rootNodes);//try to create parent root node but seems incorrect and shows an empty tree
                        //var rootNode = this.currNode.getRootNode();//loads one model only

                        var delegate = (0, _TreeViewDelegate__WEBPACK_IMPORTED_MODULE_1__.createTreeViewDelegate)(rootNode, this);
                        var options = {
                            leafClassName: 'docBrowserLeaf',
                            selectedClassName: 'selected-ex'
                        };

                        var container = this._getTabDiv(TAB_ID_TREE);

                        this.myTree = new Autodesk.Viewing.UI.Tree(
                            delegate,
                            rootNode,
                            container,
                            options
                        );
                    }

Output_Tree



from Customizing Autodesk Forge Viewer Document Browser Extension for Multi-Model Support

PythonVirtualenvOperator gives error: No module named unusual_prefix_***_dag

I'm using airflow 2.5.3 with Kubernetes executor and Python 3.7.

I've tried to make a simple DAG with only one PythonVirtualnvOperator and two context variables ( and ) passed into it.

from datetime import timedelta
from pathlib import Path
import airflow
from airflow import DAG
from airflow.operators.python import PythonOperator, PythonVirtualenvOperator
import pendulum
 
 
dag = DAG(
    default_args={
        'retries': 2,
        'retry_delay': timedelta(minutes=10),
    },
    dag_id='fs_rb_cashflow_test5',
    schedule_interval='0 5 * * 1',
    start_date=pendulum.datetime(2020, 1, 1, tz='UTC'),
    catchup=False,
    tags=['Feature Store', 'RB', 'u_m1ahn'],
    render_template_as_native_obj=True,
)
 
context = {"ts": "", "dag": ""}
op_args = [context, Path(__file__).parent.absolute()]
 
 
def make_foo(*args, **kwargs):
    print("---> making foo!")
    print("make foo(...): args")
    print(args)
    print("make foo(...): kwargs")
    print(kwargs)
 
 
make_foo_task = PythonVirtualenvOperator(
        task_id='make_foo',
        python_callable=make_foo,
        provide_context=True,
        use_dill=True,
        system_site_packages=False,
        op_args=op_args,
        op_kwargs={
          "execution_date_str": '',
        },
        requirements=["dill", "pytz", f"apache-airflow=={airflow.__version__}", "psycopg2-binary >= 2.9, < 3"],
        dag=dag)

Alas, when I'm trying to trigger this DAG, airflow gives me the following error:

[2023-10-23, 13:30:40] {process_utils.py:187} INFO - Traceback (most recent call last):
[2023-10-23, 13:30:40] {process_utils.py:187} INFO -   File "/tmp/venv5ifve2a5/script.py", line 17, in <module>
[2023-10-23, 13:30:40] {process_utils.py:187} INFO -     arg_dict = dill.load(file)
[2023-10-23, 13:30:40] {process_utils.py:187} INFO -   File "/tmp/venv5ifve2a5/lib/python3.7/site-packages/dill/_dill.py", line 287, in load
[2023-10-23, 13:30:40] {process_utils.py:187} INFO -     return Unpickler(file, ignore=ignore, **kwds).load()
[2023-10-23, 13:30:40] {process_utils.py:187} INFO -   File "/tmp/venv5ifve2a5/lib/python3.7/site-packages/dill/_dill.py", line 442, in load
[2023-10-23, 13:30:40] {process_utils.py:187} INFO -     obj = StockUnpickler.load(self)
[2023-10-23, 13:30:40] {process_utils.py:187} INFO -   File "/tmp/venv5ifve2a5/lib/python3.7/site-packages/dill/_dill.py", line 432, in find_class
[2023-10-23, 13:30:40] {process_utils.py:187} INFO -     return StockUnpickler.find_class(self, module, name)
[2023-10-23, 13:30:40] {process_utils.py:187} INFO - ModuleNotFoundError: No module named 'unusual_prefix_4c3a45107010a4223aa054ffc5f7bffc78cce4e7_dag'

Why does it give me this strange error -- and how can it be fixed?



from PythonVirtualenvOperator gives error: No module named unusual_prefix_***_dag

How can I convert a Union type into a tensor type?

I want to convert a tab-delimited text into a 2D tensor object so that I can feed the data into a CNN.

What is the proper way to do this?

I wrote the following:

from typing import List, Union, cast
import tensorflow as tf

CellType = Union[str, float, int, bool]
RowType = List[CellType]

# Mapping Python types to TensorFlow data types
TF_DATA_TYPES = {
    str: tf.string,
    float: tf.float32,
    int: tf.int32,
    bool: tf.bool
}

def convert_string_to_tensorflow_object(data_string):
    # Split the string into lines
    linesStringList1d: List[str] = data_string.strip().split('\n')

    # Split each line into columns
    dataStringList2d: List[List[str]] = []
    for line in linesStringList1d:
        rowItem: List[str] = line.split(' ')
        dataStringList2d.append(rowItem)

    # Convert the data to TensorFlow tensors
    listOfRows: List[RowType] = []
    for rowItem in dataStringList2d:
        oneRow: RowType = []
        for stringItem in rowItem:
            oneRow.append(cast(CellType, stringItem))
        listOfRows.append(oneRow)

    # Get the TensorFlow data type based on the Python type of CellType
    tf_data_type = TF_DATA_TYPES[type(CellType)]

    listOfRows = tf.constant(listOfRows, dtype=tf_data_type)

    # Create a TensorFlow dataset
    return listOfRows

if __name__ == "__main__":
    # Example usage
    data_string: str = """
    1 ASN C  7.042   9.118  0.000 1 1 1 1  1  0
    2 LEU H  5.781   5.488  7.470 0 0 0 0  1  0
    3 THR H  5.399   5.166  6.452 0 0 0 0  0  0
    4 GLU H  5.373   4.852  6.069 0 0 0 0  1  0
    5 LEU H  5.423   5.164  6.197 0 0 0 0  2  0
    """

    tensorflow_dataset = convert_string_to_tensorflow_object(data_string)

    print(tensorflow_dataset)

Output:

C:\Users\pc\AppData\Local\Programs\Python\Python311\python.exe C:/git/heca_v2~~2/src/cnn_lib/convert_string_to_tensorflow_object.py
Traceback (most recent call last):
  File "C:\git\heca_v2~~2\src\cnn_lib\convert_string_to_tensorflow_object.py", line 51, in <module>
    tensorflow_dataset = convert_string_to_tensorflow_object(data_string)
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\git\heca_v2~~2\src\cnn_lib\convert_string_to_tensorflow_object.py", line 34, in convert_string_to_tensorflow_object
    tf_data_type = TF_DATA_TYPES[type(CellType)]
                   ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^
KeyError: <class 'typing._UnionGenericAlias'>

Process finished with exit code 1

Can I resolve the error?



from How can I convert a Union type into a tensor type?

How to query arxiv daily based on keywords and write the results in a Google doc?

I want to find papers that are published in the computer science section of arxiv every day based on a list of keywords and write their titles and arxiv link to my Google doc (i.e., append to the end of what's already written):

For example, the Google doc can look as follows:

  1. Test-time Augmentation for Factual Probing
  2. Controlled Decoding from Language Models

And so on...

My list of search keywords:

arxiv_keywords = ['machine learning', 'llm', 'potato']

The titles should not be case sensitive and should contain the keywords. For example, the following made up titles should be returned Machine learninG is a mystery, LLM-based models are weird, potatoes are tasty when turned into fries

My Google doc is located in my_drive/Research/my_google_doc_name

I found this SO question that asks about querying arxiv for a specific year based on one keyword, but there are several different things in my request and theirs which complicates things to me:

  1. I only need to query the computer science section. Based on this SO question there seems to be a difference in returned results when querying from the general arxiv website and a more advance search.
  2. I need to automatically query it once a day, so I'm not sure how to automatically update the dates.
  3. I'm not sure how to modify their script to handle multiple keywords
  4. I'm not sure how to append the results to a Google doc, which requires a separate query from my understanding, from here and here and probably to enter my password somehow.

I found that I can automatically run a python using cron following this link.

Overall there seems to be a lot going on which confuses me and I'm not entirely sure how to handle all the parts.



from How to query arxiv daily based on keywords and write the results in a Google doc?

Sunday 29 October 2023

Type hints for matplotlib Axes3D

I have a function that takes a matplotlib 3D axis:

from mpl_toolkits.mplot3d.axes3d import Axes3D


def plot(ax: Axes3D):
    pass

I know how to add a type hint for a 2D axis. Just ax: plt.Axes works perfectly. However, with this mpl_toolkits.mplot3d.axes3d.Axes3D, pylance complains that:

Expected type expression but received (...) -> Unknown Pylance

Sadly, there is no plt.Axes3D equivalent to plt.Axes.

I verified that mpl_toolkits.mplot3d.axes3d.Axes3D works as expected. The following code does not give an error:

fig = plt.figure()
Axes3D(fig)

I went to the definition of Axes3D and found that the problem is the @_docstring.interpd decorator. If I remove it, the pylance warning goes away. The @_api.define_aliases seems to be fine.

I am using python 3.10.12 and matplotlib 3.8.0

Edit: The motivation for the type being Axes3D and not Axes is that otherwise pylance shows a diagnostic that Axes has no method set_zlabel(), etc.



from Type hints for matplotlib Axes3D

plotting subplots with a shared slider in python

I am trying to plot 3 subplots (separate plots directly one under the other) that share the same x range data with a slider that controls all of them on the bottom. I have managed to do that with one plot but I do not know how to add the other 2 subplots. I am sharing the code with a small part of the dataseries.

from plotly.subplots import make_subplots
import plotly.express as px
import plotly.graph_objects as go
x=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # pandas series, string
y1=[90, 92, 89, 82, 82, 78, 76, 82, 85, 88] # pandas series, numpy int64
y2=[20, 21, 19, 20, 18, 17, 14, 16, 18, 23] # pandas series, numpy int64
y3=[40, 42, 41, 42, 44, 45, 47, 49, 45, 46] # pandas series, numpy int64


fig1 = make_subplots(rows=3, cols=1, shared_xaxes=True, vertical_spacing=0.1)
fig2 = px.scatter(x=x,y=y1,labels = dict(x = "time",y = "var"))
fig1 = go.Figure(data = fig2.data) # this is defined like that so I can add a second dataset on the same plot if needed, but I need y2,y3 on a separate plot
fig1.update_layout(yaxis_title='var',xaxis_title='time')
fig1.update_xaxes(rangeslider_visible=True)
fig1.show()

Thank you!



from plotting subplots with a shared slider in python

Saturday 28 October 2023

Runtime checking for extra keys in TypedDict

This answer tells how to validated a TypedDict at runtime, but it won't catch the case of an extra key being present.

For example, this doesn't throw. How can I make it throw?

from typing import Any, TypeVar

from pydantic import TypeAdapter
from typing_extensions import TypedDict

T = TypeVar('T')

def runtime_check_typed_dict(TypedDictClass: type[T], object_: Any) -> T:
    TypedDictValidator = TypeAdapter(TypedDictClass)
    return TypedDictValidator.validate_python(object_, strict=True)


class MyThing(TypedDict):
    key: str

obj_: Any = {'key': 'stuff', 'extra': 'this should throw!'}

runtime_check_typed_dict(MyThing, obj_)


from Runtime checking for extra keys in TypedDict

Server returns Internal Server Error / Dangerous Request.Form value

MVC project, been around a long time, massive monolith. I hope I'm missing something here.

I've a form which gets submitted over ajax. It gets to the server, gets stored away, and I go to return an object to the client (basically status & a destination URL).

If there's anything in the form inputs containing illegal characters (html-ish) then it gives me the error about fearful data.

  • I've tried putting [AllowHtml] on every single text field of the view model (won't be long-term).
  • I've added [ValidateInput(false)] on the controller method
  • Removed all of the alerts (since it is saving the data).

The stupid thing is that it saves the data. The "dangerous" data makes it to the server and database and back out to the controller method. I can't seem to catch this, - I've debugged it to where I ship it from the controller, and then in-browser I get the error.

var form = $(this).parents('form:first').serializeObject();

$.ajax({
    type: "POST",
    url: '@Url.Action("EditAjax", "Item", new {area = "ProjectSupport"})',
    dataType: "json",
    data: form,
    ignoreErrors: true,
    success: function(result) {
    var formData = new FormData($('#editItem')[0]);
    if (result.Errors) {
        //alert(result.Errors);
        $('form').find('a.submit,#saveFormButton1,#saveFormButton2').removeClass('btn-inverse').addClass('btn-primary');
        submitted = false;
    } else {
        formData.append("supportObjectTypeName", result.SupportObjectTypeName);
        formData.append("supportObjectLocalizedID", result.SupportObjectLocalizedID);

        url = result.Url;

        $.ajax({
        type: "POST",
        url: '@Url.Action("UploadAttachments", "Item", new {area = "ProjectSupport"})',
        dataType: "json",
        mimeType: "multipart/form-data",
        contentType: false,
        cache: false,
        processData: false,
        data: formData,
        ignoreErrors: true,
        success: function() {
            window.location.href = url;
        },
        error: function (xhr, ajaxOptions, thrownError) {
            //alert(thrownError);
            //$('form').find('a.submit,#saveFormButton1,#saveFormButton2').removeClass('btn-inverse').addClass('btn-primary');
            //submitted = false;
            window.location.href = url;
        }
        });
    }
    },
    error: function(xhr, ajaxOptions, thrownError) {
    alert(thrownError);
    alert(xhr.responseText);
    var fullErrorText = [];
    $.each(xhr.responseJSON, function() {
        fullErrorText.push(this.errors.join());
    });
    alert(fullErrorText.join());
    $('form').find('a.submit,#saveFormButton1,#saveFormButton2').removeClass('btn-inverse').addClass('btn-primary');
    submitted = false;
    }
});

I've used AllowHtml previously and it worked just fine - slap it onto the model and you're good. I've just spent a few hours on this issue, though, and I hope somebody out there's brighter than me on this (I'm certain).

Any thoughts, please?

TLDR:

  • I've tried putting [AllowHtml] on every single text field of the view model.
  • I've added [ValidateInput(false)] on the controller method
  • Removed all of the alerts (since it is saving the data).

I still get an Ajax error about dangerous values.



from Server returns Internal Server Error / Dangerous Request.Form value

same cookie notice reappears on subdomain despite it was accepted

I'm encountering an issue where, despite accepting cookies on the primary localhost, the cookie notification persists when I move to a subdomain such as requester.localhost. Same with if I accept cookies on a subdomain, the notification shouldn't show up again on the primary localhost (without a subdomain), yet it does.

Here is the script:

export default {
  data() {
    return {
      snackbar: true,
      timeout: -1,
    };
  },
  computed: {
    cookieAgreement() {
      return this.$cookies.get("cookie_agreement") === "1";
    },
  },
  created() {
    if (this.cookieAgreement) {
      this.snackbar = false;
    }
  },
  methods: {
    acceptCookies() {
      this.$cookies.set("cookie_agreement", "1", {
        path: "/",               // Set the path to root to make it accessible across the entire domain
        domain: ".localhost" // Set the domain to make it accessible across subdomains
      });
      this.snackbar = false;
    },
  },
};


from same cookie notice reappears on subdomain despite it was accepted

How to train a network with two or more layers

I am implementing a neural network from scratch using python. I have a Neuron class, layer class and network class.

I have managed to train and use a network with 1 layer, 1 Neuron and 3 inputs.

I now want to try using 2 or more layers, both with an arbitrary number of Neurons. My problem is, how would I now change the 'train' function to train a network like this?

At present, if the layer is 0 then it will input the network inputs into the neurons. If the layer is above 0, then it will input the outputs from the previous layer.

But what do I do next?

I have used the code below:


import numpy as np
from numpy import exp, random
import math

from sklearn.datasets import make_blobs
import matplotlib.pyplot as plt

np.random.seed(1)

class Neuron:

    def __init__(self, weights, bias):

        self.weights = weights
        self.bias = bias

    def sigmoid(self, x):

        output = 1/(1+exp(-x))

        return output

    def compute(self, inputs):

        self.output = self.sigmoid(np.dot(inputs, self.weights) + self.bias)

        return self.output

class Layer: 

    def __init__(self, numberOfNeurons, numberOfInputs):

        self.neurons = []
        self.outputs = []
        self.numberOfNeurons = numberOfNeurons
        self.numberOfInputs = numberOfInputs

        self.initialiseWeightsAndBiases()

        for i in range(0,numberOfNeurons):

            self.neurons.append(Neuron(self.weights, self.biases))

    def initialiseWeightsAndBiases(self):

        self.weights = 2 * random.random((self.numberOfInputs, self.numberOfNeurons)) - 1

        self.biases = 2 * random.random((1, self.numberOfNeurons)) - 1

    
    def forward(self, inputs):

        self.outputs = np.array([])

        for i in self.neurons:

            self.outputs = np.append(self.outputs, i.compute(inputs))

class NeuralNetwork:

    def __init__(self, layers):

        self.layers = layers

    def forwardPass(self, inputs):

        for i in range(0,len(layers)):

            if i == 0:

                layers[i].forward(inputs)   

            else:
                
                layers[i].forward(layers[i-1].outputs)

        return layers[-1].outputs

    def calculateError(self, predictedOutputs, trueOutputs):

        error = (trueOutputs - predictedOutputs) * predictedOutputs * (1 - predictedOutputs)

        return error

    def trainNetwork(self, trainingDataInputs, trainingDataOutputs, numberOfIterations):

        #initialise the best weights with random values

        for y in range(0, numberOfIterations):

            predictedOutputs = self.forwardPass(trainingDataInputs)

            error = self.calculateError(predictedOutputs, trainingDataOutputs)

            for i in layers[0].neurons:             

                i.weights += np.dot(trainingDataInputs.T, error.T)


    def visualiseNetwork(self):

        pass


#Layer(numberOfNeurons, numberOfInputs)

inputLayer = Layer( 1, 3)

layers = [inputLayer]

network1 = NeuralNetwork(layers)

inputTrainingData = np.array([[0, 0, 1], [1, 1, 1], [1, 0, 1], [0, 1, 1]])
outputTrainingData = [[0, 1, 1, 0]]

network1.trainNetwork(inputTrainingData, outputTrainingData, 10000)

outputs = network1.forwardPass([[0,1,1]])

print(outputs)



from How to train a network with two or more layers

Friday 27 October 2023

Why the response is 400 when using the same headers with python requests?

I am trying to scrape this website https://segari.id/c/unggas . I tried using requests

url="https://api-v2.segari.id/v1.1/products/price?agentId=311&tagIds=838&size=999&page=0&paginationType=slice&totalPriceBefore=0&deliveryDate=2023-10-26&deliveryServiceType=NEXT_DAY_DELIVERY&pageOrigin=clp"

headers={
'Referer':'https://segari.id/',
'Sec-Ch-Ua':'"Chromium";v="118", "Google Chrome";v="118", "Not=A?Brand";v="99"',
'Sec-Ch-Ua-Mobile':'?0',
'Sec-Ch-Ua-Platform': '"Windows"',
'Sec-Fetch-Dest':'empty',
'Sec-Fetch-Mode':'cors',
'Sec-Fetch-Site':'same-site',
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36',
'X-Segari-Pathname':'/c/unggas',
'X-Segari-Platform':'web',
'X-Segari-Sec': 'v1:MjAyMy0xMC0yNVQxMzoxODozNS40OTQwMDArMDc6MDA=:96a10a8bbb9f47b8cfb1db7bd3ad483632ede18b0631de80e9720e30d4928f07'
}
 
response=requests.get(url,headers=headers)

response returns <Response [400]>

response.content returns message":"04 - Signature Mismatch","code":"TODO","data":null,"errors":null

I tried opening the api url, and returns error. I checked that X-segari-sec is the only thing missing in the header. So I deduced that's the only missing thing. I also noticed a request to 'https://ift.tt/EFbgKkC'. I tried the X-segari-sec by emulating the javascript. It's the same.

here's the python ver for it

def get_X_segar_sec(hash_key, timestamp):
  
  hash_key = "wCL8iD2xgQ5EdLrVBEYvAP3nRxX3q6TSNjIlISOx7PTphdhA78M2cjlN5RISAHwn"

  base64_encoded_timestamp = base64.b64encode(timestamp.encode()).decode()
  hash_string = f"{hash_key}{base64_encoded_timestamp}"
  hash_value = hashlib.sha256(hash_string.encode()).hexdigest()

  return f"v1:{base64_encoded_timestamp}:{hash_value}"

The response is 400 and returns a mismatch text from content. Can someone help me to get 200? Why is this happening? I tried using selenium too and no data is being loaded after waiting.

Thank you



from Why the response is 400 when using the same headers with python requests?

No module named 'pydantic_core._pydantic_core' server for python serverless on AWS lambda

I read these StackOverflow questions: 1 2 that cover the same issue, as well as this Github thread. I still feel like I am missing something.

My Issue

I have a function that is deployed to AWS Lambda/API gateway using the serverless framework (python 3.10 runtime). I am also using the serverless-python-requirements plugin. My function uses the pydantic module. I have the following in my requirements.txt (excerpt):

pydantic==2.4.2
pydantic_core==2.10.1

I am not using Flask or FastAPI. My function works just fine when invoked locally (serverless invoke local -f my_function).

After deploying and invoking the deployed function with the same command (other than removing local), I get this error:

Running "serverless" from node_modules
{
    "errorMessage": "No module named 'pydantic_core._pydantic_core'",
    "errorType": "ModuleNotFoundError",
    "requestId": "fd4eb321-5f81-42a2-9880-ea6a76a626d5",
    "stackTrace": [
        "  File \"/opt/python/serverless_aws_lambda_sdk/instrument/__init__.py\", line 598, in stub\n    return self._handler(user_handler, event, context)\n",
        "  File \"/opt/python/serverless_aws_lambda_sdk/instrument/__init__.py\", line 580, in _handler\n    result = user_handler(event, context)\n",
        "  File \"/var/task/serverless_sdk/__init__.py\", line 144, in wrapped_handler\n    return user_handler(event, context)\n",
        "  File \"/var/task/s_post_rental_app_from_airtable.py\", line 25, in error_handler\n    raise e\n",
        "  File \"/var/task/s_post_rental_app_from_airtable.py\", line 20, in <module>\n    user_handler = serverless_sdk.get_user_handler('functions.post_rental_app_from_airtable.handler.lambda_handler')\n",
        "  File \"/var/task/serverless_sdk/__init__.py\", line 56, in get_user_handler\n    user_module = import_module(user_module_name)\n",
        "  File \"/var/lang/lib/python3.10/importlib/__init__.py\", line 126, in import_module\n    return _bootstrap._gcd_import(name[level:], package, level)\n",
        "  File \"<frozen importlib._bootstrap>\", line 1050, in _gcd_import\n",
        "  File \"<frozen importlib._bootstrap>\", line 1027, in _find_and_load\n",
        "  File \"<frozen importlib._bootstrap>\", line 1006, in _find_and_load_unlocked\n",
        "  File \"<frozen importlib._bootstrap>\", line 688, in _load_unlocked\n",
        "  File \"<frozen importlib._bootstrap_external>\", line 883, in exec_module\n",
        "  File \"<frozen importlib._bootstrap>\", line 241, in _call_with_frames_removed\n",
        "  File \"/var/task/functions/post_rental_app_from_airtable/handler.py\", line 9, in <module>\n    from pydantic import BaseModel, ValidationError\n",
        "  File \"/var/task/pydantic/__init__.py\", line 3, in <module>\n    import pydantic_core\n",
        "  File \"/var/task/pydantic_core/__init__.py\", line 6, in <module>\n    from ._pydantic_core import (\n"
    ]
}
Environment: darwin, node 20.2.0, framework 3.36.0 (local) 3.35.2v (global), plugin 7.1.0, SDK 4.4.0
Credentials: Local, "default" profile
Docs:        docs.serverless.com
Support:     forum.serverless.com
Bugs:        github.com/serverless/serverless/issues

What I Tried

I read in the threads above that this problem could come about as a result of ISA mismatches in pydantic_core. So I tried the following:

Specifying the ISA for all functions in serverless.yml (provider -> architecture -> 'arm64' and 'x86_64')

provider:
  name: aws
  # latest supported python by lambda + serverless as of 2023-10-23
  runtime: python3.10
  # NOTE: arm64 may offer better price/performance
  architecture: 'arm64'

Specifying that pip should build in a docker in serverless.yml, that it should use an arm64 image, and pass the platform information to pip via the dockerRunCmdExtraArgs:

custom:
  pythonRequirements:
    # this is necessary to avoid cross-platform build issues
    dockerizePip: true
    # explicitly pass the arm64 platform to the docker build
    dockerImage: public.ecr.aws/sam/build-python3.10:latest-arm64
    # explicitly tell pip to fetch the arm64 version of the package
    dockerRunCmdExtraArgs: [ '--platform', 'linux/arm64/v8' ]

I'm not sure what else to try.



from No module named 'pydantic_core._pydantic_core' server for python serverless on AWS lambda

How can I get bookmarked hash links to work with a jQuery change function?

What's the simplest way with this jQuery change function to enable bookmarked hash links to work, i.e to be able to use example.com/#theme1 or example.com/#theme2 links?

The jQuery #theme-selector changes the hash and shows/hides the relevant divs with the class, i.e. .theme1 or .theme2, but when going to example.com/#theme1 from a bookmarked link, all divs are shown.

In addition, if I refresh example.com/#theme1, the hash remains #theme1, but all divs are shown.

Some other questions on SO deal with bookmarkable hashes, but they're many years old. And some libraries that appear useful are ten years old, too, like https://github.com/asual/jquery-address

Unfortunately, running the snippet here won't show browser hash changes, nor will JSFiddle.


Update 10/26/23

@SKJ's answer works in an html file opened with a browser, and shows bookmarkable links and retains the hash on refresh. But for some reason, it doesn't work on a live server. On the live server, there are no console errors; the dropdown #theme-selector works and filters the divs, but bookmarkable links don't work, and on a page reload, the URL hash is ignored and the selector resets to all-themes and all divs show.

But.... I have some older plain Javascript where these filters work and also bookmarkable links work; but I'm trying to simplify and replace it with jQuery (and I've also changed the html markup), hence this SO question.

So what is it in this plain Javascript https://pastebin.com/JUDizTXA that works for bookmarkable links and refresh-proof URL hashes? What can be adapted to this new jQuery to enable bookmarkable links refresh-proof URL hashes?

  jQuery(document).ready(function(jQuery){
    
        jQuery("#theme-selector").change(function () {
    
        var urlhash = document.getElementById('theme-selector').value;
    
        window.location.hash = urlhash;
    
    });
    
    
    jQuery("#theme-selector").change(function () {
    
    themeclass = window.location.hash.substr(1);
    
    jQuery('.blockcontent').hide();
    jQuery('.' +themeclass).show();
    jQuery('.theme-header').hide();
    jQuery('.' +themeclass).show();
    
        });
    
    });
 .blockcontent {
    display:inline-block;
    width:150px;
    border:1px solid #acacac;
    }
    
    .theme-header {
    display:none;
    }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

 <label for="theme-selector">Filter by Theme:</label>
    
    <select name="theme-selector" id="theme-selector" value="">
    
    <option value="all-themes">All Themes</option>
    <option value="theme1">Theme 1</option>
    <option value="theme2">Theme 2</option>
    <option value="theme3">Theme 3</option>
    
    </select>
    
    <br>
    
    <div id="theme-description-container">
    <div class="theme1 theme-header">Theme 1 Description</div>
    <div class="theme2 theme-header">Theme 2 Description</div>
    <div class="theme3 theme-header">Theme 3 Description</div>
    </div>
   
    <br>
    
    <div class="blockcontent all-themes theme1">
    theme1<br><br>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do 
    </div>
    
    <div class="blockcontent all-themes theme2">
    theme2<br><br>sed do eiusmod tempor incididunt sed do eiusmod eiusmod
    </div>
    
    <div class="blockcontent all-themes theme3 theme2">
    theme2 and theme3<br><br>consectetur adipiscing elit,consect adipiscing elit, 
    </div>
    
    <div class="blockcontent all-themes theme3">
    theme3<br><br>consectetur adipiscing elit,consect etur adipiscing elit, 
    </div>


from How can I get bookmarked hash links to work with a jQuery change function?

navbar with sticky glitches when submenu opens

I have created a navbar with sticky using HTML, CSS, and JavaScript. A submenu is opened when hovered over the nav-list elements. I have included the code for all below. The problem I am facing is, that if sticky is active and I open my submenu, it causes a glitch for a macro-second with the height of my navbar. Further, the frequency of the glitch is arbitrary. I have attached an image of my glitch. What is causing this and how can I stop it?

Image of distorted height navbar

function scrollDetect() {
  const navbar = document.getElementById("navbar");
  let lastScroll = 0;

  window.addEventListener("scroll", () => {
    const currentScroll = document.documentElement.scrollTop || document.body.scrollTop;

    if (currentScroll > 0 && lastScroll <= currentScroll) {
      navbar.classList.remove("sticky");
    } else {
      navbar.classList.add("sticky");
    }

    lastScroll = currentScroll;

    if (currentScroll < 100) {
      navbar.classList.remove("sticky");
    }
  });
}

scrollDetect();
  .main-navbar {
  position: absolute;
  display: flex;
  background-color: rgb(255, 255, 255);
  width: 100%;
  top: 3.7rem;
  height: 4.5rem;
  align-items: center;
  justify-content: space-between;
}

.nav-list {
  display: flex;
  margin-left: 5.3rem;
  align-items: center;
  margin-top: 1rem;
}

.nav-list a {
  position: relative;
}

.nav-list>li>a {
  font-size: 1.07rem;
  font-weight: 500;
  padding: 10px 15px 2rem 15px;
}

.nav-list>li>a::after {
  content: "";
  position: absolute;
  background-color: rgb(0, 0, 0);
  height: 3.9px;
  width: 0;
  left: 0;
  bottom: 14px;
  border-radius: 2px;
}

.sub-menu {
  position: absolute;
  background-color: rgb(255, 255, 255);
  visibility: hidden;
  margin-top: 1.1rem;
  left: 0;
  width: 100%;
  z-index: 100;
  border-bottom-left-radius: 7px;
  border-bottom-right-radius: 7px;
}

.nav-list>li:hover .sub-menu {
  visibility: visible;
}

.sub-menu a {
  position: relative;
  color: black;
  font-size: 1rem;
  font-weight: 200;
  padding: 3rem 40px 0 40px;
  display: block;
  width: 100%;
}

.sub-menu a:hover {
  color: #7e7978;
}

.nav-list>li:hover>a::after {
  width: 100%;
}

.sticky {
  position: fixed;
  top: 0;
  width: 100%;
  animation: faceInEffect 0.3s;
  -webkit-animation: faceInEffect 0.3s;
  border-bottom: none;
}

@keyframes faceInEffect {
  from {
    top: -5rem;
  }
  to {
    top: 0rem;
  }
}

.logo a svg {
  margin-left: 12%;
  width: 300px;
  transition: width 0.2s ease;
}

.logo a svg:hover {
  opacity: 0.7;
}

.rightnav {
  position: relative;
  right: 2.9%;
  white-space: nowrap;
  display: flex;
  align-items: center;
}

.content {
  position: relative;
  top: 100rem;
<div class="main-navbar" id="navbar">
  <div class="logo">
    <a href="">
      <svg>
            
          </svg>
    </a>
  </div>
  <div>
    <nav>
      <ul class="nav-list">
        <li>
          <a href="">NEW</a>
        </li>
        <li class="nav-item" data-menu="submenu1">
          <a href="">Men</a>
          <ul class="sub-menu" id="submenu1">
            <li><a href="">shirts</a> </li>
            <li><a href="#">Shorts</a></li>
          </ul>
        </li>
        <li class="nav-item" data-menu="submenu2">
          <a href="">Women</a>
          <ul class="sub-menu" id="submenu2">
            <li><a href="#">shirts</a> </li>
            <li><a href="#">Shorts</a></li>
            <li><a href="#">Accessories</a></li>
          </ul>
        </li>
      </ul>
    </nav>
  </div>
  <div class="rightnav">
    <div class="search-bar">
      <form>
        <div class="clickclass">
          <input class=" search bt-outline" type="text" placeholder="Search">
          <button class="bt-outline" id="clear-button">Clear</button>
          <a class="search-button" type="submit">
          </a>
        </div>
      </form>
    </div>
  </div>
</div>


<div class="content">
  hello
</div>


from navbar with sticky glitches when submenu opens

Thursday 26 October 2023

Writing to the clipboard onChange of a dropdown element in Safari

I'm trying to write text to the clipboard when a user selects an option from a dropdown menu. The following works for all browsers except Safari:

<select onChange="writeTextToClipboard(this.value);">
  <option value="Text I want to copy">Copy to clipboard</option>
</select>

function writeTextToClipboard(value) {
  navigator.clipboard.writeText(value);
}

I understand that Safari expects a Promise to be passed to the write function of the clipboard object, so following this blog post, I modified my code:

async function writeTextToClipboard(value) {
  // Safari, Chrome
  if(typeof ClipboardItem && navigator.clipboard.write) {
    const type = 'text/plain'
    const blob = new Blob([value], {type})
    const cbi = new ClipboardItem({[type]: blob})
    await navigator.clipboard.write([cbi])
    console.log(`Copied: ${value}`)
  }
  // Firefox
  else {
    navigator.clipboard.writeText(value);
  }
}

This still throws a NotAllowedError: The request is not allowed by the user agent or the platform in the current context, possibly because the user denied permission. on Safari and also breaks on Chrome with NotAllowedError: Invalid Blob types. Am I missing something?

I'm on Safari 17.0

UPDATES:

  • I fixed the issue on Chrome, but it still doesn't work on Safari. I've created a fiddle with a minimal example.
  • I also realized that my example works when it's called in the onClick handler of a button instead of onChange of a select element. I clarified my use-case.
  • The only solution I can think of at the moment is to re-build the dropdown menu with something other than a select element and use the onClick event handler on that, but I'd like to avoid that if possible. Using something other than select for a dropdown is likely creating more challenges in the future to keep it working nicely across browsers and different screen sizes.
  • This has been asked before, but the solutions mentioned in that post are using execCommand() which is deprecated and doesn't work either.


from Writing to the clipboard onChange of a dropdown element in Safari

how to automate my sources? (python and SqlServer)

In my .pbix, my data consists of 4 queries: enter image description here

One query is a Python.Execute the other 3 are Sql.Database.

  • The SqlServer is on-prem and I already have a gateway installed there…
  • The python one is a call to an API to bring my fact table

I want to publish it and have it updated automatically every Sunday night. What’s the standard way of achieving this?

I understand I need a PERSONAL gateway for the python, but, where do I install it?

My laptop doesn’t seem the right place because… When the update shall run on Sunday, my laptop is going to be off.

(we have a P1 Premium capacity)



from how to automate my sources? (python and SqlServer)

Postmessage function is not working in mobile but working in desktop

I am trying to create the widget the will have one button, when someone click on the button a popup comes. That popups contain the button too, that will do task.

Here is the popup code-

<div class="modal" id="myModal">
        <div class="modal-content">
            <span class="close-btn" id="closeModalBtn">&times;</span>
            <iframe id="modelIframe" src="base_url/modelviewer.html" style="width: 280px; height: 200px;" allow="xr-spatial-tracking" frameborder="0"></iframe>
            <p>View this product in 3D</p>
            <button id="activateAR" style="padding: 10px 10px;border: none; ">See in 3D</button> 
        </div>
    </div>

So when this poup up is visible and user click on the button (with id "activateAR"), I call the following function-

function activateARFunction() {
    console.log("data",document);
    const iframe = document.getElementById('modelIframe');
    iframe.contentWindow.postMessage('activateAR', '*');     
}


document.getElementById('activateAR').addEventListener('click', activateARFunction);

and modelviewer.html (where I am receiving the message)-

<script>
window.addEventListener('message', (event) => {
    console.log("button without data ","received");
    if (event.data === 'activateAR') {
        console.log("button ","received");
         
    }
});
</script>

When I run this code in the desktop, my modelviewer.html receive the postmessage data i.e.

console.log("button without data ","received");

However, when I run it in mobile, it does not print anything. No error in console too.



from Postmessage function is not working in mobile but working in desktop

Wednesday 25 October 2023

usage of tf.gather to index lists containing non-Tensor types

Consider the following code. I'd like to know how I can gather non-Tensor types from a list.

import tensorflow as tf

class Point(tf.experimental.ExtensionType):
    xx: tf.Tensor
    def __init__(self,xx):
        self.xx = xx
        super().__init__()

list1 = [ 1, 2, 3, 4] 
list2 = [ Point(1), Point(2), Point(3), Point(4) ]

# this works
out1 = tf.gather(list1,[0,2])
print('First gather ',out1)

# this throws: ValueError: Attempt to convert a value (Point(xx=<tf.Tensor:
# shape=(), dtype=int32, numpy=1>)) with an unsupported type
# (<class '__main__.Point'>) to a Tensor.

out2 = tf.gather(list2,[0,2])
print('Second gather ',out2)


from usage of tf.gather to index lists containing non-Tensor types

Video not playing from typescript in owl carosal

I have updated my previous question. I am trying to play a video in Owl Carousal using on button click but it's not working properly. Some of the videos are playing but most of the time it's not. Without carousal normally for a single video, it's working fine, is any CSS related to owl carousal creating an issue? I am getting proper video elements in the console too. The video seems to be playing and can hear the video sound it's not playing video seems to video screen stuck

Here is my HTML :

I am trying to create a play button by clicking with the function playVideo()

 <div class="big_img mediaCursor" *ngIf="post.mediaData && post.mediaData.length > 1 && post.post_type == 'is_post'" >
        <owl-carousel-o [options]="customOptions1"  #owlCarousel (change)="handleSlide($event)">
          <ng-container *ngFor="let img of post.mediaData"  >
            <ng-template class="slide"  carouselSlide>
              <button  *ngIf="img.media_type === 'video'" class="btn-play-video" id="video_play_" (click)="playVideo(img.id)">
                <i class="ti ti-control-play"></i>Play
              </button>
              <div class="mb-0 testtts" id="parentVideo_" >
                <img *ngIf="img.media_type === 'image'" (click)="Openpost(post.post_id)" [src]="img.file_path" alt="Icelandic"
                  class="big_img img-fluid cursor"  />
                  <video id="video_" width="640"  controls preload="none"    poster=""
                    *ngIf="img.media_type === 'video'" class="video-js vjs-default-skin cursor"
                    >
                    <source src="#t=1" type="video/mp4" />
                  </video>
              </div>
            </ng-template>
          </ng-container>
        </owl-carousel-o>
      </div>

Here is my TS for the function playVideo() :

  playVideo(eleId){
   let button:HTMLButtonElement = document.querySelector('#video_play_' + eleId)
   let video:HTMLVideoElement =  document.querySelector('#video_' + eleId)
   let parentDiv:HTMLDivElement = document.querySelector('#parentVideo_' + eleId)
   let ddVHeight = parentDiv.offsetHeight
   video.load();
  video.onloadeddata = (event) => {
    video.play()
   this.renderer.setStyle(parentDiv, "height", `${ddVHeight}px`);
    this.renderer.listen(video, 'playing', () => {
     this.renderer.addClass(button, 'hide');
   })
   this.renderer.listen(video, 'pause', () => {
     this.renderer.removeClass(button, 'hide');
   })
    
};

  }

Any suggestion or hint will be appreciated. Thanks.



from Video not playing from typescript in owl carosal

Form a "chain" of relationship through SQLAlchemy

Say I have a database where I want to model a many-to-many relationship between users and the websites they have access to.

I have the tables users and websites, and I create a third users_have_sites table with two foreign keys to modelize this many-to-many relationship.

With SQLAlchemy, this looks something like this:

from sqlalchemy.ext.declarative import DeclarativeBase
from sqlalchemy import Column, Integer, Foreignkey

Base = DeclarativeBase()

class User(Base):
     __table_name__ = "users"
     id = Column(Integer, primary_key=True)
     link = relationship("UserHasWebsite", back_populates="user")
     ... # Other columns
     
class Website(Base):
    ___table_name__ = "websites"
    id = Column(Integer, primary_key=True)
    link = relationship("UserHasWebsite", back_populates="website")
    ...

class UserHasWebsite(Base):
    __table_name__ = "users_have_websites"
    id = Column(Integer, primary_key=True)
    user = relationship("User", back_populates="link" )
    website = relationship("Website", back_populates="link")

I can get a list of Website instances linked to an user by calling [link.website for website in user.link], but I was wondering if there was the possibility to pass this "chained" relationship in the definition of the class, so that I could directly call an attribute user.websites



from Form a "chain" of relationship through SQLAlchemy

Rendering taking too much time in reactjs

I am iterating over just 100 documents on page and using a wrapper component for some conditional things.

return (
  <>
    {orders.map(({ order, ...rest }, index) => {
      return (
        <div className="booking-details" key={order.id}>
          <Grid container className="bg-dark" mt={2} alignItems="center">
            <Grid md={6} className="text-left" item>
              <Checkbox id="id1" checked={ordersToExport?.map(({ order }) => order.id).includes(order.id)} className="white" onChange={() => onClickCheckbox({ order, ...rest })} />
            </Grid>
            <Grid md={6} className="text-right" item>
              <ButtonGroup variant="text" aria-label="button group" className="white-btn-group">
                  <>
                    <ActionsAccess btnType="Accept" status={order.status}>
                      <Tooltip title="Accept" placement="top" arrow>
                        <Button size="large" onClick={() => updateOrder(order, 2)}>
                          <CheckIcon />
                        </Button>
                      </Tooltip>
                    </ActionsAccess>
                    <ActionsAccess btnType={"Accept and open document"} status={order.status}>
                      <Tooltip title="Accept and open document" placement="top" arrow>
                        <Button size="large" onClick={() => onClickAcceptOpen(order, 2)}>
                          <TaskIcon />
                        </Button>
                      </Tooltip>
                    </ActionsAccess>
                    <ActionsAccess btnType={"Accept and add note"} status={order.status}>
                      <Tooltip title="Accept and add note" placement="top" arrow>
                        <Button size="large" onClick={() => handleChange(`panel${`${index}-note`}`)}>
                          <SmsIcon />
                        </Button>
                      </Tooltip>
                    </ActionsAccess>
                    <ActionsAccess btnType={"B-Scan"} status={order.status}>
                      <Tooltip title="B-Scan" placement="top" arrow>
                        <Button size="large" onClick={() => handleChange(`panel${`${index}-bscan`}`)}>
                          <SystemUpdateAlt />
                        </Button>
                      </Tooltip>
                    </ActionsAccess>
                  </>
                <ActionsAccess btnType={"View document"} status={order.status}>
                  <Tooltip title="View document" placement="top" arrow>
                    <Button size="large" onClick={() => window.open(`/mybooking/overview-today/view-pdf?id=${order.id}`)}>
                      <DescriptionIcon />
                    </Button>
                  </Tooltip>
                </ActionsAccess>
                <ActionsAccess btnType={"Delete order"} status={order.status}>
                  <Tooltip title="Delete order" placement="top" arrow onClick={() => handleChange(`panel${`${index}-delete`}`)}>
                    <Button size="large">
                      <DeleteIcon />
                    </Button>
                  </Tooltip>
                </ActionsAccess>
                  <ActionsAccess btnType={"Reject order"} status={order.status}>
                    <Tooltip title="Reject order" placement="top" arrow onClick={() => handleChange(`panel${`${index}-reason`}`)}>
                      <Button size="large">
                        <CancelIcon />
                      </Button>
                    </Tooltip>
                  </ActionsAccess>
                <ActionsAccess btnType={"View tracking"} status={order.status}>
                  <Tooltip title="View tracking" placement="top" arrow>
                    <Button
                      size="large"
                      onClick={() => {
                        navigate(`/event-tracking?id=${order.id}`);
                        setCount(count + 1);
                      }}
                    >
                      <ZoomInIcon />
                    </Button>
                  </Tooltip>
                </ActionsAccess>
                <ActionsAccess btnType="Quick edit" status={order.status}>
                  <Tooltip title="Quick edit" placement="top" arrow>
                    <Button size="large" onClick={() => onClickQuickEdit({ order, ...rest })}>
                      <FlashOnIcon />
                    </Button>
                  </Tooltip>
                </ActionsAccess>
                  <ActionsAccess btnType={"Modify order"} status={order.status}>
                    <Tooltip title="Modify order" placement="top" arrow>
                      <Button size="large" onClick={() => navigate(`/edit-order?id=${order.id}`)}>
                        <EditIcon />
                      </Button>
                    </Tooltip>
                  </ActionsAccess>
              </ButtonGroup>
            </Grid>
          </Grid>
        </div>
      );
    })}
  </>
);

Now I am not able to understand why it is taking too much time to check the checkBox on I click. Shouldn't I use wrapper component here? and go with the just conditional rendering inside the component (which looks code ugly unreadable)

Here is my ActionAccess component.

const enums: Enums = {
  1: [
    "Accept",
    "Accept and open document",
    "Accept and add note",
    "View document",
    "Delete order",
    "Reject order",
    "View tracking",
    "Modify order",
  ],
  2: ["View document", "Delete order", "Reject order", "View tracking"],
  3: ["View document", "Delete order", "View tracking"],
  4: ["View document", "Delete order", "View tracking", "Modify order", "Quick edit"],
  8: ["View document", "Delete order", "View tracking", "Modify order", "Quick edit"],
  5: ["View document", "View tracking"],
  9: ["View document", "Delete order", "View tracking", "Modify order"]
};

export default function ActionsAccess({ children, status, btnType }: props) {
  const feature = enums[status];
  return feature?.indexOf(btnType) !== -1 ? children : null;
}


from Rendering taking too much time in reactjs

Tuesday 24 October 2023

plotnine change default colormap

Is there a way to change the default colormap in plotnine to a custom colormap? I like to avoid adding something like + scale_color_manual(custom_palette) to all my ggplot figures.

I know how to change the theme for the plotnine figures with theme_set(), but is there also something equivalent like scale_set(), since scale define the colors in plotnine.

The same applies for changing the datetick format for the x axis when your data has a time index. Is there a global way to set it instead of using scale_x_datetime(breaks=date_breaks('5 years'), labels=date_format('%Y'))

In other words, I am looking for something that is equivalent to

import matplotlib.pyplot as plt
plt.style.use('ggplot')

in Matplotlib.



from plotnine change default colormap

How to use a custom scoring function in GridSearchCV for unsupervised learning

I want to grid search over a set of hyper parameters to tune a clustering model. GridSearchCV offers a bunch of scoring functions for unsupervised learning but I want to use a function that's not in there, e.g. silhouette score.

The documentation on how to implement my custom function is unclear on how we should define our scoring function. The example there shows simply importing a custom scorer and using make_scorer to create a custom scoring function. However, make_scorer seems to require the true values (which doesn't exist in unsupervised learning), so it's not clear how to use it.

Here's what I have so far:

from sklearn.datasets import make_blobs
from sklearn.model_selection import GridSearchCV
from sklearn.cluster import DBSCAN
from sklearn.metrics import silhouette_score, make_scorer

def my_custom_function(model, X):
    preds = model.predict(X)
    return silhouette_score(X, preds)

Z, _ = make_blobs()

model = DBSCAN()
pgrid = {'eps': [0.1*i for i in range(1,6)]}
gs = GridSearchCV(model, pgrid, scoring=my_custom_function)
gs.fit(Z)
best_score = gs.score(Z)

But it throws two errors:

TypeError: my_custom_function() takes 2 positional arguments but 3 were given

and

AttributeError: 'DBSCAN' object has no attribute 'predict'

How do I correctly define my custom scoring function?



from How to use a custom scoring function in GridSearchCV for unsupervised learning

Monday 23 October 2023

Gunicorn behavior with worker and thread configuration

I'm currently using Gunicorn version 20.1.0, and I have a question regarding the behavior of Gunicorn when using the following command:

Dockerfile

 CMD ["gunicorn", "--workers", "20", "--threads", "5", "application:application", "--max-requests", "250", "--max-requests-jitter", "50"]

My initial understanding was that this command should instruct Gunicorn to start 20 workers, each supported by 5 threads. After each worker completes 250 requests, it should be terminated and a new worker should be spawned. However, the observed behavior is different.

The number of workers (processes with different PIDs) keeps increasing beyond 60, which eventually leads to memory exhaustion and timeouts. It seems like the workers are not being terminated as expected.

My question is two-fold:

Does Gunicorn create more than 20 workers as I specified in the command? Is Gunicorn failing to terminate workers that have served 250 requests? I would appreciate any insights or suggestions to help me understand and rectify this behavior. Thank you!



from Gunicorn behavior with worker and thread configuration