Tương tác so với xuất khẩu tĩnh
Các số liệu Plotly có tính tương tác khi được xem trong trình duyệt web: bạn có thể di chuột qua các điểm dữ liệu, trục và trục zoom, và hiển thị và ẩn dấu vết bằng cách nhấp hoặc nhấp đúp vào Legend. Bạn có thể xuất các số liệu sang các định dạng tệp hình ảnh tĩnh như PNG, JPEG, SVG hoặc PDF hoặc bạn có thể xuất chúng sang các tệp HTML có thể được mở trong trình duyệt. Trang này giải thích cách làm sau.
Lưu vào tệp HTML
Bất kỳ hình nào cũng có thể được lưu dưới dạng tệp HTML bằng phương thức write_html
. Các tệp HTML này có thể được mở trong bất kỳ trình duyệt web nào để truy cập hình tương tác đầy đủ.
import plotly.express as px fig =px.scatter[x=range[10], y=range[10]] fig.write_html["path/to/file.html"]
Kiểm soát kích thước của tệp HTML
Theo mặc định, tệp HTML kết quả là một tệp HTML khép kín hoàn toàn có thể được tải lên máy chủ web hoặc được chia sẻ qua email hoặc các cơ chế chia sẻ tệp khác. Nhược điểm của phương pháp này là tệp rất lớn [5MB+] vì nó chứa một bản sao được in của thư viện Plotly.js cần thiết để làm cho con số tương tác. Điều này có thể được kiểm soát thông qua đối số include_plotlyjs
[xem bên dưới].
HTML xuất trong Dash¶
Dash là cách tốt nhất để xây dựng các ứng dụng phân tích trong Python bằng cách sử dụng các số liệu thuần túy. Để chạy ứng dụng bên dưới, chạy pip install dash
, nhấp vào "Tải xuống" để lấy mã và chạy python app.py
.
Bắt đầu với các tài liệu Dash chính thức và tìm hiểu cách dễ dàng tạo kiểu và triển khai các ứng dụng như thế này với Dash Enterprise.learn how to effortlessly style & deploy apps like this with Dash Enterprise.
Đăng ký Câu lạc bộ Dash → Bảng gian lận miễn phí cộng với các bản cập nhật từ Chris Parmer và Adam Schroeder đã gửi đến hộp thư đến của bạn hai tháng một lần. Bao gồm các mẹo và thủ thuật, ứng dụng cộng đồng và lặn sâu vào kiến trúc Dash. Tham gia ngay. → Free cheat sheets plus updates from Chris Parmer and Adam Schroeder delivered to your inbox every two months. Includes tips and tricks, community apps, and deep dives into the Dash architecture. Join now.
Tài liệu tham số đầy đủ
In [2]:
import plotly.graph_objects as go help[go.Figure.write_html]
Help on function write_html in module plotly.basedatatypes: write_html[self, *args, **kwargs] Write a figure to an HTML file representation Parameters ---------- file: str or writeable A string representing a local file path or a writeable object [e.g. a pathlib.Path object or an open file descriptor] config: dict or None [default None] Plotly.js figure config options auto_play: bool [default=True] Whether to automatically start the animation sequence on page load if the figure contains frames. Has no effect if the figure does not contain frames. include_plotlyjs: bool or string [default True] Specifies how the plotly.js library is included/loaded in the output div string. If True, a script tag containing the plotly.js source code [~3MB] is included in the output. HTML files generated with this option are fully self-contained and can be used offline. If 'cdn', a script tag that references the plotly.js CDN is included in the output. HTML files generated with this option are about 3MB smaller than those generated with include_plotlyjs=True, but they require an active internet connection in order to load the plotly.js library. If 'directory', a script tag is included that references an external plotly.min.js bundle that is assumed to reside in the same directory as the HTML file. If `file` is a string to a local file path and `full_html` is True then If 'directory', a script tag is included that references an external plotly.min.js bundle that is assumed to reside in the same directory as the HTML file. If `file` is a string to a local file path and `full_html` is True, then the plotly.min.js bundle is copied into the directory of the resulting HTML file. If a file named plotly.min.js already exists in the output directory then this file is left unmodified and no copy is performed. HTML files generated with this option can be used offline, but they require a copy of the plotly.min.js bundle in the same directory. This option is useful when many figures will be saved as HTML files in the same directory because the plotly.js source code will be included only once per output directory, rather than once per output file. If 'require', Plotly.js is loaded using require.js. This option assumes that require.js is globally available and that it has been globally configured to know how to find Plotly.js as 'plotly'. This option is not advised when full_html=True as it will result in a non-functional html file. If a string that ends in '.js', a script tag is included that references the specified path. This approach can be used to point the resulting HTML file to an alternative CDN or local bundle. If False, no script tag referencing plotly.js is included. This is useful when the resulting div string will be placed inside an HTML document that already loads plotly.js. This option is not advised when full_html=True as it will result in a non-functional html file. include_mathjax: bool or string [default False] Specifies how the MathJax.js library is included in the output html div string. MathJax is required in order to display labels with LaTeX typesetting. If False, no script tag referencing MathJax.js will be included in the output. If 'cdn', a script tag that references a MathJax CDN location will be included in the output. HTML div strings generated with this option will be able to display LaTeX typesetting as long as internet access is available. If a string that ends in '.js', a script tag is included that references the specified path. This approach can be used to point the resulting HTML div string to an alternative CDN. post_script: str or list or None [default None] JavaScript snippet[s] to be included in the resulting div just after plot creation. The string[s] may include '{plot_id}' placeholders that will then be replaced by the `id` of the div element that the plotly.js figure is associated with. One application for this script is to install custom plotly.js event handlers. full_html: bool [default True] If True, produce a string containing a complete HTML document starting with an tag. If False, produce a string containing a singleelement. animation_opts: dict or None [default None] dict of custom animation parameters to be passed to the function Plotly.animate in Plotly.js. See //github.com/plotly/plotly.js/blob/master/src/plots/animation_attributes.js for available options. Has no effect if the figure does not contain frames, or auto_play is False. default_width, default_height: number or str [default '100%'] The default figure width/height to use if the provided figure does not specify its own layout.width/layout.height property. May be specified in pixels as an integer [e.g. 500], or as a css width style string [e.g. '500px', '100%']. validate: bool [default True] True if the figure should be validated before being converted to JSON, False otherwise. auto_open: bool [default True] If True, open the saved file in a web browser after saving. This argument only applies if `full_html` is True. div_id: str [default None] If provided, this is the value of the id attribute of the div tag. If None, the id attribute is a UUID. Returns ------- str Representation of figure as an HTML div stringCòn Dash thì sao? ¶
Dash là một khung nguồn mở để xây dựng các ứng dụng phân tích, không cần JavaScript và nó được tích hợp chặt chẽ với thư viện đồ thị đồ họa.
Tìm hiểu về cách cài đặt Dash tại //dash.plot.ly/installation.
Ở mọi nơi trong trang này mà bạn thấy
fig.show[]
, bạn có thể hiển thị cùng một hình trong một ứng dụng Dash bằng cách chuyển nó sang đối sốfigure
của thành phầnimport plotly.graph_objects as go help[go.Figure.write_html]0 từ góiimport plotly.graph_objects as go help[go.Figure.write_html]1 tích hợp như thế này:import plotly.graph_objects as go # or plotly.express as px fig = go.Figure[] # or any Plotly Express function e.g. px.bar[...] # fig.add_trace[ ... ] # fig.update_layout[ ... ] import dash import dash_core_components as dcc import dash_html_components as html app = dash.Dash[] app.layout = html.Div[[ dcc.Graph[figure=fig] ]] app.run_server[debug=True, use_reloader=False] # Turn off reloader if inside JupyterLàm thế nào để bạn có được đầu ra biểu đồ Python trực tiếp vào trang HTML?
Ba bước được yêu cầu để tích hợp biểu đồ Python vào trang web HTML: Tạo biểu đồ trong Plot.ly hoặc Altair. Lưu đồ thị dưới dạng trang HTML. Thao tác với HTML được tạo ra.generate the graph either in Plot.ly or Altair. save the graph as an HTML page. manipulate the generated HTML.Làm cách nào để lưu một con số âm mưu trong HTML?
Bất kỳ hình nào cũng có thể được lưu dưới dạng tệp HTML bằng phương thức Write_HTML.Các tệp HTML này có thể được mở trong bất kỳ trình duyệt web nào để truy cập hình tương tác đầy đủ.using the write_html method. These HTML files can be opened in any web browser to access the fully interactive figure.Làm cách nào để tiết kiệm âm mưu trong Python?
Viết tệp hình ảnh..Viết tệp hình ảnh.Âm mưu.....Plotly.py có thể xuất các số liệu vào một số định dạng hình ảnh raster bao gồm PNG, ... Hình.....JPEG, ... Hình.....và trang web.quả sung.....Plotly.py cũng có thể xuất các số liệu ở một số định dạng vectơ bao gồm SVG, ... Hình.....PDF, ... Hình.....và EPS [yêu cầu thư viện poppler] Hình.....Nhận hình ảnh như byte ..Làm thế nào để bạn chia sẻ một biểu đồ cốt truyện tương tác?
Để chia sẻ một lô từ không gian làm việc của Studio Studio, nhấp vào nút 'Chia sẻ' ở phía bên trái sau khi lưu cốt truyện.Phương thức chia sẻ sẽ bật lên và hiển thị một liên kết trong tab 'nhúng'.Sau đó, bạn có thể sao chép và dán liên kết này vào trang web của bạn.Bạn có tùy chọn nhúng cốt truyện của mình dưới dạng đoạn trích HTML hoặc iframe.click 'Share' button on the left-hand side after saving the plot. The Share modal will pop-up and display a link under the 'Embed' tab. You can then copy and paste this link to your website. You have the option of embedding your plot as an HTML snippet or iframe.Chủ Đề