module serverdoc.documentation_server
¶
Short summary¶
module pyquickhelper.serverdoc.documentation_server
This file defines a simple local server delivering generating documentation.
Classes¶
class |
truncated documentation |
---|---|
Define a simple handler used by HTTPServer, it just serves local content. |
|
defines a thread which holds a web server |
Functions¶
function |
truncated documentation |
---|---|
Runs the server. |
Properties¶
property |
truncated documentation |
---|---|
|
A boolean value indicating whether this thread is a daemon thread. This must be set before start() is called, otherwise … |
|
Thread identifier of this thread or None if it has not been started. This is a nonzero integer. See the get_ident() … |
|
A string used for identification purposes only. It has no semantics. Multiple threads may be given the same name. … |
|
Native integral thread ID of this thread, or None if it has not been started. This is a non-negative integer. … |
Static Methods¶
staticmethod |
truncated documentation |
---|---|
Displays the most requested files. |
|
Adds a mapping associated to a local path to watch. |
|
Locally executes a python script. |
|
Retrieves a file from the cache if it was cached, it the file was added later than a day, it returns None. |
|
defines the header to send (type of files) based on path |
|
Returns a copy of the mappings. |
|
Produces a html code for code. |
|
Processes a HTML content, replaces path which are relative to the root and not the project. |
|
Updates the cache. |
Methods¶
method |
truncated documentation |
---|---|
Regular constructor, an instance is created for each request, do not store any data for a longer time than a request. … |
|
What to do is case of GET request. |
|
What to do is case of POST request. |
|
Redirection when url is just the website. |
|
Displays something. |
|
Returns the content of a local file. |
|
logging function |
|
Runs the server. |
|
defines the header to send (type of files) based on path |
|
Tells what to do based on the path. The function intercepts the path /localfile/, otherwise it calls |
|
Functions to overload (executed after serve_content). |
|
Displays all the mapping for the default path. |
|
Shuts down the service. |
|
Shuts down the server, if it does not work, you can still kill the thread: |
Documentation¶
This file defines a simple local server delivering generating documentation.
-
class
pyquickhelper.serverdoc.documentation_server.
DocumentationHandler
(request, client_address, server)[source]¶ Bases:
http.server.BaseHTTPRequestHandler
Define a simple handler used by HTTPServer, it just serves local content.
Regular constructor, an instance is created for each request, do not store any data for a longer time than a request.
-
__init__
(request, client_address, server)[source]¶ Regular constructor, an instance is created for each request, do not store any data for a longer time than a request.
-
static
add_mapping
(key, value)[source]¶ Adds a mapping associated to a local path to watch.
- Parameters
key – key in
http://locahost:8008/key/
value – local path
Python documentation says list are protected against multithreading (concurrent accesses). If you run the server multiple times, the mappings stays because it is a static variable.
-
do_redirect
(path='/index.html')[source]¶ Redirection when url is just the website.
- Parameters
path – path to redirect to (a string)
-
static
execute
(localpath)[source]¶ Locally executes a python script.
- Parameters
localpath – local python script
- Returns
output, error
-
feed
(anys, script_python=False, params=None)[source]¶ Displays something.
- Parameters
anys – string
script_python – if True, the function processes script sections
params – extra parameters when a script must be executed (should be a dictionary)
A script section looks like:
<script type="text/python"> from pandas import DataFrame pars = [ { "key":k, "value":v } for k,v in params ] tbl = DataFrame (pars) print ( tbl.to_html(class_table="myclasstable") ) </script>
The server does not interpret Python, to do that, you need to use pyrsslocal.
-
get_file_content
(localpath, ftype, path=None)[source]¶ Returns the content of a local file.
- Parameters
localpath – local filename
ftype – r or rb
path – if != None, the filename will be path/localpath
- Returns
content
This function implements a simple cache mechanism.
-
static
get_from_cache
(key)[source]¶ Retrieves a file from the cache if it was cached, it the file was added later than a day, it returns None.
- Parameters
key – key
- Returns
content or None if None found or too old
-
static
get_ftype
(apath)[source]¶ defines the header to send (type of files) based on path
- Parameters
apath – location (a string)
- Returns
htype, ftype (html, css, …)
If a type is missing, you should look for the
MIME TYPE
on a search engine.See also media-types
-
static
html_code_renderer
(localpath, content)[source]¶ Produces a html code for code.
- Parameters
localpath – local path to file (local or not)
content – content of the file
- Returns
html string
-
static
process_html_path
(project, content)[source]¶ Processes a HTML content, replaces path which are relative to the root and not the project.
- Parameters
project – project, ex:
pyquickhelper
content – page content
- Returns
modified content
-
send_headers
(path)[source]¶ defines the header to send (type of files) based on path
- Parameters
path – location (a string)
- Returns
type (html, css, …)
-
serve_content
(cpath, method='GET')[source]¶ Tells what to do based on the path. The function intercepts the path /localfile/, otherwise it calls
serve_content_web
.If you type
http://localhost:8080/root/file
, assumingroot
is mapped to a local folder. It will display this file.- Parameters
cpath – ParseResult
method – GET or POST
-
serve_content_web
(path, method, params)[source]¶ Functions to overload (executed after serve_content).
- Parameters
path – ParseResult
method – GET or POST
params – params parsed from the url + others
-
-
class
pyquickhelper.serverdoc.documentation_server.
DocumentationThreadServer
(server)[source]¶ Bases:
threading.Thread
defines a thread which holds a web server
attribute
meaning
server
the server of run
- Parameters
server – to run
-
pyquickhelper.serverdoc.documentation_server.
run_doc_server
(server, mappings, thread=False, port=8079)[source]¶ Runs the server.
- Parameters
server – if None, it becomes
HTTPServer(('localhost', 8080), DocumentationHandler)
mappings – prefixes with local folders (dictionary)
thread – if True, the server is run in a thread and the function returns right away, otherwise, it runs the server.
port – port to use
- Returns
server if thread is False, the thread otherwise (the thread is started)
How to run a local server which serves the documentation?
The following code will create a local server: http://localhost:8079/pyquickhelper/.
this_fold = os.path.dirname(pyquickhelper.serverdoc.documentation_server.__file__) this_path = os.path.abspath( os.path.join( this_fold, "..", "..", "..", "dist", "html") ) run_doc_server(None, mappings = { "pyquickhelper": this_path } )
The same server can serves more than one project. More than one mappings can be sent.