Closed KMouratidis closed 5 years ago
Possible to get a full, reproducible mwe (including requirements)?
I'd opine pdoc --html app.py
imports app.py only once.
Fair point. MWE, two files in the same directory.
server.py
print(__name__)
from dash import Dash
app = Dash(__name__)
app.config['suppress_callback_exceptions'] = True
and app.py
:
print(__name__)
from server import app
from dash.dependencies import Input, Output
import dash_html_components as html
app.layout = html.Div([
html.H4("Hello world", id="greeter"),
html.Button("Hide this", id="sneaky", n_clicks=0)
])
# Show or hide H4 element
@app.callback(Output("greeter", "style"),
[Input("sneaky", "n_clicks")])
def show_login_input(value):
"""DOCSTRING!"""
if value % 2 == 0:
return {"display": "inline"}
return {"display": "none"}
(Edit: actually running the app or not with app.run_server
inside app.py, or debug=True/False didn't matter so I removed them).
(Edit2: adding a print statement in app.py and another in server.py shows that app.py is loaded twice)
Then using the cli: pdoc --html app.py
returns the complete traceback:
Versions (pdoc is 0.5.3 as mentioned above, python 3.6.6, GCC 8.0.1 20180414):
dash_html_components==0.15.0
dash_core_components==0.46.0
dash==0.41.0
dash_callback_chain==0.0.2
dash_table==3.6.0
Thanks. Added the prints for you.
I think I understand the problem:
$ python -c 'import app.py'
app
server
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named 'app.py'; 'app' is not a package
pdoc first tries to import the CLI-specified module (app.py) with importlib.import_module()
. The python import machinery thinks app.py is a package path, so it imports app
from the current directory, then it tries to import module py
from it, whereby it fails because app
is not a package. But app
remains imported and is later imported once again as a file.
I'm not sure what to do about it. :confused:
I am not sure about it either, I can only write about a few things I tried but there will probably be more noise rather information. Anyway, here goes nothing:
Of course commenting these out at least creates the html for the single file, but it doesn't work for the whole project. Sadly, even manually trying to use pdoc-cli on every file doesn't succeed for a significant number of files, with more or less errors of the same nature:
Of course it could be me not handling the project structure correctly, since the MWE now works.
However it also fails when I'm trying to do use pdoc-cli on the MWE directory.
All that said, I did notice something of potential interest: it is imports within the python modules that break pdoc. For example here is a truncated version of a problematic module:
### OTHER IMPORTS
from server import app
import layouts
from utils import create_dropdown, mapping, get_data
from apps.exploration.graphs.graphs2d import scatterplot
### VARIOUS STUFF
@app.callback(
### CALLBACK STUFF
)
def fit(VARIOUS_VARS):
"""A SIMPLE DOCSTRING"""
### VARIOUS STUFF
if len(xvars) == 2:
trace = scatterplot(df[xvars[0]], df[xvars[1]],
marker={'color': labels.astype(np.float)})
### OTHER STUFF
It turns out removing this line from apps.exploration.graphs.graphs2d import scatterplot
solves the issue. Either I should change how imports are done ( don't know how :/ ) or there might be a problem with how pdoc loads sibling-packages?
Anyhow, don't fret too much as this probably is a Dash-only issue. Monkey-patching the app.callback to do no validation, along with manually calling pdoc on every file (os.walk
+ os.system("pdoc...")
) fixes the issue and all the files are correctly handled. Any idea how to create an index file manually?
No, no, no, this should work!
Welcome to test PR https://github.com/pdoc3/pdoc/pull/62 as it contains importing improvements that might make a difference for you.
Thanks! It works! I've also added a few packages and modules in the MWE and it works correctly as well.
I can't seem to get it to work with my project due to some import error: ImportError: Error importing 'apps.analyze': expected str, bytes or os.PathLike object, not NoneType
. I've been trying for hours to understand what's wrong but I can't figure it out. Here's what reproduces the error:
A folder (models
) with only a single file (example.py
), and using the CLI (e.g. pdoc --html .
from within the directory or pdoc --html models
from outside):
"""Docstring"""
# version 0.20.3
from sklearn.base import BaseEstimator, ClassifierMixin, TransformerMixin
from sklearn.linear_model import LinearRegression
pass
ImportError: Error importing 'models.example': expected str, bytes or os.PathLike object, not NoneType
Is this because of relative imports? It seems some libraries (e.g. networkx) have the same issue, while others don't (e.g. pickle, quandl, dash).
Fixed in the last commit added to the PR (https://github.com/pdoc3/pdoc/pull/62/commits/a3d0f25ae3f503cb125e3d56af6d5b04e93c6f37).
Nice! It works correctly now! Thanks once again :)
Thank you for the help! :cake:
Expected Behavior
Module is not loaded more than once (?)
Actual Behavior
Module seems to be loaded multiple times, thus throwing an error due to libraries used.
Steps to Reproduce
I have a Plotly/Dash application that works fine on its own but when I try to use pdoc it raises a Dash exception that only occurs when you attempt to output to the same component multiple times:
I know this is not the case because normally the application works. This particular element isn't tampered with anyhere else in the project, so the file I'm trying to output docs for must be loaded twice.
Edit: Functions are usually wrapped with decorators that reference the top-level app (in a separate file, as per the Dash docs).
Example callback:
Additional info
Running using the cli, (e.g.
pdoc --html app.py
) Running the app is protected withing aif __name__ ....
block.