How to run a script shell in google colab for Swift?

(Qin Lu) #1

Hello, I would like to run the following cell on Colab to enable my Google drive, and setup a link to easily save the exported files.

import Python
let drive = Python.import(“google.colab”).drive
drive.mount("/content/gdrive", force_remount: True)
let driveLink = “/content/fastai-v3-swift/”
!ln -sv “/content/gdrive/My Drive/fastai-v3” .
let _ = Python.import(“google.colab”).sys.path.insert(0, slink)

I get the following error:

error: <Cell 10>:8:3: error: consecutive statements on a line must be separated by ‘;’ ln -sv “/content/gdrive/My Drive/fastai-v3” .

It seems that the syntax to run shell commands is not working for me. I also tried %%, %%shell without luck.

How to run a script shell in google colab for swift?

Finally, if there is a snippet, or better way to do what I am trying please let me know.

0 Likes

#2

Hi QinLu
Did you manage to enter your authorisation code from google. I could not.

Can you try this ( not my original work)
import Foundation
let task = Process()
task.executableURL = URL(fileURLWithPath: “/usr/share/locale/ln”)

task.arguments = ["-sv", “/content/gdrive/My Drive/fastai-v3” ]
let outputPipe = Pipe()
let errorPipe = Pipe()

task.standardOutput = outputPipe
task.standardError = errorPipe
try task.run()
let outputData = outputPipe.fileHandleForReading.readDataToEndOfFile()
let errorData = errorPipe.fileHandleForReading.readDataToEndOfFile()
let output = String(decoding: outputData, as: UTF8.self)
let error = String(decoding: errorData, as: UTF8.self)

0 Likes

(Qin Lu) #3

Hello @Conwyn,

No, I couldn’t manage to enter the authorization code from google.

Thank you for your solution, I think it should work when we get Drive on.

I will ask for the Google Drive authorization issue.

Thank you.

0 Likes

SwiftAI To Do List
#4

Hi QinLu

In colab python you can type %sx read -p “hello” and this displays hello and a entry box. The colab swift does not have this functionality which is maybe why we can not enter the gdrive authorisation code.

Also the sample code I copied does not work. The code below does but the Pipe logic above does not work tonight.

import Foundation
let task = Process()
task.executableURL = URL(fileURLWithPath: “/bin/ls”)
task.arguments = ["-al"]
try task.run()

Note the revised URL above I used the whereis command in python !whereis ls to determine the location.

0 Likes

#5

Hi Qinlu

No progress made but somebody may have seen this before.

I was thinking if we could authenticate ourselves and then invoke gdrive we would avoid the invisible input box.

So let us try to authenticate ourselves. We get the URL listed and copy the code but

import Foundation

let task = Process()
let outputPipe = Pipe()
let inputPipe = Pipe()
let errorPipe = Pipe()

//task.standardOutput = outputPipe
//task.standardError = errorPipe
task.standardInput = inputPipe

task.executableURL = URL(fileURLWithPath: “/tools/google-cloud-sdk/bin/gcloud”)

task.arguments = [
“auth”,
“login”,
“–enable-gdrive-access”,
“–no-launch-browser”,
“–quiet”
]

try task.run()

Use URL to retrieve code

let input = “4/rw…”
let data = Data(input.utf8)
inputPipe.fileHandleForWriting.write(data)

But then it prints a new URL as if I typed in the wrong code.

Regards Conwyn

0 Likes

(Qin Lu) #6

@Conwyn,

There was some discussion in Python textfield output not working.

Cheers,

0 Likes