Under cmd, try to run "bokeh serve --show myapp.py", but seems cannot compile python file

I try to run bokeh server example (Running a Bokeh server — Bokeh 2.4.2 Documentation)
under command mode, issue “bokeh serve --show myapp.py”, and get error as below:


ERROR: Error loading D:\project\BokehTest\myapp:

Invalid syntax in ‘main.py’ on line 1:

myapp.py

Traceback (most recent call last):
File “d:\project\bokehtest.venv\lib\site-packages\bokeh\application\handlers\code_runner.py”, line 106, in init
nodes = ast.parse(source, os.fspath(path))
File “C:\Users\Ixxxx\AppData\Local\Programs\Python\Python37\lib\ast.py”, line 35, in parse
return compile(source, filename, mode, PyCF_ONLY_AST)
File “D:\project\BokehTest\myapp\main.py”, line 1
# myapp.py
^
SyntaxError: invalid character in identifier

Seems the python file cannot be compiled correctly.

“bokeh serve” and “python myapp.py” are all fine. Not sure if anything goes wrong.

Hi @Chen_Jack please edit your post to use code formatting so that the code is intelligible (either with the </> icon on the editing toolbar, or triple backtick ``` fences around the code blocks)

Additionally, the issue is almost certainly something specific to your code or setup. In order to investigate we would need a complete Minimal Reproducible Example.

Hi Bryan, the myapp.py is copied from Running a Bokeh server — Bokeh 2.4.2 Documentation as below:
‘’’

myapp.py

from random import random

from bokeh.layouts import column
from bokeh.models import Button
from bokeh.palettes import RdYlBu3
from bokeh.plotting import figure, curdoc

create a plot and style its properties

p = figure(x_range=(0, 100), y_range=(0, 100), toolbar_location=None)
p.border_fill_color = ‘black’
p.background_fill_color = ‘black’
p.outline_line_color = None
p.grid.grid_line_color = None

add a text renderer to the plot (no data yet)

r = p.text(x=, y=, text=, text_color=, text_font_size=“26px”,
text_baseline=“middle”, text_align=“center”)

i = 0

ds = r.data_source

create a callback that adds a number in a random location

def callback():
global i

# BEST PRACTICE --- update .data in one step with a new dict
new_data = dict()
new_data['x'] = ds.data['x'] + [random()*70 + 15]
new_data['y'] = ds.data['y'] + [random()*70 + 15]
new_data['text_color'] = ds.data['text_color'] + [RdYlBu3[i%3]]
new_data['text'] = ds.data['text'] + [str(i)]
ds.data = new_data

i = i + 1

add a button widget and configure with the call back

button = Button(label=“Press Me”)
button.on_click(callback)

put the button and plot in a layout and add to the document

curdoc().add_root(column(button, p))
‘’’
It is always the first line has syntax err. I wonder if any setting missing causes ast.py handler error.
Bokeh version: 2.4.2
Python version: 3.7.9

Sorry that the handler shall be code_runner.py, not ast.py.
‘’’
Traceback (most recent call last):
File “C:\users\itri2021\appdata\local\programs\python\python37\lib\site-packages\bokeh\application\handlers\code_runner.py”, line 106, in init
nodes = ast.parse(source, os.fspath(path))
File “C:\users\itri2021\appdata\local\programs\python\python37\lib\ast.py”, line 35, in parse
return compile(source, filename, mode, PyCF_ONLY_AST)
File “D:\project\BokehTest\myapp.py”, line 1
# myapp.py
^
SyntaxError: invalid character in identifier
‘’’

Hi @Chen_Jack please note that for code formatting, it is backtick characters ``` not single quotes.

I cannot reproduce any problem with the code on that page, it runs as expected with Bokeh 2.4.2. You will need to provide more details about your system and exactly what you are doing. But as a first step I would actually suggest that you clone the main repo and try to run some of the examples from the examples directory there directly:

Perhaps there is some editor or copy past issue with hidden characters that is causing an issue for you on your local system. All the examples in the repo are test to work continuously in our automated CI system.

Hi @Bryan , The file created by notepad is working and the file created by Visual Studo Code wasn’t. My Visual Studio Code version is 1.67.0. Still cannot find hidden characters but that should be the problem. Many thanks!

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.