Can't read contents of a file #16
Replies: 5 comments
-
| I find I can read a text file using only pyscript with the code below. Is there a way to do it with ltk?  | 
Beta Was this translation helpful? Give feedback.
-
| In LTK, I would use  | 
Beta Was this translation helpful? Give feedback.
-
| Awesome--that works great! Thank you so much! Here is some code that exercises the new file reading ability in case it is useful for anyone.  | 
Beta Was this translation helpful? Give feedback.
-
| Can you put that example on pyscript.com? Then we can link to it from the
LTK docs.
Chris… On Fri, Nov 29, 2024 at 2:06 AM Greg Nordin ***@***.***> wrote:
 Awesome--that works great! Thank you so much!
 Here is some code that exercises the new file reading ability in case it
 is useful for anyone.
 #--------------------------------------------------------------------
 # main.py
 import ltk
 @ltk.callback
 def loaded_file(file, content):
     ltk.find("#content").html(content)
 def create():
     return ltk.VBox(
         ltk.Heading1("Can we read a file?"),
         ltk.File(loaded_file),
         ltk.Label("File contents:"),
         ltk.Preformatted(
             "File contents will be shown here after selecting file.",
             {
                 "margin-left": 20,
                 "margin-right": 20,
                 "padding": 8,
                 "background-color": "lightyellow",
                 "border": "1px solid gray",
             },
         ).attr("id", "content"),
     )
 create().appendTo(ltk.find("body"))
 #--------------------------------------------------------------------
 # index.html
 <!DOCTYPE html>
 <html lang="en">
 <head>
     <title>Try to read file</title>
     <!-- Recommended meta tags -->
     <meta charset="UTF-8">
     <meta name="viewport" content="width=device-width,initial-scale=1.0">
     <!-- PyScript CSS -->
     <link rel="stylesheet" href="https://pyscript.net/releases/2024.10.2/core.css">
     <!-- This script tag bootstraps PyScript -->
     <script type="module" src="https://pyscript.net/releases/2024.10.2/core.js"></script>
     <!-- Import jQuery - used in ltk/jquery.py -->
     <script ***@***.***/dist/jquery.min.js"></script>
     <script ***@***.***/dist/jquery-ui.min.js"></script>
     <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.13.2/themes/base/jquery-ui.css" />
 </head>
 <body>
     <script type="py" src="./main.py" config="./pyscript.toml"></script>
 </body>
 </html>
 #--------------------------------------------------------------------
 # pyproject.toml
 packages = ["pyscript-ltk"]
 [files]
 "https://raw.githubusercontent.com/pyscript/ltk/main/ltk/ltk.js" = "ltk/ltk.js"
 "https://raw.githubusercontent.com/pyscript/ltk/main/ltk/ltk.css" = "ltk/ltk.css"
 —
 Reply to this email directly, view it on GitHub
 <#16 (comment)>,
 or unsubscribe
 <https://github.com/notifications/unsubscribe-auth/AE4XAKF3KK3GHZG5MUNYKKT2C642NAVCNFSM6AAAAABSUPZQKSVHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTCNBRGEZDCMQ>
 .
 You are receiving this because you were mentioned.Message ID:
 ***@***.***>
 | 
Beta Was this translation helpful? Give feedback.
-
| I put the example on pyscript.com at https://pyscript.com/@gnordin/ltk-read-a-file/latest. If there is a different place you would like it, please let me know. I've not really used pyscript.com before so I don't know what the norms are in how to share things there. | 
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
@laffra: I'm trying to read the contents of a user-specified text file with the code appended below as
main.py. I keep getting an error because ltk.File evidently does not generate the full path of the file and instead gives something likeC:\fakepath\testfile.txtwheretestfile.txtis the correct name of the file I selected. I assume I'm missing something, but I don't know what. Is there a way to read the contents of a text file?Beta Was this translation helpful? Give feedback.
All reactions