Repository: bokeh/bokeh

Issue:
bokeh serve --show app/gapminder doesn't work
Fails with `SyntaxError: encoding declaration in Unicode string`. Encoding line was added recently. Though the problem is with bokeh server, which should strip those lines before running a script.



Discussion:
/cc @bryevdv

Is this a py2 issue then? Working fine in py3 from `master`

Yes. EDIT: Tested on py3, works.

Removing the encoding is the quickest fix for this release I assume? Can make separate issue for server to strip those out for next release?

There are unicode characters in the file, so no.

Oh, I see, for the play/pause buttons... grr OK I will do it later today. 

For reference this is how `bokeh-plot` directive does it:

```
# need to remove any encoding comment before compiling unicode
pat = re.compile(r"^# -\*- coding: (.*) -\*-$", re.M)
source = pat.sub("", source)
```

should be fairly simple to add to Bokeh server code exec too, just will have to have tests. 


Resolving patch:
diff --git a/bokeh/application/handlers/script.py b/bokeh/application/handlers/script.py
--- a/bokeh/application/handlers/script.py
+++ b/bokeh/application/handlers/script.py
@@ -1,7 +1,5 @@
 from __future__ import absolute_import, print_function
 
-import codecs
-
 from .code import CodeHandler
 
 class ScriptHandler(CodeHandler):
@@ -21,7 +19,7 @@ def __init__(self, *args, **kwargs):
             raise ValueError('Must pass a filename to ScriptHandler')
         filename = kwargs['filename']
 
-        with codecs.open(filename, 'r', 'UTF-8') as f:
+        with open(filename, 'r') as f:
             kwargs['source'] = f.read()
 
         super(ScriptHandler, self).__init__(*args, **kwargs)
diff --git a/bokeh/sampledata/gapminder.py b/bokeh/sampledata/gapminder.py
--- a/bokeh/sampledata/gapminder.py
+++ b/bokeh/sampledata/gapminder.py
@@ -30,7 +30,7 @@
         setattr(
             sys.modules[__name__],
             dataset,
-            pd.read_csv(filename, index_col='Country')
+            pd.read_csv(filename, index_col='Country', encoding='utf-8')
         )
     except (IOError, OSError):
         raise RuntimeError('Could not load gapminder data file "%s". Please execute bokeh.sampledata.download()' % filename)


Test patch:
diff --git a/bokeh/application/handlers/tests/test_script.py b/bokeh/application/handlers/tests/test_script.py
--- a/bokeh/application/handlers/tests/test_script.py
+++ b/bokeh/application/handlers/tests/test_script.py
@@ -22,3 +22,18 @@ def load(filename):
         assert result['handler']._runner.path == result['filename']
         assert result['handler']._runner.source == source
         assert not doc.roots
+
+    def test_runner_script_with_encoding(self):
+        doc = Document()
+        source = "# -*- coding: utf-8 -*-\nimport os"
+        result = {}
+        def load(filename):
+            handler = ScriptHandler(filename=filename)
+            handler.modify_document(doc)
+            result['handler'] = handler
+            result['filename'] = filename
+        with_file_contents(source, load)
+
+        assert result['handler'].error is None
+        assert result['handler'].failed is False
+        assert not doc.roots
diff --git a/tests/examples/examples.yaml b/tests/examples/examples.yaml
--- a/tests/examples/examples.yaml
+++ b/tests/examples/examples.yaml
@@ -3,6 +3,7 @@
 - path: "custom/gears"
   skip: ["gear.py"]
 - path: "compat"
+  skip: ["ggplot_density.py"]
 - path: "charts/file"
   type: file
 - path: "howto/charts"