cefpython - simple tutorials · example comes with cefpython. so does everyone who runs my...

63
CEFPython Developing applications using Chromium simpletutorials.com/cef Jason Jones @JasonProgrammer

Upload: vuongdiep

Post on 30-Apr-2018

225 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: CEFPython - Simple Tutorials · example comes with cefpython. So does everyone who runs my application need to install Python? ... What is Cython? Python + C data types It’s a language

CEFPythonDeveloping applications using Chromium

simpletutorials.com/cefJason Jones@JasonProgrammer

Page 2: CEFPython - Simple Tutorials · example comes with cefpython. So does everyone who runs my application need to install Python? ... What is Cython? Python + C data types It’s a language

What is Chromium?

The code behind Google’s Chrome browser.

http://www.chromium.org/Home

*Google has Chromium OS, but we’re talking about the browser

Page 3: CEFPython - Simple Tutorials · example comes with cefpython. So does everyone who runs my application need to install Python? ... What is Cython? Python + C data types It’s a language

What is CEF?What is CEF?

Page 4: CEFPython - Simple Tutorials · example comes with cefpython. So does everyone who runs my application need to install Python? ... What is Cython? Python + C data types It’s a language

Chromium Embedded Framework

C/C++ wrapper around Chromium.

Helps embed Chromium into applications.

Page 5: CEFPython - Simple Tutorials · example comes with cefpython. So does everyone who runs my application need to install Python? ... What is Cython? Python + C data types It’s a language

What is CEF?What is CEFPython?

Page 6: CEFPython - Simple Tutorials · example comes with cefpython. So does everyone who runs my application need to install Python? ... What is Cython? Python + C data types It’s a language

Chromium

CEF

c++

c++

CEFPython c++, cython

...a Python wrapper for CEF.

Page 7: CEFPython - Simple Tutorials · example comes with cefpython. So does everyone who runs my application need to install Python? ... What is Cython? Python + C data types It’s a language

Why use it?

● You’re familiar with web design.● You want a reponsive, cross-platform UI.● You want custom UI controls.● You want to provide a client with a custom

browser.

Page 8: CEFPython - Simple Tutorials · example comes with cefpython. So does everyone who runs my application need to install Python? ... What is Cython? Python + C data types It’s a language

What’s something I’d like to do with it?

Page 9: CEFPython - Simple Tutorials · example comes with cefpython. So does everyone who runs my application need to install Python? ... What is Cython? Python + C data types It’s a language
Page 10: CEFPython - Simple Tutorials · example comes with cefpython. So does everyone who runs my application need to install Python? ... What is Cython? Python + C data types It’s a language

What is CEF?Who uses CEF?

Page 11: CEFPython - Simple Tutorials · example comes with cefpython. So does everyone who runs my application need to install Python? ... What is Cython? Python + C data types It’s a language

Note the responsive design (resize-friendly)

Amazon

Page 13: CEFPython - Simple Tutorials · example comes with cefpython. So does everyone who runs my application need to install Python? ... What is Cython? Python + C data types It’s a language

CEFPython Requirements

Windows XP SP3+Python 2.7 (no Python 3 packages yet)

http://www.magpcss.net/cef_downloads/

Page 14: CEFPython - Simple Tutorials · example comes with cefpython. So does everyone who runs my application need to install Python? ... What is Cython? Python + C data types It’s a language

What is CEF?

What UI frameworks work with CEFPython?

Page 15: CEFPython - Simple Tutorials · example comes with cefpython. So does everyone who runs my application need to install Python? ... What is Cython? Python + C data types It’s a language

Quite a few...Kivy - MIT LicensePyWin32 - Python Software Foundation licensePanda3D - Modified BSD - 3D game engine (Disney)wxPython - based on wxWidgets C++ libraryPyGTK - LGPLPySide - LGPL

PyQt - GPL, Fee for commercial use

*No CEFPython examples provided for Tkinter

Page 16: CEFPython - Simple Tutorials · example comes with cefpython. So does everyone who runs my application need to install Python? ... What is Cython? Python + C data types It’s a language

I chose wxPython.

Open sourceFree for commercial useCross-platformBased on wxWidgets (C++ library)

Page 17: CEFPython - Simple Tutorials · example comes with cefpython. So does everyone who runs my application need to install Python? ... What is Cython? Python + C data types It’s a language

A great wxPython example comes with cefpython.

Page 18: CEFPython - Simple Tutorials · example comes with cefpython. So does everyone who runs my application need to install Python? ... What is Cython? Python + C data types It’s a language
Page 19: CEFPython - Simple Tutorials · example comes with cefpython. So does everyone who runs my application need to install Python? ... What is Cython? Python + C data types It’s a language
Page 20: CEFPython - Simple Tutorials · example comes with cefpython. So does everyone who runs my application need to install Python? ... What is Cython? Python + C data types It’s a language

So does everyone who runs my application need to install Python?

Page 21: CEFPython - Simple Tutorials · example comes with cefpython. So does everyone who runs my application need to install Python? ... What is Cython? Python + C data types It’s a language

We can create an executable from Python code.

py2exe

