holzschu / Carnets

Carnets is a stand-alone Jupyter notebook server and client. Edit your notebooks on the go, even where there is no network.
https://holzschu.github.io/Carnets_Jupyter/
BSD 3-Clause "New" or "Revised" License
567 stars 34 forks source link

WKWebView Support? #338

Closed wangyuanhong2 closed 1 month ago

wangyuanhong2 commented 1 month ago

Hi there, I would like to use webview on carnets and tried using following steps but failed. Can you help take a look and direct me how to move forward(maybe use another library to make me use webview function)? Thanks!

pip install pythonista-wkwebview v = WKWebView() v.present() v.load_html('Hello world') v.load_url('http://omz-software.com/pythonista/')

got the following error: ModuleNotFoundError Traceback (most recent call last) Cell In[1], line 1 ----> 1 from wkwebview import * 2 v = WKWebView() 3 v.present()

File /var/mobile/Containers/Data/Application/9F32A456-4131-45A4-8487-7CAEB77E3A45/Library/lib/python3.11/site-packages/wkwebview.py:8 2 ''' 3 WKWebView - modern webview for Pythonista 4 ''' 6 version = '1.0' ----> 8 from objc_util import * 9 import ui, console, webbrowser 10 import queue, weakref, ctypes, functools, time, os, json, re

ModuleNotFoundError: No module named 'objc_util'

holzschu commented 1 month ago

Hi, "pythonista-wkwebview" is specific to Pythonista, it doesn't transfer to other Python engines.

That being said, Carnets, because it is based on Jupyter, is already running in a web page. Depending on your goals, you can have HTML code in a Markdown cell, or opened as a link. I would suggest looking for "HTML embedding in a Jupyter notebook".

If your needs are more specific, there's also a-Shell, which is also running Python but has more control over the WkWebView (but again, not through pythonista-wkwebview).

wangyuanhong2 commented 1 month ago

Thank you! Can you elaborate a little more? I tried a_shell today and did not find a way for python code to use wkwebview. It seems pylibc doesn’t support iOS, so import objc on a_shell will report error: import objc Traceback (most recent call last): File "", line 1, in File "/var/mobile/Containers/Data/Application/1F0CB941-A321-49BC-9CAF-6FE22B90E124/Library/lib/python3.11/site-packages/objc/init .py", line 7, in from . import _objc ImportError: dlopen(/private/var/containers/Bundle/Application/A6CFC1CD-DA10-4B97-B3B0-0DFBF4AB0F69/a-Shell.app/Frameworks/python3_ios- objc._objc.framework/python3_ios-objc._objc, 0x0002): tried: '/private/var/containers/Bundle/Application/A6CFC1CD-DA10-4B97-B3B0-0DFBF4 AB0F69/a-Shell.app/Frameworks/python3_ios-objc._objc.framework/python3_ios-objc._objc' (no such file), '/private/preboot/Cryptexes/OS/p rivate/var/containers/Bundle/Application/A6CFC1CD-DA10-4B97-B3B0-0DFBF4AB0F69/a-Shell.app/Frameworks/python3_ios-objc._objc.framework/p ython3_ios-objc._objc' (no such file), '/private/var/containers/Bundle/Application/A6CFC1CD-DA10-4B97-B3B0-0DFBF4AB0F69/a-Shell.app/Fra Traceback (most recent call last): File "", line 1, in File "/var/mobile/Containers/Data/Application/1F0CB941-A321-49BC-9CAF-6FE22B90E124/Library/lib/python3.11/site-packages/objc/init.py", line 7, in from . import _objc ImportError: dlopen(/private/var/containers/Bundle/Application/A6CFC1CD-DA10-4B97-B3B0-0DFBF4AB0F69/a-Shell.app/Frameworks/python3_ios-objc._objc.framework/python3_ios-objc._objc, 0x0002): tried: '/private/var/containers/Bundle/Application/A6CFC1CD-DA10-4B97-B3B0-0DFBF4AB0F69/a-Shell.app/Frameworks/python3_ios-objc._objc.framework/python3_ios-objc._objc' (no such file), '/private/preboot/Cryptexes/OS/private/var/containers/Bundle/Application/A6CFC1CD-DA10-4B97-B3B0-0DFBF4AB0F69/a-Shell.app/Frameworks/python3_ios-objc._objc.framework/python3_ios-objc._objc' (no such file), '/private/var/containers/Bundle/Application/A6CFC1CD-DA10-4B97-B3B0-0DFBF4AB0F69/a-Shell.app/Fra meworks/python3_ios-objc._objc.framework/python3_ios-objc._objc' (no such file)

holzschu commented 1 month ago

Again, it depends on what you want to do. But internalbrowser <url> will open a web page or a file inside a-Shell.

wangyuanhong2 commented 1 month ago

Let me a little more specific. I used a library to scrape some websites to fetch some data back and the library uses pyminiracer which happens to be not supported in IOS. I am wondering whether I can replace pyminiracer with some code implementation to work around it on IOS.

holzschu commented 1 month ago

As far as I can tell, pyminiracer is for running JavaScript commands. From inside a-Shell, you can run JavaScript commands with jsc, as in: jsc file.js. It uses Apple's own JavaScript engine (different from V8 and node).

wangyuanhong2 commented 1 month ago

Thanks for the help. I’ve tried it.