Monday, 21 February 2022

Dash datatable getting out of container

I have a dash datatable inside a dbc.Col (which is inside a dbc.Row), and I can not figure out hoy to keep the table inside its container when the screen size is smaller (trying to make it responsive). If the table is too big for its container, it should have a scroll bar, I added the "overflowX:auto" to its style and still won't work. Right now it looks like this:

enter image description here

The code is something like:

dbc.Col(dt.DataTable(
                id='tabla_req',
                style_table={
                'overflowX': 'auto',
                'width':'100%',
                'margin':'auto'},
                row_deletable=False,
                row_selectable="single",
                page_current= 0,
                page_size= 10,
                fill_width=False,
                style_data={
                    'whiteSpace': 'normal',
                    'height': 'auto',
                },
                #editable=False,
                data=df.to_dict('records'),
                columns=[{"name": i, "id": i} for i in df.columns.tolist()]),
width=12
)


from Dash datatable getting out of container

How to download a file using Python requests, when that file is being served with redirect?

I'm trying to download a book from Fadedpage, like this one. If you click on the link to the HTML file there, it will display the HTML file. The URL appears to be https://www.fadedpage.com/books/20170817/html.php. But if you try to download that URL by any of the usual means, you only get the metadata HTML, not the HTML with the full text of the book. For instance, running wget https://www.fadedpage.com/books/20170817/html.php from the command line does return HTML, but it's again the metadata HTML file from https://www.fadedpage.com/showbook.php?pid=20170817, not the full text of the book.

Here's what I've tried so far:

def downloadFile(bookID, fileType="html"): 
    url = f"https://www.fadedpage.com/books/{bookID}/{fileType}.php"
    #url = f'https://www.fadedpage.com/link.php?file={bookID}.{fileType}'
    headers = {"Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9", 
               "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) QtWebEngine/5.15.3 Chrome/87.0.4280.144 Safari/537.36",
               "referer": "https://www.fadedpage.com/showbook.php?pid={bookID}",
               "sec-fetch-dest": "document",
                "sec-fetch-mode": "navigate",
                "sec-fetch-site": "same-origin",
                "sec-fetch-user": "?1",
                "upgrade-insecure-requests": "1",
                "cookie": "PHPSESSID=3r7ql7poiparp92ia7ltv8nai5"
              }
    print("Getting ", url)
    resp = requests.get(url, headers=headers, cookies={"PHPSESSID": "3r7ql7poiparp92ia7ltv8nai5"})
    if resp.ok: 
        return resp.text

I'm trying to give it the same headers that my web browser is giving it, in the hopes that it'll return the same thing. But it's not working.

Is there something else I need to do to be able to download this HTML file? Since it's served by PHP on the server side, I'm having a hard time reverse engineering this.

For reference, the full HTML file contains the text "The First Part of this book is intended for pupils so far advanced as to be able to distinguish the Parts of Speech." But that text is not contained in the metadata HTML file.

Testing

Here's another way of testing this:

def isValidDownload(bookID, fileType="html"): 
    """
    A download of `downloadFile("20170817", "html")` should produce
    a file 20170817.html which contains the text "It was a woodland 
    slope behind St. Pierre-les-Bains". If it doesn't, it isn't getting 
    the full text file. 
    """
    with open(f"{bookID}.{fileType}") as f: 
        raw = f.read()
    test = "woodland slope behind St. Pierre-les-Bains"
    return test in raw

This should return True:

downloadFile("20170817", "html")
isValidDownload("20170817", "html")
False

Another attempt

A simpler version, based on the answer below, also doesn't work. Here it is all together:

def downloadFile(bookID, fileType): 
    headers = {"cookie": "PHPSESSID=3r7ql7poiparp92ia7ltv8nai5"}
    url = f"https://www.fadedpage.com/link.php?file={bookID}.{fileType}"
    print("Getting ", url)
    with requests.get(url, headers = headers) as resp:
        with open(f"{bookID}.{fileType}", 'wb') as f:
            f.write(resp.content)

def isValidDownload(bookID, fileType="html"): 
    """
    A download of `downloadFile("20170817", "html")` should produce
    a file 20170817.html which contains the text "It was a woodland 
    slope behind St. Pierre-les-Bains". If it doesn't, it isn't getting 
    the full text file. 
    """
    with open(f"{bookID}.{fileType}") as f: 
        raw = f.read()
    test = "woodland slope behind St. Pierre-les-Bains"
    return test in raw

downloadFile("20170817", "html")
isValidDownload("20170817", "html")

That returns False.



from How to download a file using Python requests, when that file is being served with redirect?

React Native Animated setValue() problem?

Actually I'm trying to set value of the animation with setValue() after Animated.timing() is finished and want to use this updated animated value then in a loop animation.

//Initialising animation value=50
const leftAnim = useRef(new Animated.Value(50)).current 
useEffect(() => {
    Animated.timing(leftAnim,{
        toValue:360,
        duration:3000,
        easing:Easing.linear,
        useNativeDriver:false,
    }).start(({finished}) => {
        //Updating animation value=100
        leftAnim.setValue(100)
        //Animated API is not considering the setValue and starting the loop animation with the first value i.e 50 instead of 100
        Animated.loop(
            Animated.timing(leftAnim,{
                toValue:360,
                duration:5000,
                easing:Easing.linear,
                useNativeDriver:false
            })
        ).start()
    })
},[])

Am I doing something wrong? Is there a better way to do it?



from React Native Animated setValue() problem?