http://www.py2exe.org/

Page 22: CEFPython - Simple Tutorials · example comes with cefpython. So does everyone who runs my application need to install Python? ... What is Cython? Python + C data types It’s a language

py2exe build script

Page 23: CEFPython - Simple Tutorials · example comes with cefpython. So does everyone who runs my application need to install Python? ... What is Cython? Python + C data types It’s a language

set PYTHONPATH=C:\Python27;C:\Python27\Lib;C:\Python27\DLLsC:\Python27\python.exe build_exe.py py2exe

wxpython.exe(no Python interpreter needed to run this)

Page 24: CEFPython - Simple Tutorials · example comes with cefpython. So does everyone who runs my application need to install Python? ... What is Cython? Python + C data types It’s a language
Page 25: CEFPython - Simple Tutorials · example comes with cefpython. So does everyone who runs my application need to install Python? ... What is Cython? Python + C data types It’s a language

What does task manager look like?

Page 26: CEFPython - Simple Tutorials · example comes with cefpython. So does everyone who runs my application need to install Python? ... What is Cython? Python + C data types It’s a language
Page 27: CEFPython - Simple Tutorials · example comes with cefpython. So does everyone who runs my application need to install Python? ... What is Cython? Python + C data types It’s a language

Multiple Processes

Chromium uses multiple processes and talks between those processes (IPC).

Page 28: CEFPython - Simple Tutorials · example comes with cefpython. So does everyone who runs my application need to install Python? ... What is Cython? Python + C data types It’s a language

What processes are involved?

Page 29: CEFPython - Simple Tutorials · example comes with cefpython. So does everyone who runs my application need to install Python? ... What is Cython? Python + C data types It’s a language

Browser Process

Render process Blink - Layout (parse HTML, CSS)V8 - Run JavaScript

Render process

Plugin process (e.g. Flash)

wxpython.exe

subprocess.exe

Page 30: CEFPython - Simple Tutorials · example comes with cefpython. So does everyone who runs my application need to install Python? ... What is Cython? Python + C data types It’s a language

When debugging, keep in mind there are multiple processes.

If your breakpoint isn’t being hit, you might need to use a __debugbreak().

Page 31: CEFPython - Simple Tutorials · example comes with cefpython. So does everyone who runs my application need to install Python? ... What is Cython? Python + C data types It’s a language

browser_subprocess_path

A separate (lightweight) process to be used as each render process.

Page 32: CEFPython - Simple Tutorials · example comes with cefpython. So does everyone who runs my application need to install Python? ... What is Cython? Python + C data types It’s a language

Message LoopChromium uses multiple threads. Each thread has a message loop.

http://www.chromium.org/developers/design-documents/threading

Safe BrowsingHistoryProxy

User InterfaceIOFileDB

Page 33: CEFPython - Simple Tutorials · example comes with cefpython. So does everyone who runs my application need to install Python? ... What is Cython? Python + C data types It’s a language

To run the message loop...

Call a blocking function that runs the message loops

-OR-Call DoMessageLoopWork on a timer or on an idle event.

Page 34: CEFPython - Simple Tutorials · example comes with cefpython. So does everyone who runs my application need to install Python? ... What is Cython? Python + C data types It’s a language

Don’t starve the message loop.

Page 35: CEFPython - Simple Tutorials · example comes with cefpython. So does everyone who runs my application need to install Python? ... What is Cython? Python + C data types It’s a language

Sandbox

The render process can only access:● CPU● Memory

http://dev.chromium.org/developers/design-documents/sandbox/Sandbox-FAQ

Page 36: CEFPython - Simple Tutorials · example comes with cefpython. So does everyone who runs my application need to install Python? ... What is Cython? Python + C data types It’s a language

A simple application to search the 50 states by name.

Page 37: CEFPython - Simple Tutorials · example comes with cefpython. So does everyone who runs my application need to install Python? ... What is Cython? Python + C data types It’s a language

Multithreaded, production ready web server and framework (WSGI)

Page 38: CEFPython - Simple Tutorials · example comes with cefpython. So does everyone who runs my application need to install Python? ... What is Cython? Python + C data types It’s a language
Page 39: CEFPython - Simple Tutorials · example comes with cefpython. So does everyone who runs my application need to install Python? ... What is Cython? Python + C data types It’s a language

[ "Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "District Of Columbia", "Florida", "Georgia", …(more)...]

states.json

Page 40: CEFPython - Simple Tutorials · example comes with cefpython. So does everyone who runs my application need to install Python? ... What is Cython? Python + C data types It’s a language
Page 41: CEFPython - Simple Tutorials · example comes with cefpython. So does everyone who runs my application need to install Python? ... What is Cython? Python + C data types It’s a language

What about Python 3?https://code.google.com/p/cefpython/

Page 42: CEFPython - Simple Tutorials · example comes with cefpython. So does everyone who runs my application need to install Python? ... What is Cython? Python + C data types It’s a language
Page 43: CEFPython - Simple Tutorials · example comes with cefpython. So does everyone who runs my application need to install Python? ... What is Cython? Python + C data types It’s a language

What is Cython?

Python + C data types

It’s a language (superset of Python) that compiles to C++.It’s used for optimization and interfacing to C/C++ code.

Page 44: CEFPython - Simple Tutorials · example comes with cefpython. So does everyone who runs my application need to install Python? ... What is Cython? Python + C data types It’s a language

Can I update CEF?

What if I want the latest version of CEF/Chromium?

Page 45: CEFPython - Simple Tutorials · example comes with cefpython. So does everyone who runs my application need to install Python? ... What is Cython? Python + C data types It’s a language

CEF Builds

Latest nightly - Rev. 2378Requires Visual Studio 2013

https://cefbuilds.com/

Page 46: CEFPython - Simple Tutorials · example comes with cefpython. So does everyone who runs my application need to install Python? ... What is Cython? Python + C data types It’s a language

Python extensions for Windows need to be built with the same version of VC++ used to build Python.

Page 47: CEFPython - Simple Tutorials · example comes with cefpython. So does everyone who runs my application need to install Python? ... What is Cython? Python + C data types It’s a language

https://docs.python.org/3/extending/windows.html

Page 48: CEFPython - Simple Tutorials · example comes with cefpython. So does everyone who runs my application need to install Python? ... What is Cython? Python + C data types It’s a language

http://matthew-brett.github.io/pydagogue/python_msvc.html

Page 49: CEFPython - Simple Tutorials · example comes with cefpython. So does everyone who runs my application need to install Python? ... What is Cython? Python + C data types It’s a language

Visual C++ for VS2013 is version 18

http://en.wikipedia.org/wiki/Visual_C%2B%2B

Page 50: CEFPython - Simple Tutorials · example comes with cefpython. So does everyone who runs my application need to install Python? ... What is Cython? Python + C data types It’s a language

Are there Python binaries built with VS2013?

Page 51: CEFPython - Simple Tutorials · example comes with cefpython. So does everyone who runs my application need to install Python? ... What is Cython? Python + C data types It’s a language

http://p-nand-q.com/python/building-python-33-with-vs2013.html

Page 52: CEFPython - Simple Tutorials · example comes with cefpython. So does everyone who runs my application need to install Python? ... What is Cython? Python + C data types It’s a language

Alternatives

Is it possible to use Python 3 and the CEF Nightly build?

Page 53: CEFPython - Simple Tutorials · example comes with cefpython. So does everyone who runs my application need to install Python? ... What is Cython? Python + C data types It’s a language

Yes. You can write Python-C++ bindings with SWIG.

Page 54: CEFPython - Simple Tutorials · example comes with cefpython. So does everyone who runs my application need to install Python? ... What is Cython? Python + C data types It’s a language

http://en.wikipedia.org/wiki/SWIGhttp://www.swig.org/tutorial.htmlhttp://www.wxpython.org/OSCON2008/Python%20C++%20and%20SWIG.pdf

Simplified Wrapper and Interface Generator

Page 55: CEFPython - Simple Tutorials · example comes with cefpython. So does everyone who runs my application need to install Python? ... What is Cython? Python + C data types It’s a language

● Allegro CL● C#● CFFI● CLISP● Chicken● D

● R● Ruby● Scilab● Tcl● UFFI

SWIG can generate wrapper code for 23 languages:

● Go● Guile● Java● Javascript● Lua● Modula-3

● Mzscheme● OCAML● Octave● Perl● PHP● Python

Page 56: CEFPython - Simple Tutorials · example comes with cefpython. So does everyone who runs my application need to install Python? ... What is Cython? Python + C data types It’s a language

Install SWIG

DownloadExtractAdd directory to your PATH

Page 57: CEFPython - Simple Tutorials · example comes with cefpython. So does everyone who runs my application need to install Python? ... What is Cython? Python + C data types It’s a language

Install VS

Page 58: CEFPython - Simple Tutorials · example comes with cefpython. So does everyone who runs my application need to install Python? ... What is Cython? Python + C data types It’s a language

Interface File

Helps SWIG know what to wrap

Page 59: CEFPython - Simple Tutorials · example comes with cefpython. So does everyone who runs my application need to install Python? ... What is Cython? Python + C data types It’s a language

CefSwigApp_wrap.cxx

swig -c++ -python CefSwigApp.i

cefswig.py

Our Python module:

import cefswig

C++ wrapper we will build with our source to produce a pyd (DLL) file

Page 60: CEFPython - Simple Tutorials · example comes with cefpython. So does everyone who runs my application need to install Python? ... What is Cython? Python + C data types It’s a language

CEF C++ Solution

Page 61: CEFPython - Simple Tutorials · example comes with cefpython. So does everyone who runs my application need to install Python? ... What is Cython? Python + C data types It’s a language
Page 62: CEFPython - Simple Tutorials · example comes with cefpython. So does everyone who runs my application need to install Python? ... What is Cython? Python + C data types It’s a language
Page 63: CEFPython - Simple Tutorials · example comes with cefpython. So does everyone who runs my application need to install Python? ... What is Cython? Python + C data types It’s a language

Questions? @jasonprogrammer

Thanks!